trees a tree's a tree. how many more do you need to look at? --ronald reagan
TRANSCRIPT
![Page 1: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/1.jpg)
TREESA tree's a tree. How many more do you need to look at?
--Ronald Reagan
![Page 2: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/2.jpg)
Tree Rules• One root• Each node may have 1 or more children• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root
root
child
parent
leaf leaf
![Page 3: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/3.jpg)
Tree Example
Root node
Node with 2 children
Node with right child
Leaf node
Node with left child
![Page 4: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/4.jpg)
Tree Examples
![Page 5: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/5.jpg)
Tree Examples
![Page 6: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/6.jpg)
Binary Tree Rules• One root• Each node can have a maximum of 2 child nodes
• Left child• Right child
• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root
![Page 7: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/7.jpg)
Binary Search Tree
• One root• Each node can have a maximum of 2 child nodes
• Left child• Right child
• The left child’s value is less than the node.• The right child’s value is greater than the node.• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root
![Page 8: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/8.jpg)
![Page 9: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/9.jpg)
Full Binary Tree• Every leaf has the same depth
![Page 10: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/10.jpg)
Complete Binary Tree• All new leaves of a full binary tree are added from the left.
• Add left-most nodes first
![Page 11: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/11.jpg)
Heap• One root• Each node can have a maximum of 2 child nodes
• Left child• Right child
• Complete binary tree• Every node must have max children except…• Leaf level can be incomplete if all nodes are as far left as possible
• The node’s value is >= the value of its children• The left child’s value is less than (or equal to) the node.• The right child’s value is less than (or equal to) the node.
• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root
![Page 12: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/12.jpg)
B Tree
• One root• Each node can have MINIMUM entries (key indices) for children nodes up to twice the value
of MINIMUM for a single node.• The entries are stored in an array, sorted from the smallest to largest• The number of subtrees below a nonleaf node is always one more than the number of entries
in the node.• For any nonleaf node an entry at index i is greater than all the entries in subtree number i of
that node and an entry at index i is less than all the entries in subtree number i+ 1 of the node.• Data items are only stored in leaves• All leaves have the same depth• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root
![Page 13: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/13.jpg)
Tree Traversals (pp 500)• Climbing all nodes of a tree• Traversals are named by when the root is processed and
“root” implies all roots of all subtrees• Pre-order traversal• In-order traversal• Post-order traversal
![Page 14: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/14.jpg)
Pre-Order Traversal• Root is processed previous to its subtrees• 1. process root• 2. process nodes in left subtree recursively• 3. process nodes in right subtree recursivelytemplate <class Item>void preorder_print(binary_tree_node <Item>* node_ptr){ if (node_ptr != NULL) { //1. process root std::cout << node_ptr->data() << std::endl; //2. process left preorder_print(node_ptr->left()); //3. process right preorder_print(node_ptr->right()); }}
![Page 15: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/15.jpg)
In-Order Traversal• Root is processed between its subtrees• 1. process nodes in left subtree recursively• 2. process root• 3. process nodes in right subtree recursivelytemplate <class Item>void inorder_print(binary_tree_node <Item>* node_ptr){ if (node_ptr != NULL) { //1. process left inorder_print(node_ptr->left()); //2. process root std::cout << node_ptr->data() << std::endl; //3. process right inorder_print(node_ptr->right()); }}
![Page 16: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan](https://reader035.vdocument.in/reader035/viewer/2022072011/56649e355503460f94b247f2/html5/thumbnails/16.jpg)
Post-Order Traversal• Root is processed after its subtrees• 1. process nodes in left subtree recursively• 2. process nodes in right subtree recursively• 3. process roottemplate <class Item>void postorder_print(binary_tree_node <Item>* node_ptr){ if (node_ptr != NULL) { //1. process left postorder_print(node_ptr->left()); //2. process right postorder_print(node_ptr->right()); //3. process root std::cout << node_ptr->data() << std::endl; }}