complete binary trees
DESCRIPTION
Complete binary trees. Outline. Introducing complete binary trees Background Definitions Examples Logarithmic height Array storage. Background. 5.3. A perfect binary tree has ideal properties but restricted in the number of nodes: n = 2 h – 1 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/1.jpg)
ECE 250 Algorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer EngineeringUniversity of WaterlooWaterloo, Ontario, Canada
© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Complete binary trees
![Page 2: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/2.jpg)
2Complete binary trees
Outline
Introducing complete binary trees– Background– Definitions– Examples– Logarithmic height– Array storage
![Page 3: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/3.jpg)
3Complete binary trees
Background
A perfect binary tree has ideal properties but restricted in the number of nodes: n = 2h – 1
1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, ....
We require binary trees which are– Similar to perfect binary trees, but– Defined for all n
5.3
![Page 4: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/4.jpg)
4Complete binary trees
Definition
A complete binary tree filled at each depth from left to right:
5.3.1
![Page 5: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/5.jpg)
5Complete binary trees
Definition
The order is identical to that of a breadth-first traversal
5.3.1
![Page 6: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/6.jpg)
6Complete binary trees
Recursive Definition
Recursive definition: a binary tree with a single node is a complete binary tree of height h = 0 and a complete binary tree of height h is a tree where either:– The left sub-tree is a complete tree of height h – 1 and the right sub-
tree is a perfect tree of height h – 2, or– The left sub-tree is perfect tree with height h – 1 and the right sub-tree
is complete tree with height h – 1
5.3.1
![Page 7: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/7.jpg)
7Complete binary trees
Height
TheoremThe height of a complete binary tree with n nodes is h = lg(⌊ n)⌋
Proof:– Base case:
• When n = 1 then lg(⌊ 1) = ⌋ 0 and a tree with one node is a complete tree with height h = 0
– Inductive step:• Assume that a complete tree with n nodes has height lg(⌊ n)⌋• Must show that lg(⌊ n + 1)⌋ gives the height of a complete tree with
n + 1 nodes• Two cases:
– If the tree with n nodes is perfect, and– If the tree with n nodes is complete but not perfect
5.3.2
![Page 8: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/8.jpg)
8Complete binary trees
Height
Case 1 (the tree is perfect):– If it is a perfect tree then
• Adding one more node must increase the height– Before the insertion, it had n = 2h + 1 – 1 nodes:
– Thus,
– However,
1 1
1 1
1
2 2 1 2
lg 2 lg 2 1 lg 2 1
lg 2 1 1
h h h
h h h
h
h h
h h
lg n h
1 1lg 1 lg 2 1 1 lg 2 1h hn h
5.3.2
![Page 9: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/9.jpg)
9Complete binary trees
Height
Case 2 (the tree is complete but not perfect):– If it is not a perfect tree then
– Consequently, the height is unchanged: lg( ⌊ n + 1 ) = ⌋ h
By mathematical induction, the statement must be true for all n ≥ 1
1
1
1
2 2 1
2 1 1 2
lg 2 1 lg 1 lg 2 1
lg 2 1 lg 1 1
h h
h h
h h
h
n
n
h n h
h n h
5.3.2
![Page 10: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/10.jpg)
10Complete binary trees
Array storage
We are able to store a complete tree as an array– Traverse the tree in breadth-first order, placing the entries into the array
5.3.3
![Page 11: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/11.jpg)
11Complete binary trees
Array storage
We can store this in an array after a quick traversal:
5.3.3
![Page 12: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/12.jpg)
12Complete binary trees
Array storage
To insert another node while maintaining the complete-binary-tree structure, we must insert into the next array location
5.3.3
![Page 13: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/13.jpg)
13Complete binary trees
Array storage
To remove a node while keeping the complete-tree structure, we must remove the last element in the array
5.3.3
![Page 14: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/14.jpg)
14Complete binary trees
Leaving the first entry blank yields a bonus:– The children of the node with index k are in 2k and 2k + 1– The parent of node with index k is in k ÷ 2
Array storage
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
5.3.3
![Page 15: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/15.jpg)
15Complete binary trees
Leaving the first entry blank yields a bonus:– In C++, this simplifies the calculations:
Array storage
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
5.3.3
parent = k >> 1;left_child = k << 1;right_child = left_child | 1;
![Page 16: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/16.jpg)
16Complete binary trees
Array storage
For example, node 10 has index 5:– Its children 13 and 23 have indices 10 and 11, respectively
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
5.3.3
![Page 17: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/17.jpg)
17Complete binary trees
Array storage
For example, node 10 has index 5:– Its children 13 and 23 have indices 10 and 11, respectively– Its parent is node 9 with index 5/2 = 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
5.3.3
![Page 18: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/18.jpg)
18Complete binary trees
Array storage
Question: why not store any tree as an array using breadth-first traversals?– There is a significant potential for a lot of wasted memory
Consider this tree with 12 nodes would require an array of size 32– Adding a child to node K doubles the required memory
5.3.4
![Page 19: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/19.jpg)
19Complete binary trees
Array storage
In the worst case, an exponentialamount of memory is required
These nodes would be storedin entries 1, 3, 6, 13, 26, 52, 105
5.3.4
![Page 20: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/20.jpg)
20Complete binary trees
Summary
In this topic, we have covered the concept of a complete binary tree:– A useful relaxation of the concept of a perfect binary tree– It has a compact array representation
![Page 21: Complete binary trees](https://reader033.vdocuments.us/reader033/viewer/2022061617/56815cb0550346895dcaae79/html5/thumbnails/21.jpg)
21Complete binary trees
Usage Notes
• These slides are made publicly available on the web for anyone to use
• If you choose to use them, or a part thereof, for a course at another institution, I ask only three things:– that you inform me that you are using the slides,– that you acknowledge my work, and– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides
Sincerely,Douglas Wilhelm Harder, [email protected]