cs 132 spring 2008 chapter 11
DESCRIPTION
CS 132 Spring 2008 Chapter 11. Binary Trees p. 631-666. Jill’s Pizza Shop. Owner Jill Manager Chef Brad Carl - PowerPoint PPT PresentationTRANSCRIPT
1
CS 132 Spring 2008Chapter 11
Binary Trees
p. 631-666
2
Jill’s Pizza Shop
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
3
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has a Root Node
ROOT NODE
4
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
Leaf Nodes Have No Children
LEAF NODES
5
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has Levels
LEVEL 0
LEVEL 1
6
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
A Subtree
LEFT SUBTREE OF ROOT NODE
Note: the left subtree is a tree
7
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
Another Subtree
RIGHT SUBTREE OF ROOT NODE
8
Binary Trees (formal definition)
A binary tree, T, is either empty or such that
– T has a special node called the root node
– T has two sets of nodes:
LT (the left subtreeof T)
RT (the right subtree of T)
– LT and RT are binary trees
9
Binary Tree Definitions
Leaf: node that has no left and right children
Parent: node with at least one child node
Level of a node: number of steps from root to the node
Height: number of nodes in the longest path from root to a leaf
10
Binary TreesA node of a binary tree:
template<class elemType>
struct nodeType
{
elemType info; //data
nodeType<elemType> *llink; //ptr to left child
nodeType<elemType> *rlink; //ptr to right child
};
Hand out binaryTree.h
11
Height of a Binary Tree
Recursive algorithm to find height of binary tree with root p:
if(p is NULL)
height(p) = 0
else
height(p) = 1 + max(height(p->llink), height(p->rlink))
Recursive function:template<class elemType>
int height(nodeType<elemType> *p)
{
if(p == NULL)
return 0;
else
return 1 + max(height(p->llink), height(p->rlink));
}
12
Binary Tree Traversal
Visit the root node first or visit the subtrees first– inorder
• traverse the left subtree• visit the node• traverse the right subtree
– preorder• visit the node• traverse the left subtree• traverse the right subtree
– postorder• traverse the left subtree• traverse the right subtree• visit the node
13
Binary TreeTraversal
Inorder:B D A C
Preorder:A B D C
Postorder:C D B A
14
Binary Tree: Traversals
template<class elemType>
void postorder(nodeType<elemType> *p)
{
if(p != NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout<<p->info<<” “;
}
}1
template<class elemType>
void inorder(nodeType<elemType> *p)
{
if(p != NULL)
{
inorder(p->llink);
cout<<p->info<<” “;
inorder(p->rlink);
}
}
What about preorder?
15
Traversal of Binary Expression Trees
What would be the effect of traversalinorder
preorder
postorder
More later
*
ba
16
A special kind of binary tree in which:
1. each node contains a distinct data value,
2. the key values in the tree can be compared using “greater than” and “less than”, and
3. the key value of each node in the tree is
less than every key value in its right subtree, and
greater than every key value in its left subtree
A Binary Search Tree (BST)
What would inorder traversal produce?
17
Binary Search Trees
E
Q
L
K v
T A
S
What would inorder traversal produce?
18
Operations on Binary Search Trees
Search for a particular item
How do you know if 46 is in a tree?– is it the root?
– no, less;
– go left ...
19
How Do You Know If 46 Is In A Tree?
Is it the root? No, less; go left ...
20
template<class elemType>
bool bSearchTreeType<elemType>::search(const elemType& searchItem)
{
nodeType<elemType> *current;
bool found = false;
if(root == NULL)
cerr<<"Cannot search the empty tree."<<endl;
else
{
current = root;
while(current != NULL && !found)
{
if(current->info == searchItem)
found = true;
else
if(current->info > searchItem)
current = current->llink;
else
current = current->rlink;
}//end while
}//end else
return found;
}//end search
Is there another way?
21
Binary Search Tree Analysis
Worst Case: linear tree
n nodes: n steps
22
Binary Search Tree Analysis
Best case: a balanced tree
Nodes Steps 1 1
3 2
7 3
15 4
2n – 1 n
23
Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.
Insert the first value into the root node:
Creating Binary Search Tree
‘J’
24
Thereafter, each value to be inserted begins by – comparing itself to the value in the root node
– moving left it is less, or moving right if it is greater
– continuing down until it can be inserted as a new leaf
E is less than J, go left:
Inserting ‘E’ into the BST
‘J’
‘E’
25
Compare ‘F’ to the value in the root node
Move left because it is less
Compare ‘F’ to the left child node value, move right
We reached the bottom, insert the node
Inserting ‘F’ into the BST
‘J’
‘E’
‘F’
new node
26
Compare ‘T’ to the value in the root node ...
Inserting ‘T’ into the BST
‘J’
‘E’
‘F’
‘T’
27
Compare ‘A’ to the value in the root node ...
Inserting ‘A’ into the BST
‘J’
‘E’
‘F’
‘T’
‘A’
28
Another Binary Search Tree
Add nodes for these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’
‘K’ ‘P’
29
What BST is obtained by inserting elements
‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?
Creating a Binary Search Tree
‘A’
‘E’
‘F’
‘J’
‘T’
30
AVL (Height-balanced Trees)
A perfectly balanced binary tree is a binary tree such that:– The height of the left and right subtrees of the root are equal
– The left and right subtrees of the root are perfectly balanced binary trees
31
AVL (Height-balanced Trees)
An AVL tree (or height-balanced tree) is a binary search tree such that:– the height of the left and right subtrees of the root differ by at most 1
– the left and right subtrees of the root are AVL trees
Goal: maintain a BST as AVL
Approach: rebalance unbalanced
trees every now and then
Us: skip this part
32
Full and Complete Binary Trees
A full binary tree: all the leaves are on the same level and every non leaf node has two children.
A complete binary tree: is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible.
33
Heaps
A heap is a binary tree that satisfies special properties:
– shape: a complete binary tree.
– order: the value stored in each node is greater than or equal to the value in each of its children.
Main applications: – heap sort
– to implement a priority queue
34
Are These Heaps?
C
A T
treePtr
50
20
18
30
10
treePtr
35
70
60
40 30
12
8 10
treePtr
Is This a Heap?
Where is the largest element in a heap?
36
70
0
60
1
40
3
30
4
12
2
8
5
tree
Number the Nodes Left to Right by Level
37
70
0
60
1
40
3
30
4
12
2
6
5
tree
Use Node Numbers as Array Indexes
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
6
tree.nodes
38
Heap Sort for an Array
1. Make an unsorted array into a heap
2. Take the root (maximum) element off the heap by swapping it into its correct place in the array at the end of the unsorted elements
3. Reheap the remaining unsorted elements
4. Repeat until all elements are sorted
39
Reheap Down (Step 3)
To make heaps when all but the top element is in heap position
How: – swap root with left or right child, which ever violates heapness
– reheap (down) with that child
Used when application wants to remove the highest element: – remove the root
– put last element in root position
– reheap down to restore the order
40
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
6
10
values
70
0
60
1
40
3
30
4
12
2
6
5
root
10
6
Create the Original Heap (how later)
41
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
6
10
values
70
0
60
1
40
3
30
4
12
2
6
5
root
10
6
Swap Root Element int last Place in Unsorted Array
42
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
60
1
40
3
30
4
12
2
6
5
root
70
6
10
60
12
40
30
6
70 This is in the right slotNo need to consider again
After Swapping Root Element
43
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
values
10
0
60
1
40
3
30
4
12
2
6
5
root10
60
12
40
30
6
10 is out of place, swap with its largest child 6010 is out of place, swap with its largest child 40
The Unsorted Array Is No Longer A Heap
44
Remaining Unsorted Elements Reheaped:
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
60
0
40
1
10
3
30
4
12
2
6
5
root
70
6
60
40
12
10
30
6
70
45
Swap Root Element Into Last Unsorted Place
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
60
0
40
1
10
3
30
4
12
2
6 5
root
70
6
60
40
12
10
30
6
70
46
After Swapping Root Element
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
40
1
10
3
30
4
12
2
root
70
6
6
40
12
10
30
60
70
60
5
In the right slotsNo need to consider again
47
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
40
0
30
1
10
3
6
4
12
2
root
70
6
40
30
12
10
6
60
70
60
5
Reheap Remaining Unsorted Elements
48
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
40
0
30
1
10
3
6
4
12
2
root
70
6
40
30
12
10
6
60
70
60
5
Swap Root Element Into Last Unsorted Place
49
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
30
1
10
3
12
2
root
70
6
60
5
40
4
6
30
12
10
40
60
70 In the right slotsNo need to consider again
After Swapping Root Element
50
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
30
0
10
1
6
3
12
2
root
70
6
60
5
40
4
30
10
12
6
40
60
70
Reheap Remaining Unsorted Elements
51
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
30
0
10
1
6
3
12
2
root
70
6
60
5
40
4
30
10
12
6
40
60
70
Swap Root Element Into Last Unsorted Place
52
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
10
1
12
2
root
70
6
60
5
40
4
6
10
12
30
40
60
70
30
3 In the right slots
No need to consider again
After Swapping Root Element
53
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
12
0
10
1
6
2
root
70
6
60
5
40
4
12
10
6
30
40
60
70
30
3
After Reheaping Remaining Unsorted Elements
54
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
12
0
10
1
6
2
root
70
6
60
5
40
4
12
10
6
30
40
60
70
30
3
Swap Root Element Into Last Unsorted Place
55
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
10
1
root
70
6
60
5
40
4
30
3
12
2
6
10
12
30
40
60
70 In the right slotsNo need to consider again
After Swapping Root Element
56
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
6
1
root
70
6
60
5
40
4
30
3
12
2
10
6
12
30
40
60
70
After Reheaping Remaining Unsorted Elements
57
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
6
1
root
70
6
60
5
40
4
30
3
12
2
10
6
12
30
40
60
70
Swap Root Element Into Last Unsorted Place
58
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
root
70
6
60
5
40
4
30
3
12
2
6
10
12
30
40
60
70
10
1
6
0
All elements are sorted
After Swapping Root Element
59
Implementing Heaps
Text approach– add function to orderedArrayListType to build a heap
– add function to orderedArrayListType for heap sort
Comment: – this is counterintuitive
– a heap is not an ordered array
A different approach– define a heap struct
– include functions for converting an array to a heap
– use the heap struct to build heap sort
60
// HEAP SPECIFICATION
// Assumes ItemType is either a built-in simple data type// or a class with overloaded relational operators.
template< class ItemType >struct HeapType
{ void ReheapDown ( int root , int bottom ) ;
void ReheapUp ( int root, int bottom ) ;
ItemType* elements ; // ARRAY to be allocated dynamically
int numElements ;
};
60
61
Observations
Heap is a struct with member functions
Why not a class? – to allow access to its components
– heaps are usually used in conjunction with other classes which want access to the components but will hide the components from the application using them
62
Reheap Down
Make a heap when all but the top element is in heap position
How: – swap root with left or right child, whicheever violates heapness
– reheap (down) with that child
Used to put the highest element is its sorted position: – remove the root
– put last element in root position
– reheap down to restore the order
63
Reheap Up(slide differs from notes)
For a node that violates a heap upwards:
swap with parent until it is a heap
So, if we add a node to the “end” of the tree that is otherwise a heap, reheap up would put it in the right spot
Build a heap method 1:– start with empty heap– add element to the end and reheap up
Build a heap method 2 (in heapsort.h):
– reheap down with the parent of the last child – reheap down with the element before that– ...– reheap down with node 0
64
Back to Heapsort
This does not use the heap struct
It adds the array to be sorted as a parameter to ReheapDown:
template<class elemType>
void heapSort(elemType values[], int numValues)
{
// Convert the array of values into a heap.
// Sort the array.
}
65
template <class ItemType >void HeapSort ( ItemType values [ ] , int numValues )// Post: Sorts array values[ 0 . . numValues-1 ] into // ascending order by key{
int index ; // Convert array values[0..numValues-1] into a heapfor (index = numValues/2 - 1; index >= 0; index--)
ReheapDown ( values , index , numValues - 1 ) ;
// Sort the array.for (index = numValues - 1; index >= 1; index--){ Swap (values [0] , values[index]);
ReheapDown (values , 0 , index - 1);}
}
Alternate way to convert values[0..numValues-1] into a heap:for (index =1; numValues-1; index++)
ReheapUp ( values , 0 , index ) ;
65
66
Exercises
Make a heap out of elements
D H A K P R
Remove the top element and reheap
67
Priority Queues
An ADT with the property that only the highest-priority element can be accessed at any time
Example: homework assignments are ordered by priority
Like a sorted list except for the access restriction
Goal: be able to insert in order and to access the top element
efficiently
Approach: use a heap
Notation:
Enqueue = = addQueue
Dequeue = = remove and return the removed item
(combines front and deleteQueue)
68
PQType
~PQType
Enqueue
Dequeue . . .
class PQType<char>
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
‘X’ ‘C’ ‘J’
Private Data:
numItems
3
maxItems
10
items
.elements .numElements
3
69
// CLASS PQTYPE DEFINITION AND MEMBER FUNCTIONS //----------------------------------------------------
template<class ItemType>
class PQType {
public:
PQType( int );
~PQType ( );
void MakeEmpty( );
bool IsEmpty( ) const;
bool IsFull( ) const;
void Enqueue( elemType item ); //adds a new item
void Dequeue( elemType& item ); //returns the removed item
private:
int numItems;
heapType<elemType> items;
int maxItems;
};
See driver code on website
69
70
Priority Queues: enqueue
Assuming the priority queue is implemented as a heap,
insert the new element in the next position in the list and
reheap up
template<class elemType>
void PQType<elemType>::Enqueue(elemType newItem)
{
numItems++;
items.elements[numItems - 1] = newItem;
items.ReheapUp(0, numItems - 1);
}
71
Priority Queues: dequeue
Assume the priority queue is implemented as a heap
To remove the first element of the priority queue:copy the last element of the list into the first array position.
reduce the length of the list by 1.
ReheapUp to restore the heap in the list
template<class elemType>
void PQType<elemType>::Dequeue(elemType& item)
{
item = items.elements[0]; //the value returned
items.elements[0] = items.elements[numItems-1]; //1
numItems--; //2
items.ReheapDown(0, numItems - 1); //3
}
72
A special kind of binary tree in which:
1. each leaf node contains a single operand
2. each nonleaf node contains a single binary
3. the left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator
A Binary Expression Tree is . . .
73
A Two-Level Binary Expression
‘-’
‘8’ ‘5’
treePtr
INORDER TRAVERSAL: 8 - 5 has value 3
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
74
Levels Indicate Precedence
When a binary expression tree represents an expression, the
levels of the nodes in the tree indicate their relative
precedence of evaluation
Operations at higher levels of the tree are evaluated later than
those below them
The root operation is always performed last
75
A Binary Expression Tree
‘*’
‘+’
‘4’
‘3’
‘2’
What value does it have? ( 4 + 2 ) * 3 = 18
What infix, prefix, postfix expressions does it represent?
76
Inorder Traversal: (A + H) / (M - Y)
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
tree
Print left subtree first Print right subtree last
Print second
77
Preorder Traversal: / + A H - M Y
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
tree
Print left subtree second Print right subtree last
Print first
78
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
tree
Print left subtree first Print right subtree second
Print last
Postorder Traversal: A H + M Y - /
79
Evaluate:
‘*’
‘-’
‘8’ ‘5’
What infix, prefix, postfix expressions does it represent?
‘/’
‘+’
‘4’
‘3’
‘2’
80
Answer
Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )
Prefix: * - 8 5 / + 4 2 3
Postfix: 8 5 - 4 2 + 3 / * has operators in order used
‘*’
‘-’
‘8’ ‘5’
‘/’
‘+’
‘4’
‘3’
‘2’
81
The Content in a Tree Nodes has 2 forms
enum OpType { OPERATOR, OPERAND } ;
struct elemType {
OpType whichType; union // content dependes on the value of whichType {
char operation ; int operand ; }};
. whichType . operation
OPERATOR ‘+’
. whichType . operand
OPERAND 7
82
Tree Nodes
struct NodeType {
elemType info ; // Data member TreeNode* llink ; // Pointer to left child TreeNode* rlink ; // Pointer to right child};
. llink . info . rlink
NULL NULL
. whichType . operand
OPERAND 7
83
class ExprTree
‘*’
‘+’
4
3
2
ExprTree
~ExprTree
Build
Evaluate . . .
private:
root
84
What if ExprTree allowed variables?
‘*’
‘+’
‘x’
‘x’
2
ExprTree
~ExprTree
Build
Evaluate . . .
private:
root
85
What Can You Do With This?
Add VARIABLE to enumerated type Operand
Evaluate a function? int Eval ( TreeNode* ptr, float value ) ... case VARIABLE return value
Differentiate a function?