Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan."— 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 child parent leaf

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

4 Tree Examples

5

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

9 Full Binary Tree Every leaf has the same depth

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

11 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

12 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

13 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

14 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 void preorder_print(binary_tree_node * node_ptr) { if (node_ptr != NULL) { //1. process root std::cout data() << std::endl; //2. process left preorder_print(node_ptr->left()); //3. process right preorder_print(node_ptr->right()); }

15 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 void inorder_print(binary_tree_node * node_ptr) { if (node_ptr != NULL) { //1. process left inorder_print(node_ptr->left()); //2. process root std::cout data() << std::endl; //3. process right inorder_print(node_ptr->right()); }

16 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 void postorder_print(binary_tree_node * 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 data() << std::endl; }


Download ppt "TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan."

Similar presentations


Ads by Google