700 likes | 858 Views
A Binary Tree. root. leaf. leaf. leaf. A Binary Tree. root. descendent of root. descendent of root. parent of leaf. leaf. leaf. leaf. Complete Binary Tree On 12 nodes. Full Binary Tree on 7 nodes. i. Number of nodes at depth i = 2 Maximum depth of a node = floor(log 2 (n)),
E N D
A Binary Tree root leaf leaf leaf
A Binary Tree root descendent of root descendent of root parent of leaf leaf leaf leaf
Complete Binary Tree On 12 nodes
Full Binary Tree on 7 nodes i Number of nodes at depth i = 2 Maximum depth of a node = floor(log2(n)), a formula which also holds for any complete binary tree
Searching looking for 31 28 33 16 8 18 30 42 6 17 15 31
Searching looking for 31 28 Compare – go right 33 16 8 18 30 42 6 17 15 31
Searching looking for 31 28 33 16 Compare – go left 8 18 30 42 6 17 15 31
Searching looking for 31 28 33 16 8 18 30 42 Compare – go right 6 17 15 31
Searching looking for 31 28 33 16 8 18 30 42 Found it! 6 17 15 31 Complexity O(d), where d is depth of tree
Inserting 22 28 33 16 8 18 30 42 6 31 17 15
Inserting 22 28 compare to 28, move left 33 16 8 18 30 42 6 31 17 15
Inserting 22 28 compare to 16, move right 33 16 8 18 30 42 6 31 17 15
Inserting 22 28 33 16 8 18 30 42 compare to 18, insert to right 6 31 17 15
Inserting 22 28 33 16 8 18 30 42 6 31 17 15 22 done complexity: O(d), where d is depth of tree
Deletions require no list comparisons, only pointer assignments 28 33 16 Delete 15 8 18 30 42 6 15 31 17 Delete leaf nodes by simply cutting them off
15 Deleted 28 33 16 8 18 30 42 6 31 17
Delete 18 28 To delete a node with only one child, simply “splice” around it. 33 16 8 18 30 42 6 31 17
18 Deleted 28 To delete a node with only one child, simply “splice” around it. 33 16 8 17 30 42 6 31
To delete a node with two children, replace it with its inorder successor, and then delete the inorder successor (which has at most one child). Delete 28 28 33 16 8 17 30 42 6 31
Replace 28 by inorder successor 30. Delete 28 30 33 16 8 17 30 42 6 31
Now delete original occurrence of 28’s inorder successor 30. Delete inorder successor of 28 30 33 16 8 17 30 42 6 31
Deletion of 28 now complete. 30 33 16 8 17 31 42 6
A (Min) Heap A Complete Binary Tree with nodes containing numbers. Hence, full at every level except (possibly) the deepest. Nodes at deepest level number filled left-to-right. Children (if any) of node at index i have indices 2i + 1and 2i + 2, respectively. Parent of node at index i has index floor((i-1)/2). All descendents of a node contain not smaller numbers. A (Max) Heap is same as above, except “not smaller” is replaced by “not larger.” A (Max) Heap: Same as above, except “not smaller” replaced by “not larger.” A
A (Min) Heap 5 34 12 23 75 35 55 33 54 89
Delete a Node 34 12 23 75 35 55 33 54 89
Delete a Node 89 34 12 23 75 35 55 33 54
Delete a Node 12 34 89 23 75 35 55 33 54
Delete a Node 12 34 23 89 75 35 55 33 54
Delete a Node 12 34 23 33 75 35 55 89 54
Delete a Node 12 34 23 33 75 35 55 89 54 Complexity of delete: O(log n)
Insert a Node 12 34 23 33 75 35 55 89 54 14
Insert a Node 12 34 23 33 14 35 55 89 54 75
Insert a Node 12 34 14 33 23 35 55 89 54 75
Insert a Node 12 34 14 33 23 35 55 89 54 75 Complexity of insert: O(log n)
Build a (min) Heap by inserting the elements 12,8,6,5,4,3,2,1 stored in array A[0:7] as in MakeMinHeap1 12 Complexity: 0
Build by inserting 12 8 Complexity: 0
Build by inserting 8 12 Complexity: 0+1
Build by inserting 8 6 12 Complexity: 0+1
Build by inserting 6 8 12 Complexity: 0+1+1
Build by inserting 6 8 12 5 Complexity: 0+1+1
Build by inserting 6 8 5 12 Complexity: 0+1+1
Build by inserting 5 8 6 12 Complexity: 0+1+1+2
Build by inserting 5 8 6 12 4 Complexity: 0+1+1+2
Build by inserting 5 8 4 12 6 Complexity: 0+1+1+2
Build by inserting 4 8 5 12 6 Complexity: 0+1+1+2+2
Build by inserting 4 8 5 12 6 3 Complexity: 0+1+1+2+2
Build by inserting 4 3 5 12 6 8 Complexity: 0+1+1+2+2
Build by inserting 3 4 5 12 6 8 Complexity: 0+1+1+2+2+2
Build by inserting 3 4 5 12 6 8 2 Complexity: 0+1+1+2+2+2
Build by inserting 3 2 5 12 6 8 4 Complexity: 0+1+1+2+2+2