a binary tree
DESCRIPTION
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)), - PowerPoint PPT PresentationTRANSCRIPT
A Binary Tree
root
leafleaf
leaf
A Binary Tree
root
leafleaf
leaf
descendent of rootdescendent of root
parent of leaf
Complete Binary TreeOn 12 nodes
Full Binary Tree on 7 nodes
Number of nodes at depth i = 2Maximum depth of a node = floor(log2(n)),a formula which also holds for any complete binary tree
i
28
16 33
8 18 30 42
Searchinglooking for 31
6 15 17 31
28
16 33
8 18 30 42
Searchinglooking for 31
6 15 17 31
Compare – go right
28
16 33
8 18 30 42
Searchinglooking for 31
6 15 17 31
Compare – go left
28
16 33
8 18 30 42
Searchinglooking for 31
6 15 17 31
Compare – go right
28
16 33
8 18 30 42
Searchinglooking for 31
6 15 17 31
Found it!
Complexity O(d), where d is depth of tree
28
16 33
8 18 30 42
Inserting 22
6 15 17 31
28
16 33
8 18 30 42
Inserting 22
6 15 17 31
compare to 28, move left
28
16 33
8 18 30 42
Inserting 22
6 15 17 31
compare to 16, move right
28
16 33
8 18 30 42
Inserting 22
6 15 17 31
compare to 18, insert to right
28
16 33
8 18 30 42
Inserting 22
6 15 17 31
done
22
complexity: O(d), where d is depth of tree
28
16 33
8 18 30 42
Delete 15
6 15 17 31
Delete leaf nodes by simply cutting them off
Deletions require no list comparisons, only pointer
assignments
28
16 33
8 18 30 42
15 Deleted
6 17 31
28
16 33
8 18 30 42
Delete 18
6 17 31
To delete a node with onlyone child, simply “splice”
around it.
28
16 33
8 17 30 42
18 Deleted
6 31
To delete a node with onlyone child, simply “splice”
around it.
28
16 33
8 17 30 42
Delete 28
6 31
To delete a node with twochildren, replace it with itsinorder successor, and then delete the inorder successor
(which has at most one child).
30
16 33
8 17 30 42
Delete 28
6 31
Replace 28 by inordersuccessor 30.
30
16 33
8 17 30 42
Delete inordersuccessor of 28
6 31
Now delete original occurrenceof 28’s inorder successor 30.
30
16 33
8 17 31 42
6
Deletion of 28 now complete.
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 indices2i + 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
5
12 34
23 75 35 55
33 54 89
A (Min) Heap
12 34
23 75 35 55
33 54 89
Delete a Node
12 34
23 75 35 55
33 54
89Delete a Node
89 34
23 75 35 55
33 54
12Delete a Node
23 34
89 75 35 55
33 54
12Delete a Node
23 34
33 75 35 55
89 54
12Delete a Node
23 34
33 75 35 55
89 54
12Delete a Node
Complexity of delete: O(log n)
23 34
33 75 35 55
89 54
12
14
Insert a Node
23 34
33 14 35 55
89 54
12
75
Insert a Node
14 34
33 23 35 55
89 54
12
75
Insert a Node
14 34
33 23 35 55
89 54
12
75
Insert a Node
Complexity of insert: O(log n)
12Build a (min)Heap byinserting theelements 12,8,6,5,4,3,2,1stored in array A[0:7] as in MakeMinHeap1
Complexity: 0
8
12Build byinserting
Complexity: 0
12
8Build byinserting
Complexity: 0+1
12 6
8Build by inserting
Complexity: 0+1
12 8
6Build byinserting
Complexity: 0+1+1
12 8
5
6Build byinserting
Complexity: 0+1+1
5 8
12
6Build byinserting
Complexity: 0+1+1
6 8
12
5Build byinserting
Complexity: 0+1+1+2
6 8
12 4
5Build byinserting
Complexity: 0+1+1+2
4 8
12 6
5Build by inserting
Complexity: 0+1+1+2
5 8
12 6
4Build byinserting
Complexity: 0+1+1+2+2
5 8
12 6 3
4Build byinserting
Complexity: 0+1+1+2+2
5 3
12 6 8
4Build byinserting
Complexity: 0+1+1+2+2
5 4
12 6 8
3Build byinserting
Complexity: 0+1+1+2+2+2
5 4
12 6 8 2
3Build byinserting
Complexity: 0+1+1+2+2+2
5 2
12 6 8 4
3Build byinserting
Complexity: 0+1+1+2+2+2
5 3
12 6 8 4
2Build byinserting
Complexity: 0+1+1+2+2+2+2
5 3
12 6 8 4
1
2Build byinserting
Complexity: 0+1+1+2+2+2+2
5 3
1 6 8 4
12
2Build byinserting
Complexity: 0+1+1+2+2+2+2
1 3
5 6 8 4
12
2Build byinserting
Complexity: 0+1+1+2+2+2+2
2 3
5 6 8 4
12
1Build byinserting
Complexity: 0+1+1+2+2+2+2+3
2 3
5 6 8 4
12
1Build byinserting
Complexity: 0+1+1+2+2+2+2+3
Keep going: 2 *1 + 2 *2 + 2 *3 + 2 *4 + 2 *5 + ... + 2 *log(n) 1 2 3 4 5 log(n)
(min) heap created
2 *1 + 2 *2 + 2 *3 + 2 *4 + 2 *5 + ... + 2 *log(n) 1 2 3 4 5 log(n)
2 *1 + 2 *1 + 2 *1 + 2 *1 + 2 *1 + ... + 2 *1 = 2(2 - 1) 1 2 3 4 5 log(n)
2 *1 + 2 *1 + 2 *1 + 2 *1 + ... + 2 *1 = 2(2 - 2) 2 3 4 5 log(n)
2 *1 + 2 *1 + 2 *1 + ... + 2 *l = 2(2 - 4)3 4 5 log(n)
2 *1 + 2 *1 + ... + 2 *l = 2(2 - 8)4 5 log(n)
log(n)
log(n)
log(n)
log(n)
ε Θ(n log(n))But 1+2+4+8+ ... +2 = 2 -1so sum of above sums is 2n log(n) – n + 1
log(n)-1 log(n)
8 6
5 4 3 2
1
12Build by repeatedadjusting as inMakeMinHeap2
adjust
8 6
1 4 3 2
5
12Build by repeatedadjusting as inMakeMinHeap2
complexity: 1
8 6
1 4 3 2
5
12Build by repeatedadjusting as inMakeMinHeap2
adjust
8 2
1 4 3 6
5
12Build by repeatedadjusting as inMakeMinHeap2
complexity: 2
complexity so far: 1 + 2
8 2
1 4 3 6
5
12Build by repeatedadjusting as inMakeMinHeap2
adjust
1 2
8 4 3 6
5
12Build by repeatedadjusting as inMakeMinHeap2
complexity: 2
continueadjust
1 2
5 4 3 6
8
12Build by repeatedadjusting as inMakeMinHeap2
adjust completed
complexity: 1
complexity so far: 1 + 2 + 2 + 1
1 2
5 4 3 6
8
12Build by repeatedadjusting as inMakeMinHeap2
adjust
12 2
5 4 3 6
8
1Build by repeatedadjusting as inMakeMinHeap2
continueadjust
complexity: 2
4 2
5 12 3 6
8
1Build by repeatedadjusting as inMakeMinHeap2
complexity: 2
(Min) Heap now created
Total complexity:1 + 2 + 2 + 1 + 2+ 2
To compute the total complexity of the heap creation, let d = floor(log2n)Note that we start adjusting at nodes at level d – 1. Moreover, when weAdjust at a node at level i, we make at most 2(d – i) comparisons. Hence,We make at most njid
d
j
jdd
i
i 2)2/1(2)(21
1
0
comparisons altogether.