1 / 16

Trees

Trees. A tree's a tree. How many more do you need to look at? --Ronald Reagan. 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. parent. leaf. child. leaf. Tree Example.

kiley
Download Presentation

Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees A tree's a tree. How many more do you need to look at? --Ronald Reagan

  2. 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 parent leaf child leaf

  3. Tree Example Root node Node with 2 children Node with right child Leaf node Node with left child

  4. Tree Examples

  5. Tree Examples

  6. 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

  7. 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

  8. Full Binary Tree • Every leaf has the same depth

  9. Complete Binary Tree • All new leaves of a full binary tree are added from the left. • Add left-most nodes first

  10. 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

  11. 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

  12. 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

  13. 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 recursively template <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()); } }

  14. 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 recursively template <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()); } }

  15. 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 root template <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; } }

More Related