Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complete Binary Trees Chapter 10 introduces trees.

Similar presentations


Presentation on theme: "Complete Binary Trees Chapter 10 introduces trees."— Presentation transcript:

1 Complete Binary Trees Chapter 10 introduces trees.
This presentation illustrates the simplest kind of trees: Complete Binary Trees. This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing on complete binary Before this lecture, students should have a good understanding of the simpler linear data structures of lists, stacks, and queues. Data Structures and Other Objects Using C++

2 Trees First example of nonlinear structure
Do not form a simple sequence of first entry, second entry, …… More complex linking between the components

3 Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. But it is the connections between the nodes which characterize a binary tree. In many ways, a tree is like the other structures you have seen: A tree consists of nodes, and each node can contain data of one sort or another.

4 Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. But it is the connections between the nodes which characterize a binary tree. But, unlike a linked list, the connections between the nodes are more than a simple one-to-another progression. An example can illustrate the connections in a binary tree. An example can illustrate how the connections work

5 A Binary Tree of States Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona In this example, the data contained at each node is one of the 50 states. This is an example of a binary tree with nine nodes. Presumably each node contains information about one of the 50 states. In this example, the states are not arranged in any particular order, except insofar as I need to illustrate the different special kinds of nodes and connections in a binary tree.

6 A Binary Tree of States Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Each tree has a special node called its root, usually drawn at the top. The first special kind of node is the root. Computer scientists always draw this special node at the top. (I think they must have flunked botany.)

7 A Binary Tree of States Arizona Oklahoma Washington Arkansas Colorado Florida Massachusetts Washington Each tree has a special node called its root, usually drawn at the top. The example tree has Washington as its root. In this example, Washington State is at the root. An interesting true story about Washington State: When Washington first applied for statehood, congress was considering several possible names for the new state, including Columbia since the major river in the state is the Columbia river. But congress was worried that people might get Columbia mixed up with the District of Columbia...so they decided to call it Washington instead.

8 A Binary Tree of States Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Each node is permitted to have two links to other nodes, called the left child and the right child. Each node in a binary tree is permitted to have two links downward to other nodes, called the left child and the right child.

9 A Binary Tree of States Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Each node is permitted to have two links to other nodes, called the left child and the right child. The fact that we allow two links to children is the reason that these trees are called binary trees. "Binary" means "two parts". Later you will see more general trees where the number of children is not restricted to two.

10 A Binary Tree of States Children are usually drawn below a node.
Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Children are usually drawn below a node. The right child of Washington is Colorado. The left child of Washington is Arkansas. But for binary trees, each node has at most two children. For example, the left child of Washington is Arkansas. The right child of Washington is Colorado.

11 A Binary Tree of States Some nodes have only one child. Arkansas has a
Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Some nodes have only one child. Arkansas has a left child, but no right child. In a binary tree, a node might have a left child, but no right child. For example, the left child of Arkansas is Florida. But Arkansas has no right child.

12 A Quiz Which node has only a right child?
Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Some nodes have only one child. Which node has only a right child? Here is a quiz: Find a node that has a right child, but no left child...

13 A Quiz Some nodes have only one child. Florida has only a right child.
Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Some nodes have only one child. Florida has only a right child. In this example, Massachusetts is the right child of Florida, and Florida has no left child.

14 A Binary Tree of States A node with no children is called a leaf.
Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona A node with no children is called a leaf. Some nodes have no children, and those nodes are called leaves. In this example, there are four leaves: Massachusetts, Oklahoma, New Hampshire (or is that Vermont?) and Nebraska. (Yes, that really is Nebraska. Either the author ran out of room on the slide and had to shrink it, or the author is from rival state Colorado.)

15 A Binary Tree of States Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Each node is called the parent of its children. Washington is the parent of Arkansas and Colorado. We can also refer to the parent of a node, which is the upward link. For example, Washington is the parent of both Arkansas and Colorado.

16 A Binary Tree of States Two rules about parents:
Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Two rules about parents: The root has no parent. Every other node has exactly one parent. There are two rules about parents in any tree: 1. The root never has a parent. 2. Every other node has exactly one parent. There is also a related rule which is not written here, but is part of the definition of a tree: If you start at the root, there is always one way to get from the root to any particular node by following a sequence of downward links (from a parent to its child).

17 A Binary Tree of States Washington Arkansas Colorado Florida Massachusetts Oklahoma Arizona Two nodes with the same parent are called siblings. Arkansas and Colorado are siblings. Two nodes that have the same parent are called siblings, as shown here. In a binary tree, a node has at most one sibling.

18 Binary Trees Terminologies
Node Parent Children: left and right child Sibling: have the same parent Leaf: a node with no children Ancestor First ancestor: the node’s parent Next ancestor: the parent of the parent Descendant First descendant: the node’s children Next descendant: the children’s children Subtree: left and right subtree Depth of a node: the number of steps moving up to the parent Root node is at 0 depth

19 Binary Trees Terminologies
Depth of a tree The maximum depth of any of its leaves Full binary tree Every leaf has the same depth, and every non-leaf has two children Complete binary tree Every level except the deepest must contain as many nodes as possible At the deepest level, all the nodes are as far left as possible

20 Complete Binary Trees A complete binary tree is a special kind of binary tree which will be useful to us. The rest of this lecture demonstrates a special kind of binary tree called a complete binary tree. The tree has several applications, and is also special because it is extremely easy to implement.

21 Complete Binary Trees A complete binary tree is a special kind of binary tree which will be useful to us. When a complete binary tree is built, its nodes are generally added one at a time. As with any tree, the first node must be the root. When a complete binary tree is built, its first node must be the root.

22 Complete Binary Trees The second node of a complete binary tree is always the left child of the root... With a complete binary tree, the second node must be the left child of the root.

23 Complete Binary Trees The second node of a complete binary tree is always the left child of the root... ... and the third node is always the right child of the root. The next node must be the right child of the root.

24 Complete Binary Trees The next nodes must always fill the next level from left to right. And so we continue, adding nodes. But in a complete binary tree, the way that we add nodes is restricted: Nodes must completely fill each level from left-to-right before proceeding to the next level. Following this rule, where would the fifth node have to be placed?

25 Complete Binary Trees The next nodes must always fill the next level from left to right. The fifth node must go in the next available spot in this level, which is the right child of Arkansas. By the way, there is also an interesting story about Idaho. Apparently congress decreed that the border between Idaho and Montana was to be the continental divide. So the surveyers started at Yellowstone Park, mapping out the border, but at “Lost Trail Pass” they made a mistake and took a false divide. They continued along the false divide for several hundred miles until they crossed over a ridge and saw the Clark Fork River cutting right across what they thought was the divide. At this point, they just gave up and took the rest of the border straight north to Canada. So those of you who come from Kalispell and Missoula Montana should really be Idahonians.

26 Complete Binary Trees The next nodes must always fill the next level from left to right. Then we can add the left child of Colorado...

27 Complete Binary Trees The next nodes must always fill the next level from left to right. ...and the right child of Colorado. Where would the next node after this go?...

28 Complete Binary Trees The next nodes must always fill the next level from left to right. ...now we can move to the next level, and add the first node as the left child of Florida.

29 Complete Binary Trees The next nodes must always fill the next level from left to right. Then the right child of Florida.

30 Is This Complete? Just to check your understanding, is this binary tree a complete binary tree? No, it isn't since the nodes of the bottom level have not been added from left to right.

31 Is This Complete? Is this complete?
No, since the third level was not completed before starting the fourth level.

32 Is This Complete? This is also not complete since Washington has a right child but no left child.

33 Is This Complete? But this binary tree is complete. It has only one node, the root.

34 Is This Complete? Yes! It is called the empty tree, and it has no nodes, not even a root. This binary tree is also complete, although you might have to squint to see it. You see, this binary tree has no nodes at all. It is called the empty tree, and it is considered to be a complete binary tree.

35 Binary Trees A binary tree is a finite set of nodes. The set might be empty. But if the set is not empty, it follows these rules: There is one special node, called the root. Each node may be associated with up to two other different nodes. Each node, except the root, has exactly one parent; the root has no parent If you start at a node and move to the node’s parent, then move again to that node’s parent, and keep moving upward to each node’s parent, you will eventually reach the root.

36 Implementing a Complete Binary Tree
We will store the date from the nodes in a partially-filled array. An integer to keep track of how many nodes are in the tree 3 There is one last feature of complete binary trees that you need to learn: How to implement a complete binary tree using a partially-filled array. With this implementation, the data from each node is placed in one component of a partially-filled array. The primary thing we need to keep track of is where in the array does each node belong? An array of data We don't care what's in this part of the array.

37 Implementing a Complete Binary Tree
The solution to this problem is in Section 9.2 of the text, which discusses several different ways to implement binary trees. The simplest implementation uses a partially-filled array, as suggested by this picture, but you should be warned: The array implementation is only practical for complete binary trees. R

38 Implementing a Complete Binary Tree

39 Implementing a Complete Binary Tree

40 Implementing a Complete Binary Tree
R E C

41 Implementing a Complete Binary Tree
R E C U

42 Implementing a Complete Binary Tree
R E C U R

43 Implementing a Complete Binary Tree
R E C U R S

44 Implementing a Complete Binary Tree
R E C U R S I

45 Implementing a Complete Binary Tree
R E C U R S I O N

46 Implementing a Complete Binary Tree
Properties: Root node is at index 0 The parent of a node at index i is at location (i - 1)/2 The children of a node at index i are at 2 * i + 1 and 2 * i + 2 * If those indices are out of bounds, then the nodes does not have one or both of those children R E C U R S I O N

47 Implementing a Complete Binary Tree using Nodes
The node has: Data element Left link Right link Tree Operations: Tree size Tree clear Tree copy In-order: Pre-order: Post-order:

48 Animal Guess Game

49 Animal Guess Game Exam the code:
Useful functions void eat_line( ) bool inquire(const char query[ ]) void instruct( ) Postcondition: Instructions for playing the game have been printed to the screen. binary_tree_node<string>* beginning_tree( ) Postcondition: The function has created a small binary tree. The return value is the root pointer of the new tree. void play(binary_tree_node<string>* current_ptr) Precondition: current_ptr points to the root of a binary tree with at least two leaves. Postcondition: One round of the animal game has been played, and maybe the tree has been improved. void ask_and_move(binary_tree_node<string>*& current_ptr) Precondition: current_ptr points to a non-leaf node in a binary tree. Postcondition: The question at the current node has been asked. The current pointer has been shifted left (if the user answered yes) or right (for a no answer). void learn(binary_tree_node<string>*& leaf_ptr) Precondition: leaf_ptr is a pointer to a leaf in a binary tree. The leaf contains a wrong guess that was just made. Postcondition: Information has been elicited from the user, and the tree has been improved.

50 Implementing a Complete Binary Tree using Nodes
Tree traversals In-order Pre-order Post-order Do not want to rewrite traversal for every usage Pass in usage (function) as an argument to traversal function 7 5 6 3 2 8 9 4 In-order: Pre-order: Post-order: template <class BTNode> void inorder(BTNode* node_ptr) { if (node_ptr != NULL) inorder(node_ptr->left( )); cout << node_ptr->data( ) ); inorder(node_ptr->right( )); }

51 Function Parameters Parameter to a function may be a function
void apply(void f(int&), int data[], size_t n) Precondition: data is an array with at least n components Post-condition: the function f has been applied to the n components of the data array

52 Function Parameters void apply (void f(int&), int data[], size_t n) { for (size_t i = 0; i < n; ++i) f(data[i]); } void seven_up(int & i){ i += 7; apply(seven_up, numbers, 10);

53 Template Function Parameters
Template<class Item, class SizeType> void apply (void f(Item&), Item data[], SizeType n) { for (size_t i = 0; i < n; ++i) f(data[i]); } void convert_to_upper(char & c); apply(convert_to_upper, names, 10);

54 Further Generalization
Template<class Process, class Item, class SizeType> void apply (Process f, Item data[], SizeType n) { for (size_t i = 0; i < n; ++i) f(data[i]); } void triple(int & i); void print(const string & s); apply(triple, numbers, 10); Apply(print, words, 10);

55 Template Function for Tree Traversals
Template<class Process, class BTNode> void preorder (Process f, BTNOde* node_ptr) { if (node_ptr != NULL) { f (node_ptr->data()); preorder(f, node_ptr->left()); preorder(f, node_ptr->right()); }

56 Summary Binary trees contain nodes.
Each node may have a left child and a right child. If you start from any node and move upward, you will eventually reach the root. Every node except the root has one parent. The root has no parent. Complete binary trees require the nodes to fill in each level from left-to-right before starting the next level. A quick summary . . .


Download ppt "Complete Binary Trees Chapter 10 introduces trees."

Similar presentations


Ads by Google