Download - CS 132 Spring 2008 Chapter 11
![Page 1: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/1.jpg)
1
CS 132 Spring 2008Chapter 11
Binary Trees
p. 631-666
![Page 2: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/2.jpg)
2
Jill’s Pizza Shop
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
![Page 3: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/3.jpg)
3
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has a Root Node
ROOT NODE
![Page 4: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/4.jpg)
4
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
Leaf Nodes Have No Children
LEAF NODES
![Page 5: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/5.jpg)
5
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has Levels
LEVEL 0
LEVEL 1
![Page 6: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/6.jpg)
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
![Page 7: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/7.jpg)
7
Owner Jill
Manager Chef Brad Carl
Waitress Waiter Cook Helper Joyce Chris Max Len
Another Subtree
RIGHT SUBTREE OF ROOT NODE
![Page 8: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/8.jpg)
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
![Page 9: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/9.jpg)
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
![Page 10: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/10.jpg)
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
![Page 11: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/11.jpg)
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));
}
![Page 12: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/12.jpg)
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
![Page 13: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/13.jpg)
13
Binary TreeTraversal
Inorder:B D A C
Preorder:A B D C
Postorder:C D B A
![Page 14: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/14.jpg)
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?
![Page 15: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/15.jpg)
15
Traversal of Binary Expression Trees
What would be the effect of traversalinorder
preorder
postorder
More later
*
ba
![Page 16: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/16.jpg)
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?
![Page 17: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/17.jpg)
17
Binary Search Trees
E
Q
L
K v
T A
S
What would inorder traversal produce?
![Page 18: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/18.jpg)
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 ...
![Page 19: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/19.jpg)
19
How Do You Know If 46 Is In A Tree?
Is it the root? No, less; go left ...
![Page 20: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/20.jpg)
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?
![Page 21: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/21.jpg)
21
Binary Search Tree Analysis
Worst Case: linear tree
n nodes: n steps
![Page 22: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/22.jpg)
22
Binary Search Tree Analysis
Best case: a balanced tree
Nodes Steps 1 1
3 2
7 3
15 4
2n – 1 n
![Page 23: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/23.jpg)
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’
![Page 24: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/24.jpg)
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’
![Page 25: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/25.jpg)
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
![Page 26: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/26.jpg)
26
Compare ‘T’ to the value in the root node ...
Inserting ‘T’ into the BST
‘J’
‘E’
‘F’
‘T’
![Page 27: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/27.jpg)
27
Compare ‘A’ to the value in the root node ...
Inserting ‘A’ into the BST
‘J’
‘E’
‘F’
‘T’
‘A’
![Page 28: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/28.jpg)
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’
![Page 29: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/29.jpg)
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’
![Page 30: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/30.jpg)
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
![Page 31: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/31.jpg)
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
![Page 32: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/32.jpg)
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.
![Page 33: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/33.jpg)
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
![Page 34: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/34.jpg)
34
Are These Heaps?
C
A T
treePtr
50
20
18
30
10
treePtr
![Page 35: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/35.jpg)
35
70
60
40 30
12
8 10
treePtr
Is This a Heap?
Where is the largest element in a heap?
![Page 36: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/36.jpg)
36
70
0
60
1
40
3
30
4
12
2
8
5
tree
Number the Nodes Left to Right by Level
![Page 37: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/37.jpg)
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
![Page 38: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/38.jpg)
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
![Page 39: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/39.jpg)
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
![Page 40: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/40.jpg)
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)
![Page 41: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/41.jpg)
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
![Page 42: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/42.jpg)
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
![Page 43: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/43.jpg)
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
![Page 44: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/44.jpg)
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
![Page 45: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/45.jpg)
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
![Page 46: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/46.jpg)
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
![Page 47: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/47.jpg)
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
![Page 48: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/48.jpg)
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
![Page 49: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/49.jpg)
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
![Page 50: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/50.jpg)
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
![Page 51: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/51.jpg)
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
![Page 52: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/52.jpg)
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
![Page 53: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/53.jpg)
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
![Page 54: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/54.jpg)
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
![Page 55: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/55.jpg)
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
![Page 56: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/56.jpg)
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
![Page 57: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/57.jpg)
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
![Page 58: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/58.jpg)
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
![Page 59: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/59.jpg)
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
![Page 60: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/60.jpg)
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
![Page 61: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/61.jpg)
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
![Page 62: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/62.jpg)
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
![Page 63: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/63.jpg)
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
![Page 64: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/64.jpg)
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.
}
![Page 65: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/65.jpg)
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
![Page 66: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/66.jpg)
66
Exercises
Make a heap out of elements
D H A K P R
Remove the top element and reheap
![Page 67: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/67.jpg)
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)
![Page 68: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/68.jpg)
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
![Page 69: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/69.jpg)
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
![Page 70: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/70.jpg)
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);
}
![Page 71: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/71.jpg)
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
}
![Page 72: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/72.jpg)
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 . . .
![Page 73: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/73.jpg)
73
A Two-Level Binary Expression
‘-’
‘8’ ‘5’
treePtr
INORDER TRAVERSAL: 8 - 5 has value 3
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
![Page 74: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/74.jpg)
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
![Page 75: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/75.jpg)
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?
![Page 76: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/76.jpg)
76
Inorder Traversal: (A + H) / (M - Y)
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
tree
Print left subtree first Print right subtree last
Print second
![Page 77: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/77.jpg)
77
Preorder Traversal: / + A H - M Y
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
tree
Print left subtree second Print right subtree last
Print first
![Page 78: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/78.jpg)
78
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
tree
Print left subtree first Print right subtree second
Print last
Postorder Traversal: A H + M Y - /
![Page 79: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/79.jpg)
79
Evaluate:
‘*’
‘-’
‘8’ ‘5’
What infix, prefix, postfix expressions does it represent?
‘/’
‘+’
‘4’
‘3’
‘2’
![Page 80: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/80.jpg)
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’
![Page 81: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/81.jpg)
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
![Page 82: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/82.jpg)
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
![Page 83: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/83.jpg)
83
class ExprTree
‘*’
‘+’
4
3
2
ExprTree
~ExprTree
Build
Evaluate . . .
private:
root
![Page 84: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/84.jpg)
84
What if ExprTree allowed variables?
‘*’
‘+’
‘x’
‘x’
2
ExprTree
~ExprTree
Build
Evaluate . . .
private:
root
![Page 85: CS 132 Spring 2008 Chapter 11](https://reader034.vdocument.in/reader034/viewer/2022051316/568150a2550346895dbea323/html5/thumbnails/85.jpg)
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?