Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complete Binary Trees Chapter 9 introduces trees.

Similar presentations


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

1 Complete Binary Trees Chapter 9 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 Java

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

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

4 A Binary Tree of States 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.

5 A Binary Tree of States 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.)

6 A Binary Tree of States 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.

7 A Binary Tree of States 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.

8 A Binary Tree of States 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.

9 A Binary Tree of States 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.

10 A Binary Tree of States 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.

11 A Quiz Which node has only a right child?
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...

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

13 A Binary Tree of States 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.)

14 A Binary Tree of States 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.

15 A Binary Tree of States 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).

16 A Binary Tree of States 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.

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

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

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

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

21 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?

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

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

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

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

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

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

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

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

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

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

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

33 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 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. Read Section 9.2 to see details of how the entries are stored. An array of data We don't care what's in this part of the array.

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

35 Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and sets. This presentation illustrates how another data type called a dictionary is implemented with binary search trees. This lecture shows a common application of binary trees: Binary Search Trees used to implement a Dictionary data type. Before this lecture, students should have a good understanding of binary trees, and should have seen some basic container data types similar to a dictionary (for example, a bag or a set). Data Structures and Other Objects Using Java

36 The Dictionary Data Type
A dictionary is a collection of items, similar to a bag. But unlike a bag, each item has a string attached to it, called the item's key. This lecture shows how to use a binary tree to implement an abstract data structure called a Dictionary. We"ll start by explaining what a Dictionary is, without any reference to trees. Then we will show how the trees can be used to actually implement a Dictionary. It's important to realize that trees are but one possible way to implement a Dictionary, and the actual explanation of "What is a Dictionary?" will not refer to trees at all. In other words, a Dictionary is an abstract data type, and the trees are one of the mechanisms that can be used to implement the Dictionary. So, what is a Dictionary? In many ways it is like other ADTs that you have seen, such as a bag which contains a collection of items. The difference is that each item in a Dictionary is attached to a string called the item's key.

37 The Dictionary Data Type
A dictionary is a collection of items, similar to a bag. But unlike a bag, each item has a string attached to it, called the item's key. For this example, each item that I"m putting in the Dictionary is a record which contain a bunch of geographical information about a state. Example: The items I am storing are records containing data about a state.

38 The Dictionary Data Type
A dictionary is a collection of items, similar to a bag. But unlike a bag, each item has a string attached to it, called the item's key. The key for each record is the name of the state. In general, the keys could be some other sort of value such as social security numbers. The keys must have the property that they form a total order under some comparison operation such as “less than”. Often, the keys are strings. Example: The key for each record is the name of the state. Washington

39 The Dictionary Data Type
public void insert(The key for the new item, The new item) The insertion method for a dictionary has two parameters. When an item is placed into the Dictionary, we must specify both the record of information and the key that is attached to that information. For example, if the Dictionary is implemented as an object type, then there will be an Insert method. The Insert method will have two parameters: a string (which is the key) and a record (which is the item being inserted). Washington

40 The Dictionary Data Type
When you want to retrieve an item, you specify the key... When you want to retrieve information from the Dictionary, you call a retrieval method, and specify the key of the item that you are looking for. This key is the parameter of the retrieval method. public Object retrieve("Washington")

41 The Dictionary Data Type
When you want to retrieve an item, you specify the key and the retrieval method returns the item. The Dictionary finds the information, and returns it. public Object retrieve("Washington")

42 The Dictionary Data Type
We"ll look at how a binary tree can be used as the internal storage mechanism for the dictionary. That's enough about the abstract workings of a Dictionary. Now we are going to look at how a binary tree can be used to store the information of a Dictionary in a way that makes it fairly easy to add new items, to retrieve existing items, and to remove items. (You never know when a state might want to secede from the union.)

43 A Binary Search Tree of States
Florida The data in the dictionary will be stored in a binary tree, with each node containing an item and a key. Oklahoma Colorado Arizona Arkansas Mass. Washington As you might have guessed, the data in the dictionary will be stored in a binary tree, with each node containing both a record of information and the key that's attached to that information. In this example, the Dictionary currently has only 9 of the 50 states, but that's enough to illustrate the idea. Hampshire New Virginia West

44 A Binary Search Tree of States
Florida Storage rules: Every key to the left of a node is alphabetically before the key of the node. Oklahoma Colorado Arizona Arkansas Colorado Mass. Washington The nodes cannot appear in just any order. The nodes must follow the special storage rules of a binary search tree. There are two such rules: 1. Every key to the left of a node is alphabetically before the key of the node. Hampshire New Virginia West

45 A Binary Search Tree of States
Florida Storage rules: Every key to the left of a node is alphabetically before the key of the node. Oklahoma Colorado Arizona Mass. Washington Example: " Massachusetts" and " New Hampshire" are alphabetically before "Oklahoma" Notice that this rule applies to every which can be reached by starting down the left branch of a node. For example, if I start at Oklahoma, and head down the left branch, I can reach Massachusetts and New Hampshire. So, both Massachusetts and New Hampshire must be alphabetically before Oklahoma. Hampshire New Virginia West Arkansas

46 A Binary Search Tree of States
Florida Storage rules: Every key to the left of a node is alphabetically before the key of the node. Every key to the right of a node is alphabetically after the key of the node. Oklahoma Colorado Arizona Mass. Washington The second rule is the mirror image of the first rule: 2. Every key to the right of a node is alphabetically after the key of the node. Hampshire New Virginia West Arkansas

47 A Binary Search Tree of States
Florida Storage rules: Every key to the left of a node is alphabetically before the key of the node. Every key to the right of a node is alphabetically after the key of the node. Oklahoma Colorado Arizona Mass. Washington Here's an example: Arkansas is alphabetically after Arizona. Hampshire New Virginia West Arkansas

48 Retrieving Data Start at the root.
Florida Start at the root. If the current node has the key, then stop and retrieve the data. If the current node's key is too large, move left and repeat 1-3. If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Arizona Mass. Washington Once a tree is organized according to the storage rule of a binary search tree, it is easy to find any particular key, following this algorithm. The algorithm starts at the root and repeatedly executes these steps. 1. First check to see if we found the key we were looking for. If so, then we can stop and return the associated information. 2. On the other hand, if the key at the current node is larger than the key that we"re searching for, then we"ll continue our search by moving leftward. 3. And if the key at the current node is smaller than the key that we"re searching for, then we"ll continue our search by moving rightward. Hampshire New Virginia West Arkansas

49 Retrieve " New Hampshire"
Florida Start at the root. If the current node has the key, then stop and retrieve the data. If the current node's key is too large, move left and repeat 1-3. If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Arizona Mass. Washington As an example, suppose we are searching for New Hampshire. We start at Florida, and since this is not the node we are after we must move down. Do we move left or right? Hampshire New Virginia West Arkansas

50 Retrieve "New Hampshire"
Florida Start at the root. If the current node has the key, then stop and retrieve the data. If the current node's key is too large, move left and repeat 1-3. If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Arizona Mass. Washington We have moved right from Florida, because Florida is smaller than New Hampshire. Or to be more specific: The name "Florida" is alphabetically before the name "New Hampshire". That's what we mean by 'smaller". Now we have arrived at Oklahoma. Which way next? Hampshire New Virginia West Arkansas

51 Retrieve "New Hampshire"
Florida Start at the root. If the current node has the key, then stop and retrieve the data. If the current node's key is too large, move left and repeat 1-3. If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Arizona Mass. Washington We move left from Oklahoma to Massachusetts. The leftward step was because Oklahoma is larger than the key we are looking for. Hampshire New Virginia West Arkansas

52 Retrieve "New Hampshire"
Florida Start at the root. If the current node has the key, then stop and retrieve the data. If the current node's key is too large, move left and repeat 1-3. If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Arizona Mass. Washington From Massachusetts we continue searching, and take a right step. Now we have found New Hampshire, so we can return the data from this node. Hampshire New Virginia West Arkansas

53 Adding a New Item with a Given Key
Florida Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. Oklahoma Colorado Arizona Mass. Washington Adding a new node requires two steps, the first of which is similar to searching. In this first step we pretend that we are trying to find the key. Of course, we won"t find the key, because it is not yet in the tree. So eventually we will reach a spot where there is no "next node" to step onto. At this point we stop and add the new node. Hampshire New Virginia West Arkansas

54 Adding Iowa Florida Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. Oklahoma Colorado Arizona Mass. Washington For example, suppose that Iowa wants to join our dictionary. We start by pretending to search for Iowa, beginning at the root... Hampshire New Virginia West Arkansas

55 Adding Iowa Florida Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. Oklahoma Colorado Arizona Mass. Washington Which way will we move from the root if we are searching for Iowa? Hampshire New Virginia West Arkansas

56 Adding Iowa Florida Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. Oklahoma Colorado Arizona Mass. Washington From the root we have moved right because Florida was smaller than Iowa. Hampshire New Virginia West Arkansas

57 Adding Iowa Florida Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. Oklahoma Colorado Arizona Mass. Washington From Oklahoma we move left, onto Massachusetts,... Hampshire New Virginia West Arkansas

58 Adding Iowa Florida Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. Oklahoma Colorado Arizona Mass. Washington ...and from Massachusetts we would move left again, if we could. But there is no node here. So we stop... Hampshire New Virginia West Arkansas

59 Adding Florida Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. Oklahoma Colorado Arizona Mass. Washington ...and this is the location for Iowa. Later, when we are searching for Iowa, we will follow the same path down to Massachusetts. We will step left from Massachusetts, and there we find our goal of Iowa. Important note: New nodes are always added at the leaves. Iowa Hampshire New Virginia West Arkansas

60 Adding Where would you add this state? Kazakhstan Florida Oklahoma
Colorado Arizona Mass. Washington One more example: Where would you add the new state of Kazakhstan?... Iowa Hampshire New Virginia West Arkansas

61 Adding Kazakhstan is the new right child of Iowa? Florida Oklahoma
Colorado Arizona Mass. Washington If you followed the algorithm, then you saw that Kazakhstan is added as the right child of Iowa. Iowa Hampshire New Virginia West Arkansas Kazakhstan

62 Removing an Item with a Given Key
Florida Find the item. If necessary, swap the item with one that is easier to remove. Remove the item. Oklahoma Colorado Arizona Mass. Washington Removing an item requires three steps. We can look at the steps in detail in a moment, but first let's go through this outline so that you know roughly what to expect. 1. Find the item that you want to remove. 2. Some items are harder to remove than others. In particular, an item with two children is particularly hard to remove. So, in this second step we sometimes take an item that is hard to remove, and exchange it with an item that is easier to remove. 3. Finally, once the item is in a spot that is easy to remove, we remove it. Let's look at the three steps in detail. Iowa Hampshire New Virginia West Arkansas Kazakhstan

63 Removing "Florida" Find the item. Florida Oklahoma Colorado Arizona
Mass. Washington As an example, let's remove Florida. (It's way too hot to be part of the union anyway). First we find it, which is easy enough by using the usual method to search for a key. Iowa Hampshire New Virginia West Arkansas Kazakhstan

64 Removing "Florida" Florida cannot be removed at the moment... Florida
Oklahoma Colorado Arizona Mass. Florida cannot be removed at the moment... Washington In the second step we need to swap Florida with another item that is easier to remove. The reason that we need to do this swap is because of a problem that occurs if we just try to remove Florida... Iowa Hampshire New Virginia West Arkansas Kazakhstan

65 Removing "Florida" ... because removing Florida would
Oklahoma Colorado Arizona Mass. ... because removing Florida would break the tree into two pieces. Washington We end up breaking the tree into two separate parts. Iowa Hampshire New Virginia West Arkansas Kazakhstan

66 Removing "Florida" If necessary, do some rearranging. The problem of
Oklahoma Colorado Arizona Mass. The problem of breaking the tree happens because Florida has 2 children. Washington In general it is hard to recombine these two parts into a single tree. So, our goal is to find another item that is easier to remove, and copy that other item spot that we are trying to remove. Iowa Hampshire New Virginia West Arkansas Kazakhstan

67 Removing "Florida" If necessary, do some rearranging.
Oklahoma Colorado Arizona Mass. For the rearranging, take the smallest item in the right subtree... Washington In general, there are two items that we could copy on top of Florida. In the book you"ll see that one of these items is in the left subtree. In this lecture I"ll use the other one, from the right subtree. In an actual program you can follow either approach. Anyway, the approach we"ll take here is to copy the smallest item in the right subtree onto Florida. To find that smallest item, step onto the right subtree (Oklahoma), and then race as far left as you can -- onto Iowa. Iowa Hampshire New Virginia West Arkansas Kazakhstan

68 Removing "Florida" If necessary, do some rearranging.
Iowa If necessary, do some rearranging. Oklahoma Colorado Arizona Mass. ...copy that smallest item onto the item that we"re removing... Washington Copy Iowa on top of the item that we are removing. Iowa Hampshire New Virginia West Arkansas Kazakhstan

69 Removing "Florida" If necessary, do some rearranging.
Iowa If necessary, do some rearranging. Oklahoma Colorado Arizona Mass. ... and then remove the extra copy of the item we copied... Washington We have eliminated Florida, but now we have two copies of Iowa. If you"ve ever been to Iowa, you know that one Iowa is more than enough, so next we must remove the extra Iowa... Hampshire New Virginia West Arkansas Kazakhstan

70 Removing "Florida" If necessary, do some rearranging.
Iowa If necessary, do some rearranging. Oklahoma Colorado Arizona Mass. Washington Note that the extra Iowa had one child (Kazakhstan), so that child is reconnected to the parent. Here's a good question for you: Remember that it's hard to remove nodes with two children. How do you know that the smallest item in the right subtree does not have two children? Answer: Since it is the smallest item, it can"t have a left child because that left child would be even smaller. ... and reconnect the tree Kazakhstan Hampshire New Virginia West Arkansas

71 Removing "Florida" Why did I choose the smallest item
Oklahoma Colorado Arizona Mass. Why did I choose the smallest item in the right subtree? Washington In fact, the guarantee that the smallest item has at most one child is one of the reasons why I selected the smallest item. There's a second reason, can you think of it? Hampshire New Virginia West Arkansas Kazakhstan

72 Removing "Florida" Because every key must be smaller than
Iowa Oklahoma Colorado Arizona Mass. Because every key must be smaller than the keys in its right subtree Washington The second reason is that I am take this smallest item and place it in Florida's location. In order to maintain the binary search tree storage rules, this item must be smaller than anything that remains in the right subtree--therefore I must choose the smallest item in the right subtree. Kazakhstan Hampshire New Virginia West Arkansas

73 Removing an Item with a Given Key
Find the item. If the item has a right child, rearrange the tree: Find smallest item in the right subtree Copy that smallest item onto the one that you want to remove Remove the extra copy of the smallest item (making sure that you keep the tree connected) else just remove the item. Here's a summary of the removal steps. Note that if the item that we want to remove does not have a right child, then we can just remove it (and reconnect its left child if there is one). In the text book, the process is done in a symmetrical way--using the largest item in the left subtree. Either approach works fine. Also, the textbook implements a Bag rather than a Dictionary. One of the resultant differences is that the Bag may have several copies of the same item, so that items in the left subtree can be less than or equal to the item in a node.

74 Summary Binary search trees are a good implementation of data types such as sets, bags, and dictionaries. Searching for an item is generally quick since you move from the root to the item, without looking at many other items. Adding and deleting items is also quick. But as you’ll see later, it is possible for the quickness to fail in some cases -- can you see why? A quick summary . . .


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

Similar presentations


Ads by Google