![Page 1: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/1.jpg)
Binary Trees
![Page 2: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/2.jpg)
Binary Trees
● A binary tree has nodes, similar to nodes in a linked list structure.
● Data of one sort or another may be stored at each node.
● But it is the connections between the nodes which characterize a binary tree.
![Page 3: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/3.jpg)
Binary Trees
● A binary tree has nodes, similar to nodes in a linked list structure.
● Data of one sort or another may be stored at each node.
● But it is the connections between the nodes which characterize a binary tree.
An example canillustrate how theconnections work
![Page 4: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/4.jpg)
A Binary Tree of States
In this example, the data contained at each node is one of the 50 states.
![Page 5: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/5.jpg)
A Binary Tree of States
Each tree has a special node called its root, usually drawn at the top.
![Page 6: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/6.jpg)
A Binary Tree of States
Each tree has a special node called its root, usually drawn at the top. The example tree
has Washingtonas its root.
![Page 7: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/7.jpg)
A Binary Tree of States
Each node is permitted to have two links to other nodes, called the left child and the right child.
![Page 8: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/8.jpg)
A Binary Tree of States
Each node is permitted to have two links to other nodes, called the left child and the right child.
![Page 9: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/9.jpg)
A Binary Tree of States
Children are usually drawn below a node.
The right child ofWashington is
Colorado.
The left child ofWashington is
Arkansas.
![Page 10: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/10.jpg)
A Binary Tree of States
Some nodes have only one child.
Arkansas has aleft child, but no
right child.
![Page 11: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/11.jpg)
A Binary Tree of States
A node with no children is called a leaf.
![Page 12: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/12.jpg)
A Binary Tree of States
Each node is called the parent of its children.
Washington is theparent of Arkansas
and Colorado.
![Page 13: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/13.jpg)
A Binary Tree of States
Two rules about parents:
The root has no parent.
Every other node has exactly one parent.
![Page 14: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/14.jpg)
A Binary Tree of States
Two nodes with the same parent are called siblings.
Arkansasand Coloradoare siblings.
![Page 15: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/15.jpg)
Complete Binary Trees
A complete binary tree is a special kind of binary tree which will be useful to us.
![Page 16: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/16.jpg)
Complete Binary Trees
A complete binary tree is a special kind of binary tree which will be useful to us.
When a completebinary tree is built,
its first node must bethe root.
![Page 17: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/17.jpg)
Complete Binary Trees
The second node of a complete binary tree is always the left child of the root...
![Page 18: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/18.jpg)
Complete Binary Trees
The second node of a complete binary tree is always the left child of the root...
... and the third node is always the right child of the root.
![Page 19: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/19.jpg)
Complete Binary Trees
The next nodes must always fill the next level from left to right.
![Page 20: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/20.jpg)
Complete Binary Trees
The next nodes must always fill the next level from left to right.
![Page 21: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/21.jpg)
Complete Binary Trees
The next nodes must always fill the next level from left to right.
![Page 22: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/22.jpg)
Complete Binary Trees
The next nodes must always fill the next level from left to right.
![Page 23: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/23.jpg)
Complete Binary Trees
The next nodes must always fill the next level from left to right.
![Page 24: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/24.jpg)
Complete Binary Trees
The next nodes must always fill the next level from left to right.
![Page 25: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/25.jpg)
Implementing a Complete Binary Tree
● We will store the date from the nodes in a partially-filled array.
An array of dataWe don't care what's inthis part of the array.
An integer to keeptrack of how many nodes are in the tree
3
![Page 26: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/26.jpg)
Implementing a Complete Binary Tree
● Data from root – always at index 0● If data from a non-root node is at index i
– Data for parent is at index (i-1)/2
– Data for children (if they exist) are at indices 2i+1 & 2i+2
● Very easy to parent from children & vice-versa● Could use static or dynamic array or STL vector
![Page 27: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/27.jpg)
General Binary Trees
● Finite set of nodes– Root – special node
– Up to 2 children
– Each node – only 1 parent – except root which has no parent
● Ancestor– Node's parent – first ancestor
– Parent of parent is next ancestor
– Root is ancestor of every other node
![Page 28: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/28.jpg)
General Binary Trees
● Descendant– Node's children are first descendants
– Children's children are next descendants
● Subtree– Any node in a tree can be viewed as a root of a new
tree – called subtree
● Depth of a node – Number of “steps” up tree from node to root
![Page 29: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/29.jpg)
General Binary Trees
● Leaf – node with no children● Depth of a tree – maximum depth of any of its
leaves● Full binary tree – every leaf has the same depth
![Page 30: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/30.jpg)
General Trees
● Finite set of nodes● Special node – root● Each node – may be associated with one or more
nodes – children● Each node has exactly 1 parent – except root
![Page 31: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/31.jpg)
Tree Representations
● Array representation of complete binary trees● Node representation of binary trees
![Page 32: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/32.jpg)
Tree Traversals
● Traversal – visit every node once● Binary trees
– Depth first traversals – difference is the order that the node, left child, right child are visited
● Pre-order● In-order● Post-order● Backward pre-order● Backward in-order● Backward post-order
– Breadth first traversals● Forward● backwards
![Page 33: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/33.jpg)
Traversals
● Traversals are frequently used● Do not want to rewrite traversal for every usage● Pass in usage (function) as an argument to traversal
function
![Page 34: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/34.jpg)
Function Parameters
● Parameter to a function may be a function● Parameter declared by writing return type the
function name and then list of parameter types in parentheses
– void foo (void f (int&), ....)
– Actual argument – any function with that signature
![Page 35: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/35.jpg)
Template Function Parameters
void do_it (void f(int&), int data[], size_t n)
{
for (size_t i = 0; i < n; ++i)
f(data[i]);
}● f is applied to every element in the array● Can generalize to arrays of any type
template <class Item, class SizeType>
void do_it (void f(Item&), Item data[], SizeType n)
{
for (SizeType i = 0; i < n; ++i)
f(data[i]);
}
![Page 36: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/36.jpg)
Further Generalization
● Restricted as to signature of function– Want a different function signature – need to
construct a new templatetemplate <class Process, class Item, class SizeType>
void do_it (Process f, Item data[], SizeType n)
{
for (SizeType i = 0; i < n; ++i)
f(data[i]);
}● Process – type of first argument (function)
![Page 37: Complete Binary Trees - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~dgayler/cs3304/class_notes/binary_trees.pdf · list structure. Data of one sort ... Complete Binary Trees A complete](https://reader031.vdocuments.site/reader031/viewer/2022022600/5b4399ef7f8b9ac3618b6833/html5/thumbnails/37.jpg)
THE END
Presentation copyright 2010 Addison Wesley Longman,For use with Data Structures and Other Objects Using Javaby Michael Main.
Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.