Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an...

35
1 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. Chapter 10 introduces trees . This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures and Other Objects Using C++

Transcript of Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an...

Page 1: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

1

This lecture is an introduction to trees, illustrating basic terminology forbinary trees, and focusing on complete binary

Before this lecture, students should have a good understanding of thesimpler linear data structures of lists, stacks, and queues.

❐ Chapter 10 introduces trees.❐ This presentation illustrates the

simplest kind of trees: CompleteBinary Trees.

Complete Binary Trees

Data Structuresand Other ObjectsUsing C++

Page 2: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

2

In many ways, a tree is like the other structures you have seen: A treeconsists of nodes, and each node can contain data of one sort oranother.

Binary Trees

❐ A binary tree has nodes, similar to nodes in alinked list structure.

❐ Data of one sort or another may be stored at eachnode.

❐ But it is the connections between the nodes whichcharacterize a binary tree.

Page 3: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

3

But, unlike a linked list, the connections between the nodes are morethan a simple one-to-another progression. An example can illustrate theconnections in a binary tree.

Binary Trees

❐ A binary tree has nodes, similar to nodes in alinked list structure.

❐ Data of one sort or another may be stored at eachnode.

❐ But it is the connections between the nodes whichcharacterize a binary tree.

An example canillustrate how theconnections work

Page 4: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

4

This is an example of a binary tree with nine nodes. Presumably eachnode contains information about one of the 50 states. In this example,the states are not arranged in any particular order, except insofar as Ineed to illustrate the different special kinds of nodes and connections ina binary tree.

A Binary Tree of States

In this example,the datacontained ateach node isone of the 50states.

Page 5: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

5

The first special kind of node is the root. Computer scientists alwaysdraw this special node at the top. (I think they must have flunkedbotany.)

A Binary Tree of States

Each tree has aspecial nodecalled its root,usually drawnat the top.

Page 6: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

6

In this example, Washington State is at the root.

An interesting true story about Washington State: When Washingtonfirst applied for statehood, congress was considering several possiblenames for the new state, including Columbia since the major river in thestate is the Columbia river. But congress was worried that people mightget Columbia mixed up with the District of Columbia...so they decidedto call it Washington instead.

A Binary Tree of States

Each tree has aspecial nodecalled its root,usually drawnat the top. The example tree

has Washingtonas its root.

Page 7: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

7

Each node in a binary tree is permitted to have two links downward toother nodes, called the left child and the right child.

A Binary Tree of States

Each node ispermitted tohave two linksto other nodes,called the leftchild and theright child .

Page 8: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

8

The fact that we allow two links to children is the reason that thesetrees are called binary trees. "Binary" means "two parts".

Later you will see more general trees where the number of children isnot restricted to two.

A Binary Tree of States

Each node ispermitted tohave two linksto other nodes,called the leftchild and theright child .

Page 9: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

9

But for binary trees, each node has at most two children. For example,the left child of Washington is Arkansas. The right child of Washingtonis Colorado.

A Binary Tree of States

Children areusually drawnbelow a node.

The right child ofWashington is

Colorado.

The left child ofWashington is

Arkansas.

Page 10: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

10

In a binary tree, a node might have a left child, but no right child. Forexample, the left child of Arkansas is Florida. But Arkansas has no rightchild.

A Binary Tree of States

Some nodeshave only onechild.

Arkansas has aleft child, but no

right child.

Page 11: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

11

Here is a quiz: Find a node that has a right child, but no left child...

A Quiz

Some nodeshave only onechild.

Which node hasonly a right child?

Page 12: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

12

In this example, Massachusetts is the right child of Florida, and Floridahas no left child.

A Quiz

Some nodeshave only onechild.

Florida hasonly a right child.

Page 13: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

13

Some nodes have no children, and those nodes are called leaves. Inthis example, there are four leaves: Massachusetts, Oklahoma, NewHampshire (or is that Vermont?) and Nebraska. (Yes, that really isNebraska. Either the author ran out of room on the slide and had toshrink it, or the author is from rival state Colorado.)

A Binary Tree of States

A node with nochildren iscalled a leaf.

Page 14: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

14

We can also refer to the parent of a node, which is the upward link. Forexample, Washington is the parent of both Arkansas and Colorado.

A Binary Tree of States

Each node iscalled theparent of itschildren.

Washington is theparent of Arkansas

and Colorado.

Page 15: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

15

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 thedefinition of a tree: If you start at the root, there is always one way toget from the root to any particular node by following a sequence ofdownward links (from a parent to its child).

A Binary Tree of States

Two rules about parents:

❶ The root has noparent.

❷ Every other nodehas exactly oneparent.

Page 16: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

16

Two nodes that have the same parent are called siblings, as shownhere.

In a binary tree, a node has at most one sibling.

A Binary Tree of States

Two nodes withthe same parentare calledsiblings.

Arkansasand Coloradoare siblings.

Page 17: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

17

The rest of this lecture demonstrates a special kind of binary tree calleda complete binary tree. The tree has several applications, and is alsospecial because it is extremely easy to implement.

Complete Binary Trees

A completebinary tree is aspecial kind ofbinary treewhich will beuseful to us.

Page 18: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

18

When a complete binary tree is built, its nodes are generally added oneat a time. As with any tree, the first node must be the root.

Complete Binary Trees

A completebinary tree is aspecial kind ofbinary treewhich will beuseful to us. When a complete

binary tree is built,its first node must be

the root.

Page 19: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

19

With a complete binary tree, the second node must be the left child ofthe root.

Complete Binary Trees

The second node of acomplete binary treeis always the leftchild of the root...

Page 20: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

20

The next node must be the right child of the root.

Complete Binary Trees

The second node of acomplete binary treeis always the leftchild of the root...

... and the third nodeis always the rightchild of the root.

Page 21: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

21

And so we continue, adding nodes. But in a complete binary tree, theway that we add nodes is restricted: Nodes must completely fill eachlevel from left-to-right before proceeding to the next level.

Following this rule, where would the fifth node have to be placed?

Complete Binary Trees

The nextnodes mustalways fillthe next levelfrom left toright .

Page 22: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

22

The fifth node must go in the next available spot in this level, which isthe right child of Arkansas.

By the way, there is also an interesting story about Idaho. Apparentlycongress decreed that the border between Idaho and Montana was tobe the continental divide. So the surveyers started at Yellowstone Park,mapping out the border, but at “Lost Trail Pass” they made a mistakeand took a false divide. They continued along the false divide forseveral hundred miles until they crossed over a ridge and saw the ClarkFork River cutting right across what they thought was the divide. At thispoint, they just gave up and took the rest of the border straight north toCanada. So those of you who come from Kalispell and MissoulaMontana should really be Idahonians.

Complete Binary Trees

The nextnodes mustalways fillthe next levelfrom left toright .

Page 23: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

23

Then we can add the left child of Colorado...

Complete Binary Trees

The nextnodes mustalways fillthe next levelfrom left toright .

Page 24: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

24

...and the right child of Colorado.

Where would the next node after this go?...

Complete Binary Trees

The nextnodes mustalways fillthe next levelfrom left toright .

Page 25: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

25

...now we can move to the next level, and add the first node as the leftchild of Florida.

Complete Binary Trees

The nextnodes mustalways fillthe next levelfrom left toright .

Page 26: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

26

Then the right child of Florida.

Complete Binary Trees

The nextnodes mustalways fillthe next levelfrom left toright .

Page 27: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

27

Just to check your understanding, is this binary tree a complete binarytree?

No, it isn't since the nodes of the bottom level have not been addedfrom left to right.

Is This Complete?

Page 28: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

28

Is this complete?

No, since the third level was not completed before starting the fourthlevel.

Is This Complete?

Page 29: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

29

This is also not complete since Washington has a right child but no leftchild.

Is This Complete?

Page 30: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

30

But this binary tree is complete. It has only one node, the root.

Is This Complete?

Page 31: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

31

This binary tree is also complete, although you might have to squint tosee it. You see, this binary tree has no nodes at all. It is called theempty tree, and it is considered to be a complete binary tree.

Is This Complete?

Yes!

✔ It is called the emptytree, and it has nonodes, not even a root.

Page 32: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

32

There is one last feature of complete binary trees that you need tolearn: How to implement a complete binary tree using a partially-filledarray. With this implementation, the data from each node is placed inone component of a partially-filled array. The primary thing we need tokeep track of is where in the array does each node belong?

Implementing a Complete BinaryTree

❐ We will store the date from the nodesin a partially-filled array.

An array of dataWe don't care what's inthis part of the array.

An integer to keeptrack of how many nodes are in the tree

3

Page 33: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

33

The solution to this problem is in Section 10.2 of the text, whichdiscusses several different ways to implement binary trees. Thesimplest implementation uses a partially-filled array, as suggested bythis picture, but you should be warned: The array implementation isonly practical for complete binary trees.

Implementing a Complete BinaryTree

❐ We will store the date from the nodesin a partially-filled array.

An array of dataWe don't care what's inthis part of the array.

An integer to keeptrack of how many nodes are in the tree

3

Read Section 10.2 tosee details of how

the entries are stored.

Page 34: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

34

A quick 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, youwill eventually reach the root.

❐ Every node except the root has one parent. Theroot has no parent.

❐ Complete binary trees require the nodes to fill ineach level from left-to-right before starting thenext level.

Summary

Page 35: Complete Binary Trees - cs.colorado.edumain/supplements/pdf/notes10a.pdf · 1 This lecture is an introduction to trees, illustrating basic terminology for binary trees, and focusing

35

Feel free to send your ideas to:

Michael Main

[email protected]

THE END

Presentation copyright 1997 Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.

Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.