Data Structures inary Trees number of leaves:?? height leaves Depth (Maximum level of the tree) leaves or nodes Root Level = 0 Level=1 57 feet root 2 Level=2 Number of nodes: 2 (2+1) - 1 = 7 2
inary Trees Levels - Nodes Max. number of leaves/nodes: 2 (d+1) - 1 where d = depth (assume the tree is full (complete)) The level of a node is its distance from the root. The depth of a tree is the level of the leaf in the longest path from the root plus one. strictly binary tree: every non-leaf node has nonempty left & right sub trees IS265/506: hapter 08 Intro to inary Trees 3 inary Trees Levels - Nodes complete binary tree of depth d has all leaves at level d n almost complete binary tree of depth d is such that: 1. ny node at a level less than (d-1) has 2 sons 2. The bottom level at height d is partially filled from left to right, and may only have from 1 to 2 d nodes. This may make some of the nodes at level d-1 into leaf nodes. So a binary tree of height d may have anywhere from 2 d to 2 d+1-1 nodes. tree can be strictly binary, but not almost complete onversely, a tree can be almost complete, but not strictly binary. IS265/506: hapter 08 Intro to inary Trees 4
inary Trees Levels Nodes tree can be strictly binary, but not almost complete onversely, a tree can be almost complete, but not strictly binary. IS265/506: hapter 08 Intro to inary Trees 5 inary Trees binary tree is a tree whose nodes have up to two children (possibly empty). Each circle represents a node. The circle right below it (and attached by the arrow) is the child (or leaf). Each child is either the left or right child. Each child can have only one parent, and each parent can have only two children. Each path (from node to node) is a straight line. There are no cycles in binary trees. IS265/506: hapter 08 Intro to inary Trees 6
inary Trees This is a tree, but not a binary tree. There are 2 nodes that have three children. IS265/506: hapter 08 Intro to inary Trees 7 re these binary trees? Why or why not?? IS265/506: hapter 08 Intro to inary Trees 8
inary Trees Why do care for binary trees? Improve speed when searching a long list Trees are naturally ordered - information is placed in a logical location so it can be found easier. 1 2 3 4 5 1 3 2 4 5 IS265/506: hapter 08 Intro to inary Trees 9 Implementing a inary Tree Trees can be implemented as either arrays or dynamic structures, similar to linked lists. We will only look at the dynamic implementation - the array implementation can be difficult to manage. left info right Left & right are pointers to nodes (children) in the tree. Info is where your data are stored. IS265/506: hapter 08 Intro to inary Trees 10
onceptual View of a Tree data *left *right data data *left *right *left *right data data data data *left *right *left *right *left *right *left *right IS265/506: hapter 08 Intro to inary Trees 11 inary Trees - Ordering Trees can be ordered in many ways. The most common are: Hierarchical: Highest to lowest. Example - university has multiple campuses, which have multiple colleges, which have many departments, etc. hronological: The order in the tree is determined by when the item was placed in the tree. Search Trees (lternately called ordered trees): formal definition: if a node n of the tree contains the value v, then all the values stored in the left subtree are less than v, and all the values in the right subtree are greater than v. Duplicate values are not allowed. IS265/506: hapter 08 Intro to inary Trees 12
Traversing a tree Three important ways to visit every node of a tree. INORDER (left, root, right) Inorder visits the left child, root, then right child PREORDER (root, left, right) Preorder visits the root, the left child, and then the right child POSTORDER (left, right, root) Postorder visits the left child, the right child, and finally the root. IS265/506: hapter 08 Intro to inary Trees 13 Tree Traversal The mechanics of visiting nodes could be accomplished by either: an explicitly navigation (I will do everything by myself approach), or using recursion (let the system help me). The explicit method requires you to define and use a stack - all data points are pushed on the stack as we visit them from top-to-bottom and/or left-to-right, and we then pop each element from the stack to view the data. This could be quite cumbersome. IS265/506: hapter 08 Intro to inary Trees 14
Sample Tree Structure Tree ( pointer to the root) Left Sub Tree Right Sub Tree D E Top-to-ottom (Depth-First Search) Left-to-Right (reath-first Search),, D, E,,,, D, E Examining One Node info tree tree.getleft() Tree.getRight() IS265/506: hapter 08 Intro to inary Trees 16
Tree Traversal n elegant visitation method is based on recursion. This requires a bit of extra effort on your part when debugging, but the code is much shorter and simpler to write. The algorithm for INORDER Traversals: ssume the tree exists Visit the left sub-tree Visit the root Visit the right sub-tree IS265/506: hapter 08 Intro to inary Trees 17 Let s Look at the ode // Inorder visits: // the left child, root, then right child void inordertraversal(nodeptr tree) { if (tree!= NULL) { inordertraversal(tree.getleft()); System.out.println(tree.getInfo()); inordertraversal(tree.getright()); } } IS265/506: hapter 08 Intro to inary Trees 18
Simple ase - Inorder blue red black Is the tree NULL? No, it does exist inordertraversal( tree.getleft ) - Visit the red () node: We call the procedure again using the red () node as the tree. There is no left node of red (), so we print the root. Now, there is no right node of red (), so we exit the recursive call. System.out.println( tree.getinfo ); - We print the blue () node. IS265/506: hapter 08 Intro to inary Trees 19 Simple ase (ontinued) blue red black inordertraversal( tree.getright ) - Visit the black () node: We call the procedure again using the black () node as the tree. There is no left node of black (), so we print the root. Now, there is no right node of black (), so we exit the recursive call. /* end if */ - We are done. Our output is: red () blue () black () IS265/506: hapter 08 Intro to inary Trees 20
Let s do one that is more difficult E D F D E F IS265/506: hapter 08 Intro to inary Trees 21 PreOrder Traversal Preorder visits the root, the left child, and then the right child The algorithm for PREORDER Traversals: ssume the tree exists Visit the root Visit the left subtree Visit the right subtree IS265/506: hapter 08 Intro to inary Trees 22
Let s Look at the ode // Preorder visits // the root, left child, then right child void preordertraversal(nodeptr tree) { if (tree!= NULL) { System.out.println( tree.getinfo() ); preordertraversal( tree.getleft() ); preordertraversal( tree.getright() ); } } IS265/506: hapter 08 Intro to inary Trees 23 Very Simple ase blue red black Is the tree NULL? No, it does exist System.out.println(tree.getInfo); - We print the blue () node. preordertraversal(tree.getleft) - Visit the red () node: We call the procedure again using the red () node as the tree. We print the root (red () ) and note that there is no left node of red () or no right node of red (), so we exit the recursive call. IS265/506: hapter 08 Intro to inary Trees 24
Very Simple ase (ontinued) blue red black preordertraversal(tree.getright) - Visit the black () node: We call the procedure again using the black () node as the tree. We print the root (black () ) and note that there is no left node of black () or no right node of black (), so we exit the recursive call. /* end if */ - We are done. Our output is: blue () red () black () IS265/506: hapter 08 Intro to inary Trees 25 Let s do one that is more difficult E D F E D F IS265/506: hapter 08 Intro to inary Trees 26
PostOrder Traversal Postorder visits the the left child, the right child, then the root The algorithm for POSTORDER Traversals: ssume the tree exists Visit the left subtree Visit the right subtree Visit the root IS265/506: hapter 08 Intro to inary Trees 27 Let s Look at the ode // Postorder visits // left child, right child, then the root void postordertraversal(nodeptr tree) { if (tree!= NULL) { postordertraversal( tree.getleft() ); postordertraversal( tree.getright() ); System.out.println( tree.getinfo() ); } } IS265/506: hapter 08 Intro to inary Trees 28
Very Simple ase blue red black Is the tree NULL? No, it does exist postordertraversal(tree.getleft) - Visit the red () node: We call the procedure again using the red () node as the tree. We note that there is no left node of red () or no right node of red, so we print the root (red () ) and exit the recursive call. IS265/506: hapter 08 Intro to inary Trees 29 Very Simple ase (ontinued) blue red black postordertraversal(tree.getright) - Visit the black () node: We call the procedure again using the black () node as the tree. We note that there is no left node of black () or no right node of black (), so we print the root (black () ) and exit the recursive call. System.out.println(tree.getInfo); - We print the blue() node. /* end if */ - We are done. Our output is: red () black () blue () IS265/506: hapter 08 Intro to inary Trees 30
Let s do one that is more difficult E D F D F E IS265/506: hapter 08 Intro to inary Trees 31