Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of...

41
Chapter 5 Chapter 5 Binary Trees Binary Trees

Transcript of Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of...

Page 1: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Chapter 5Chapter 5

Binary TreesBinary Trees

Page 2: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Definitions and PropertiesDefinitions and Properties

A binary tree is made up of a finite set of elements A binary tree is made up of a finite set of elements called called nodesnodes

It consists of a It consists of a root root and two and two subtreessubtrees There is an There is an edge edge from the root to its childrenfrom the root to its children If nIf n11, n, n22,…n,…nkk is a sequence of nodes such that n is a sequence of nodes such that nii is the is the

parent of nparent of ni+1i+1, then that sequence is a , then that sequence is a pathpath The The lengthlength of the path is k-1 of the path is k-1 The The depthdepth of node M is the length of the path from of node M is the length of the path from

the root to Mthe root to M

Page 3: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

More definitionsMore definitions

The The heightheight of the tree is one more than the depth of of the tree is one more than the depth of the deepest nodethe deepest node

A A leafleaf node is any node with no children node is any node with no children An An internalinternal node is a node with at least one child node is a node with at least one child A A fullfull binary tree has each node as either an internal binary tree has each node as either an internal

node with two children or as a leafnode with two children or as a leaf A A completecomplete binary tree has each level filled and the binary tree has each level filled and the

last level may not be filled, but it is filled from left to last level may not be filled, but it is filled from left to rightright

Page 4: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Binary Tree TheoremsBinary Tree Theorems

The number of leaves in a non-empty full The number of leaves in a non-empty full binary tree is one more than the number of binary tree is one more than the number of internal nodes.internal nodes.

The number of empty subtrees in a non-empty The number of empty subtrees in a non-empty binary tree is one more than the number of binary tree is one more than the number of nodes in the treenodes in the tree

These are useful when you need to compute These are useful when you need to compute the amount of overhead used by a treethe amount of overhead used by a tree

Page 5: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Binary Tree ADTBinary Tree ADT

There are activities that relate to nodesThere are activities that relate to nodes Reach a childReach a child Get a valueGet a value

There are activities that relate to treesThere are activities that relate to trees InitializationInitialization ClearingClearing

Due to these activities the Node class should Due to these activities the Node class should be separate from the Tree classbe separate from the Tree class

Page 6: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Binary Tree TraversalsBinary Tree Traversals

Any process for visiting all the nodes in some Any process for visiting all the nodes in some order is a order is a traversaltraversal

Any traversal that visits each node only one Any traversal that visits each node only one time is an time is an enumerationenumeration

There are three main kinds of enumerations on There are three main kinds of enumerations on treestrees PreorderPreorder PostorderPostorder InorderInorder

Page 7: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Traversal FunctionTraversal Function

The traversal function is easily written as a The traversal function is easily written as a recursive functionrecursive function

An important design decision is when to check An important design decision is when to check for empty subtreesfor empty subtrees You can check at the beginning of the function, You can check at the beginning of the function,

returning if you are a null pointerreturning if you are a null pointer You can check before you call the recursive You can check before you call the recursive

functionfunction The first choice is betterThe first choice is better

Page 8: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Why?Why?

We would have to use two checks for a null We would have to use two checks for a null tree versus one.tree versus one.

We make twice as many function calls.We make twice as many function calls. We are required that an empty tree is never We are required that an empty tree is never

passed in or make an additional check for an passed in or make an additional check for an empty treeempty tree

In any case, it gets complicated and error In any case, it gets complicated and error proneprone

Page 9: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Binary Tree Node ImplementationsBinary Tree Node Implementations

Normally nodes contain a data field value and Normally nodes contain a data field value and two pointers to childrentwo pointers to children

Some programmers find it convenient to add a Some programmers find it convenient to add a pointer to the parentpointer to the parent

This is somewhat analogous to a pointer to a This is somewhat analogous to a pointer to a previous node in a Linked Listprevious node in a Linked List

However, it is almost always unnecessary and However, it is almost always unnecessary and add unneeded overheadadd unneeded overhead

Page 10: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

More Binary NodesMore Binary Nodes

An important decision is whether the same An important decision is whether the same class can be used for internal and leaf nodesclass can be used for internal and leaf nodes

Some applications only store data in the leaf Some applications only store data in the leaf nodes.nodes.

Other applications require one type of data in Other applications require one type of data in an internal node and another in the leaves.an internal node and another in the leaves.

There are some choices in what to doThere are some choices in what to do

Page 11: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Union ConstructUnion Construct

enum Nodetype {leaf, internal};enum Nodetype {leaf, internal};

class VarBinNode { // Generic node classclass VarBinNode { // Generic node classpublic:public: Nodetype mytype; // Store type for nodeNodetype mytype; // Store type for node union {union { struct { // internal nodestruct { // internal node VarBinNode* left; // Left childVarBinNode* left; // Left child VarBinNode* right; // Right childVarBinNode* right; // Right child Operator opx; // ValueOperator opx; // Value } intl;} intl; Operand var; // Leaf: Value onlyOperand var; // Leaf: Value only };};

Page 12: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Huh?Huh?

In the union construct, you get one or the In the union construct, you get one or the other, but not bothother, but not both

So we either get the pointers to the children So we either get the pointers to the children and the operator (internal node)and the operator (internal node)

Or we get the value (leaf node)Or we get the value (leaf node) Problem is the data structure store enough Problem is the data structure store enough

space for the biggest subtypespace for the biggest subtype

Page 13: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Better ApproachBetter Approach

Use inheritanceUse inheritance Have a base class that implements a general nodeHave a base class that implements a general node Inherit from the base class one for each type of nodeInherit from the base class one for each type of node The method isLeaf() returns true for leaves and false The method isLeaf() returns true for leaves and false

otherwiseotherwise In this option the tree handles traversing the nodes.In this option the tree handles traversing the nodes. This method uses polymorphism to handle which This method uses polymorphism to handle which

member functions get calledmember functions get called

Page 14: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Third ApproachThird Approach

This approach also uses inheritance.This approach also uses inheritance. The major difference is that the nodes know The major difference is that the nodes know

how to traverse themselves.how to traverse themselves. Meaning that each subclass implements a Meaning that each subclass implements a

traverse function that is polymorphically traverse function that is polymorphically called.called.

This is known as a Composite design patternThis is known as a Composite design pattern

Page 15: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Pros and ConsPros and Cons

When comparing the two inheritance methodsWhen comparing the two inheritance methods The first does not require that the node classes The first does not require that the node classes

explicitly support a traverse functionexplicitly support a traverse function This fact makes it easy to add new node typesThis fact makes it easy to add new node types The second approach relieves the tree class The second approach relieves the tree class

from needing to know anything about the from needing to know anything about the internals of the node classes.internals of the node classes.

Secondly, the traverse function does not need Secondly, the traverse function does not need to know all the node typesto know all the node types

Page 16: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Space RequirementsSpace Requirements

Assuming every node has two pointers and a Assuming every node has two pointers and a data spotdata spot

For n nodes we getFor n nodes we get n(2p + d)n(2p + d)

The total overhead required is 2pn for the The total overhead required is 2pn for the entire treeentire tree

The fraction of overhead is 2p/(2p+d)The fraction of overhead is 2p/(2p+d) If p = d, then about 2/3 of the tree is overheadIf p = d, then about 2/3 of the tree is overhead

Page 17: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Space for Full TreesSpace for Full Trees

If only the leaves store data, then we can get If only the leaves store data, then we can get great savings in space.great savings in space.

(n/2(2p))/(n/2(2p)+d) = p/(p+d)(n/2(2p))/(n/2(2p)+d) = p/(p+d) When p = d we get about ½ of the space is When p = d we get about ½ of the space is

overheadoverhead This is much better.This is much better.

Page 18: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Array Implementation for Complete Array Implementation for Complete Binary TreesBinary Trees

We begin by assigning numbers to the node We begin by assigning numbers to the node position in the complete binary tree, level by position in the complete binary tree, level by level.level.

An array can store the tree’s data value An array can store the tree’s data value efficiently, placing each value in the array efficiently, placing each value in the array position corresponding to the nodes positionposition corresponding to the nodes position

Simple formulas can be determined to Simple formulas can be determined to calculate siblings, parents, and children.calculate siblings, parents, and children.

Page 19: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

--------1010----88----66----44----22----Right SiblingRight Sibling

----99----77----55----33----11--------Left SiblingLeft Sibling

----------------------------101088664422Right ChildRight Child

------------------------11119977553311Left ChildLeft Child

5544443333222211110000----ParentParent

1111101099887766554433221100PositionPosition

Page 20: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

FormulasFormulas

Parent(r) = | (r-1)/2 | if r Parent(r) = | (r-1)/2 | if r ≠ 0≠ 0 Left Child(r) = 2r+1 if 2r+1 < nLeft Child(r) = 2r+1 if 2r+1 < n Right Child(r) = 2r+2 if 2r+2 < nRight Child(r) = 2r+2 if 2r+2 < n Left sibling(r) = r-1 if r is evenLeft sibling(r) = r-1 if r is even Right sibling(r) = r+1 is odd and r+1 < nRight sibling(r) = r+1 is odd and r+1 < n

Page 21: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Binary Search TreeBinary Search Tree

Binary Search Tree (BST) Property: All nodes Binary Search Tree (BST) Property: All nodes stored in the left subtree of a node whose key stored in the left subtree of a node whose key is K have key values less than K. All nodes is K have key values less than K. All nodes stored in the right subtree of a node whose key stored in the right subtree of a node whose key value is K have key values greater than or value is K have key values greater than or equal to K.equal to K.

Page 22: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.
Page 23: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Method ExampleMethod Example

bool find(const Key& k) constbool find(const Key& k) const{ return findHelp(root, K); }{ return findHelp(root, K); }

bool findHelp(BinNode* subRoot, const Key& k) constbool findHelp(BinNode* subRoot, const Key& k) const{{

if(subRoot == NULL) return false;if(subRoot == NULL) return false;else if( k < subRoot->val() )else if( k < subRoot->val() )

return findHelp(subRoot->left(), k);return findHelp(subRoot->left(), k);else if( k > subRoot->val() )else if( k > subRoot->val() )

return findHelp(subRoot->right(), k);return findHelp(subRoot->right(), k);elseelse

return true;return true;}}

Page 24: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

InsertionInsertion

Insertion requires that the location for the record is Insertion requires that the location for the record is first locatedfirst located

The recursion ends when the subroot is nullThe recursion ends when the subroot is null Either the node is an internal node with an open child Either the node is an internal node with an open child

on the correct side or the node is a leafon the correct side or the node is a leaf A new node is created and the node is inserted in the A new node is created and the node is inserted in the

treetree Question: how does the parent know who their new Question: how does the parent know who their new

child is?child is?

Page 25: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Insertion IdeasInsertion Ideas

Insert uses an insertHelper function to actually Insert uses an insertHelper function to actually perform the insertion.perform the insertion.

insertHelper returns a pointer to itselfinsertHelper returns a pointer to itself This allows the parent to set the correct pointer This allows the parent to set the correct pointer

to point to the new childto point to the new child

Page 26: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Removing NodesRemoving Nodes

This is a bit trickier than insertion.This is a bit trickier than insertion. Let's look at the smaller problem of removing the Let's look at the smaller problem of removing the

smallest item from a given subtreesmallest item from a given subtree We will traverse down the left child pointers until we We will traverse down the left child pointers until we

find a node that does not have a left childfind a node that does not have a left child We will call that node min and return a reference to itWe will call that node min and return a reference to it We will also return a pointer to its right subchildWe will also return a pointer to its right subchild

Page 27: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

More DeletionsMore Deletions

So to delete a general key value R, we first find So to delete a general key value R, we first find it and check its childrenit and check its children

If it is a leaf, we blow it awayIf it is a leaf, we blow it away If it has one subchild, we have the node's parent If it has one subchild, we have the node's parent

point to it and we are donepoint to it and we are done If it has two children, well we have to do more If it has two children, well we have to do more

workwork What can we do?What can we do?

Page 28: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

More DeletionsMore Deletions

We can find a value to substitute for this value We can find a value to substitute for this value that will keep the BST propertythat will keep the BST property

Two choices,Two choices, The greatest value from the left subtreeThe greatest value from the left subtree The least value from the right subtreeThe least value from the right subtree

Well, we find the smallest value in the nodes Well, we find the smallest value in the nodes right subtree.right subtree.

Why?Why?

Page 29: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Threaded TreesThreaded Trees

All previously discussed tree traversals All previously discussed tree traversals required a stackrequired a stack

Either the stack is created and maintained Either the stack is created and maintained explicitly by the user or created by the system explicitly by the user or created by the system as the run-time stack.as the run-time stack.

So there is there another way to traverse a tree So there is there another way to traverse a tree without a stack?without a stack? Of course there is…otherwise why would we be Of course there is…otherwise why would we be

talking about it.talking about it.

Page 30: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Stackless Depth-First TraversalStackless Depth-First Traversal

Threaded trees allow you traverse the tree by Threaded trees allow you traverse the tree by following pointers stored within the treefollowing pointers stored within the tree

Each node would store pointers to its Each node would store pointers to its predecessor and successorpredecessor and successor

This would create a lot of overhead with the This would create a lot of overhead with the additional two pointers for a total of 4 pointers additional two pointers for a total of 4 pointers per nodeper node

So what can we do?So what can we do?

Page 31: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

It’s all in the contextIt’s all in the context

You’ve already got two pointers in the nodes, You’ve already got two pointers in the nodes, why not use them?why not use them?

Of course you need to know what pointers are Of course you need to know what pointers are currently referring to, their contextcurrently referring to, their context

You can use a bit to indicate what the pointer You can use a bit to indicate what the pointer is being used for.is being used for.

How does this work?How does this work?

Page 32: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Inorder exampleInorder examplevoid ThreadedTree<T>::inorder(){void ThreadedTree<T>::inorder(){ThreadedNode<T> *prev, *p = root;ThreadedNode<T> *prev, *p = root;if( p != 0 )if( p != 0 ) {{

while ( p->left != 0 )while ( p->left != 0 )p = p->left;p = p->left;

while( p != 0 ) {while( p != 0 ) {visit( p );visit( p );prev = p;prev = p;p = p->right;p = p->right;if ( p != 0 && prev->successor == if ( p != 0 && prev->successor ==

0)0)while( p->left != 0 )while( p->left != 0 )

p = p->left;p = p->left;}}

}}}}

Page 33: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Other MethodsOther Methods

You can also reorganize the tree to traverse the You can also reorganize the tree to traverse the tree without stacks and without threadstree without stacks and without threads

It is easy to traverse a tree with no left childIt is easy to traverse a tree with no left child One such algorithm is:One such algorithm is:

MorrisInorder()MorrisInorder() While not finishedWhile not finished

If node has no left descendantIf node has no left descendant Visit it;Visit it; Go to the rightGo to the right

Else make this node the right child of the rightmost node in its Else make this node the right child of the rightmost node in its left descendants, go to this left descendantleft descendants, go to this left descendant

Page 34: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Heaps and Priority QueuesHeaps and Priority Queues

There are many cases where we want to choose There are many cases where we want to choose the next most important item from a listthe next most important item from a list

This occurs in hospitals, OSs, etc.This occurs in hospitals, OSs, etc. Normal queues are not efficient enough to Normal queues are not efficient enough to

implement a priority queue.implement a priority queue. We could use a binary tree to help with this We could use a binary tree to help with this

problemproblem

Page 35: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

HeapsHeaps

A heap is a complete binary tree, and so it is A heap is a complete binary tree, and so it is almost always implemented in an array.almost always implemented in an array.

The values stored in a heap are partially orderedThe values stored in a heap are partially ordered This means that there is a relationship between This means that there is a relationship between

the value stored at any node and the values of the value stored at any node and the values of its children.its children.

There are two variationsThere are two variations

Page 36: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Min Heaps and Max HeapsMin Heaps and Max Heaps

The The min heap min heap stores at every node a value that stores at every node a value that is less than or equal to that of its children.is less than or equal to that of its children.

The root stores the smallest valueThe root stores the smallest value The The max heapmax heap stores at every node a value that stores at every node a value that

is greater than or equal to that if its childrenis greater than or equal to that if its children The root stores the greatest valueThe root stores the greatest value

Page 37: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Building a HeapBuilding a Heap

(a) (4-2) (4-1) (2-1) (5-2) (5-4) (6-3) (6-5) (7-5) (7-6)(b) (5-2), (7-3), (7-1), (6-1)

Page 38: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

DetailsDetails

Assume you have a root and its children are Assume you have a root and its children are already heaps.already heaps.

You “siftdown” the root to the correct You “siftdown” the root to the correct location.location. If the root is greater than its children you are doneIf the root is greater than its children you are done If not exchange the root with its bigger child and If not exchange the root with its bigger child and

repeat at the next levelrepeat at the next level Where do you start this process?Where do you start this process?

At the last internal node.At the last internal node.

Page 39: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

ExampleExample

Page 40: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

RemovingRemoving

Removing requires that we replace the root Removing requires that we replace the root with a value so that we maintain the shape of with a value so that we maintain the shape of the tree.the tree.

Take the last node and swap it with the root.Take the last node and swap it with the root. Call siftdown on the root.Call siftdown on the root.

Page 41: Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.

Cost to Build a HeapCost to Build a Heap

Cost for heap construction:Cost for heap construction:

log log nn

((ii - 1) - 1) nn/2/2ii nn..

ii=1=1