Download presentation
Presentation is loading. Please wait.
1
Data Structures Binary Trees 1
2
(Maximum level of the tree) leaves or nodes Level=1
Root Level = 0 number of leaves: ?? leaves Depth (Maximum level of the tree) leaves or nodes Level=1 height Level=2 root 2 57 feet Number of nodes: 2(2+1) - 1 = 7 CIS265/506: Chapter 08 Intro to Binary Trees 2
3
CIS265/506: Chapter 08 Intro to Binary Trees
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. A strictly binary tree: every non-leaf node has nonempty left & right sub trees CIS265/506: Chapter 08 Intro to Binary Trees
4
CIS265/506: Chapter 08 Intro to Binary Trees
Trees – Levels - Nodes A complete binary tree of depth d has all leaves at level d An almost complete binary tree of depth d is such that: Any node at a level less than (d-1) has 2 sons For any node with a right descendant at level d, it must have a left son and every left descendant is either a leaf at level d or has 2 sons. A tree can be strictly binary, but not almost complete Conversely, a tree can be almost complete, but not strictly binary. CIS265/506: Chapter 08 Intro to Binary Trees
5
CIS265/506: Chapter 08 Intro to Binary Trees
A binary tree is a tree whose nodes have children (possibly empty) and each child is either a left child or a right child. Each circle is 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. CIS265/506: Chapter 08 Intro to Binary Trees 3
6
CIS265/506: Chapter 08 Intro to Binary Trees
This is a tree, but not a binary tree. There are 2 nodes that have three children. CIS265/506: Chapter 08 Intro to Binary Trees 4
7
Are these binary trees? Why or why not??
CIS265/506: Chapter 08 Intro to Binary Trees 5
8
CIS265/506: Chapter 08 Intro to Binary Trees
Why do we have 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 3 4 2 5 1 CIS265/506: Chapter 08 Intro to Binary Trees 6
9
Implementing a Binary 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. CIS265/506: Chapter 08 Intro to Binary Trees 7
10
Conceptual 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 CIS265/506: Chapter 08 Intro to Binary Trees
11
Binary Trees - Ordering
Trees can be ordered in many ways. The most common are: Hierarchical: Highest to lowest. Example - A university has multiple campuses, which have multiple colleges, which have many departments, etc. Chronological: The order in the tree is determined by when the item was placed in the tree. Search Trees (Alternately called ordered trees): A 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. CIS265/506: Chapter 08 Intro to Binary Trees 2
12
CIS265/506: Chapter 08 Intro to Binary Trees
Traversing a tree Three important ways to visit every node of a tree. INORDER Inorder visits the left child, root, then right child PREORDER Preorder visits the root, the left child, and then the right child POSTORDER Postorder visits the left child, the right child, and finally the root. CIS265/506: Chapter 08 Intro to Binary Trees 12
13
CIS265/506: Chapter 08 Intro to Binary Trees
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, and we then pop each element from the stack to view the data. This could be tedious and expensive in terms of time & space. It also requires the additional data structure of at least one generic stack to do any tree manipulations. CIS265/506: Chapter 08 Intro to Binary Trees 13
14
CIS265/506: Chapter 08 Intro to Binary Trees
Sample Tree Structure Tree (A pointer to the root) Left Sub Tree Right Sub Tree CIS265/506: Chapter 08 Intro to Binary Trees
15
CIS265/506: Chapter 08 Intro to Binary Trees
Examining One Node tree info tree left tree right CIS265/506: Chapter 08 Intro to Binary Trees
16
CIS265/506: Chapter 08 Intro to Binary Trees
Tree Traversal The other (more 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: Assume the tree exists Visit the left subtree Visit the root Visit the right subtree CIS265/506: Chapter 08 Intro to Binary Trees 14
17
CIS265/506: Chapter 08 Intro to Binary Trees
Let’s Look at the Code // Inorder visits: // the left child, root, then right child void inOrderTraversal(NodePtr tree) { if (tree != NULL) { inOrderTraversal(tree->left); System.out.println(tree->info); inOrderTraversal(tree->right); } } CIS265/506: Chapter 08 Intro to Binary Trees 15
18
CIS265/506: Chapter 08 Intro to Binary Trees
A Very Simple Case B blue A C red black Is the tree NULL? No, it does exist “inOrderTraversal(tree->left)” - Visit the red (A) node: We call the procedure again using the red (A) node as the tree. There is no left node of red (A) , so we print the root. Now, there is no right node of red (A) , so we exit the recursive call. “System.out.println(tree->info);” - We print the blue (B) node. CIS265/506: Chapter 08 Intro to Binary Trees 16
19
A Simple Case (Continued)
B blue A C red black “inOrderTraversal(tree->right)” - Visit the black (C) node: We call the procedure again using the black (C) node as the tree. There is no left node of black (C) , so we print the root. Now, there is no right node of black (C) , so we exit the recursive call. “/* end if */” - We are done. Our output is: red (A) blue (B) black (C) CIS265/506: Chapter 08 Intro to Binary Trees 17
20
Let’s do one that is more difficult
B E A D F A B C D E F CIS265/506: Chapter 08 Intro to Binary Trees 18
21
CIS265/506: Chapter 08 Intro to Binary Trees
PreOrder Traversal Preorder visits the root, the left child, and then the right child The algorithm for PREORDER Traversals: Assume the tree exists Visit the root Visit the left subtree Visit the right subtree CIS265/506: Chapter 08 Intro to Binary Trees 19
22
CIS265/506: Chapter 08 Intro to Binary Trees
Let’s Look at the Code // Preorder visits // the root, left child, then right child void preOrderTraversal(NODEPTR tree) { if (tree != NULL) { System.out.println(tree->info); preOrderTraversal(tree->left); preOrderTraversal(tree->right); } } CIS265/506: Chapter 08 Intro to Binary Trees 20
23
CIS265/506: Chapter 08 Intro to Binary Trees
A Very Simple Case B blue A C red black Is the tree NULL? No, it does exist “System.out.println(tree->info);” - We print the blue (B) node. “preOrderTraversal(tree->left)” - Visit the red (A) node: We call the procedure again using the red (A) node as the tree. We print the root (red (A) ) and note that there is no left node of red (A) or no right node of red (A), so we exit the recursive call. CIS265/506: Chapter 08 Intro to Binary Trees 21
24
A Very Simple Case (Continued)
B blue A C red black “preOrderTraversal(tree->right)” - Visit the black (C) node: We call the procedure again using the black (C) node as the tree. We print the root (black (C) ) and note that there is no left node of black (C) or no right node of black (C), so we exit the recursive call. “/* end if */” - We are done. Our output is: blue (B) red (A) black (C) CIS265/506: Chapter 08 Intro to Binary Trees 22
25
CIS265/506: Chapter 08 Intro to Binary Trees
Let’s do one that is more difficult C B E A D F C B A E D F CIS265/506: Chapter 08 Intro to Binary Trees 23
26
CIS265/506: Chapter 08 Intro to Binary Trees
PostOrder Traversal Postorder visits the the left child, the right child, then the root The algorithm for POSTORDER Traversals: Assume the tree exists Visit the left subtree Visit the right subtree Visit the root CIS265/506: Chapter 08 Intro to Binary Trees 24
27
CIS265/506: Chapter 08 Intro to Binary Trees
Let’s Look at the Code // Postorder visits // left child, right child, then the root void postOrderTraversal(NODEPTR tree) { if (tree != NULL) { postOrderTraversal(tree->left); postOrderTraversal(tree->right); System.out.println(tree->info); } } CIS265/506: Chapter 08 Intro to Binary Trees 25
28
CIS265/506: Chapter 08 Intro to Binary Trees
A Very Simple Case B blue A C red black Is the tree NULL? No, it does exist “postOrderTraversal(tree->left)” - Visit the red (A) node: We call the procedure again using the red (A) node as the tree. We note that there is no left node of red (A) or no right node of red, so we print the root (red (A) ) and exit the recursive call. CIS265/506: Chapter 08 Intro to Binary Trees 26
29
CIS265/506: Chapter 08 Intro to Binary Trees
A Very Simple Case (Continued) B blue A C red black “postOrderTraversal(tree->right)” - Visit the black (C) node: We call the procedure again using the black (C) node as the tree. We note that there is no left node of black (C) or no right node of black (C), so we print the root (black (C) ) and exit the recursive call. “System.out.println(tree->info);” - We print the blue(B) node. “/* end if */” - We are done. Our output is: red (A) black (C) blue (B) CIS265/506: Chapter 08 Intro to Binary Trees 27
30
CIS265/506: Chapter 08 Intro to Binary Trees
Let’s do one that is more difficult C B E A D F A B D F E C CIS265/506: Chapter 08 Intro to Binary Trees 28
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.