1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.
-
Upload
raymond-fowler -
Category
Documents
-
view
219 -
download
0
Transcript of 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.
1CompSci 105 SS 2006
Principles of Computer Science
Lecture 17: Heaps cont.
2
An Aside: Iterators
• Quite often we want to get data in a certain order
• We want the data to come one at a time at our leisure
• We do not care how it gets the data, as long as it is in the order we expect
3
An Aside: Iterators
• The Iterator interface abstracts this concept for us
Interface: Iterator
boolean hasNext()
Object next()
void remove() (optional)
4
An Aside: Iterators
• Writing an Iterator is simple• Mostly return these from various
methodse.g. BSTpublic Iterator getPostOrderTraversal(){...}-or-public static Iterator
getPreOrderTraversal(BinaryTree b){...}
5
An Aside: Iterators
• How the Iterator arrives at the next node is completely up to the writer
• The user has no idea
• Turns any data structure into a sorted list
6
Heap Implementation
U
J
G
Q
MB
As a tree
7
Binary Trees in Arrays?
M
Q
UO
G
JB
0
1 2
3 4 5 6
As a tree
8
Binary Trees in Arrays!
M
Q
UO
G
JB
0
1 2
3 4 5 6
0 M
1 G
2 Q
3 B
4 J
5 O
6 U
As a tree As an array
9
Other Implementations of Trees
• Multiple array implementation– Easy– left and right child references are indices to
array– Does not require following object
references
10
Array implementation
0 U 1 2
1 Q 3 4
2 J 5 -1
3 B -1 -1
4 M -1 -1
5 G -1 -1
6
U
J
G
Q
MB
11
Other Implementations of Trees
• Single array implementation– most memory and speed efficient– Does not require references (i.e.
compatible with almost all languages)– tree is stored in level order in array– “Missing” children are null– Use formulae to find children or parent...
12
Array implementation
• Given node at index ‘i’ in the array...
parentIndex = (i-1)/2;
rightChild = 2*i+1;
leftChild = 2*i+2;
0 U
1 Q
2 J
3 B
4 M
5 G
6 null
U
J
G
Q
MB
13
Heap Delete Operation
• Retrieves and then deletes a Heap’s root item– Return the item in the root– Produce a semi-heap– Transform the semi-heap into a Heap
14
• Return the item in the root
• Produce a semi-heap
• Transform the semi-heap into a Heap
15
• Return the item in the root
• Produce a semi-heap
• Transform the semi-heap into a Heap
16
• Return the item in the root
• Produce a semi-heap
• Transform the semi-heap into a Heap– Allows the item in the root to trickle down
to its right place– Compare the search key in the root with
that of its children's– Swap the item in the root with that of the
larger child
17
Transform the semi-heap into a Heap– Allows the item in the root to trickle down
to its right place– Compare the search key in the root with
that of its children's– Swap the item in the root with that of the
larger child
18
Heap Insert Operation
• Insert a new item into a Heap– New item is inserted at the bottom of the tree– Then it trickled up to its proper place
• Compare the search key in current node with that of its parent
• Swap the current node with its parent, if the current node has greater value
• The efficiency of a Heap Insert and Heap Delete is O(log n)
19
Heap Insert Operation
20
Heap insertheap[size] = newItem;
child=size; parent=(size-1)/2;
while ( parent>=0 && heap[child]>heap[parent] ) {
temp = heap[parent] ;
heap[parent] = heap[child] ;
heap[child] = temp ;
child=parent; parent=(parent-1)/2;
}
0 U
1 Q
2 J
3 B
4 M
5 G
6
size = 6
newItem = K
21
• Heaps remain complete and efficient
• BST’s may not
• BUT – Heaps have restrictions on use
• How to make BST more balanced without restricting its use?
Back to Trees
22
Balanced Trees
• Obviously need to sacrifice some efficiency to keep balanced
• But if can guarantee tree is balanced, make search very efficient
• Two methods:– Splay Trees– AVL trees
23
Tree Rotations
• A rotation replaces the root of a subtree with one of its children.
• Rotation is an important operation for maintaining the balance of a Binary Search Tree.
24
Rotations
• Note that rotation operation can make certain children “parentless”
• These children/subtrees must be inserted into tree at appropriate locations
25TreeNode rotateRight(TreeNode p) {if (p == null)
return null;else if (p.getLeft() == null)
return p; else {
TreeNode newRoot = p.getLeft();p.setLeft(newRoot.getRight());newRoot.setRight(p);return newRoot;}
} Question:
Why can X’s child, B, always be made P’s left child??
26
Left Rotation???
Homework exercise...
27
Splay Trees• A Splay Tree is a BST that rearranges its
nodes during the access of nodes• Splay – whenever a node is added, removed
or retrieved, move the referenced node to become the new root of the Tree– In the case of removal, splay the tree around the
parent node• The average depth of those nodes to the root
is halved, more close to the top of the tree and more efficiently accessed
• Can guarantee the O(log2n) performance
28
Splaying a node• Case 0: If x is the root, we are done.• Case 1: If x is a left/right child of the root, rotate the tree
to the right/left about the root. x becomes the root and we are done.
• Case 2a: If x is the left child of its parent p, and p is the left child of its grandparent g, rotate first right about g, followed by a right rotation about p. This is called a double-right rotation. If x is the right child of a right child perform a double-left rotation. After the double rotation, continue splay at x in the new tree.
• Case 2b: If x is the right child of its parent p, and p is the left child of its grandparent g, we rotate first left around p and then right around g. This is called a left-right double rotation. If x is the left child of a right child perform a right-left double rotation. After the double rotation, continue splay at x in the new tree.
29
Splaying a nodeCase 1: The parent of x is root =>perform a single rotation
30
Splaying a nodeCase 2a: The parent of x is not the root and it is the left (right)
child of a left(right) child => perform a double-right (double-left) rotation
31
Splaying a nodeCase 2b: The parent of x is not the root and it is the left
(right) child of a right (left) child => perform a right-left (left-right) double rotation
32
Splaying a node
Example: Splay the tree around node 5
33
Intuitive method...
Always rotate node toward the root.
34
Efficiency??
What is the efficiency of splaying a node to the root?
How often will this happen?
35
AVL Trees
• The AVL tree is named after its two inventors, G.M. Adelson-Velsky and E.M. Landis
• Keeps a BST tree mostly balanced so that all operations remain O(log2n)
36
AVL Trees• An AVL Tree is a Binary Search Tree that the heights of
the left and right subtrees of any node differ by no more than 1 :
| HeightLeft – HeightRight | ≤1• Operations on an AVL tree is almost as efficiently as a
minimum-height Binary Search Tree• After each insert or delete, check whether the tree is still
an AVL Tree; if not, restore the property by tree rotations
37
Efficiency??
What is the efficiency of checking the height of the left and right subtree of the
root of a tree??
How often will this happen?
38
39
Rotations to obtain AVL property
• Case 0: No rotation needed
• Case 1: Single rotation to the Left (Right) to obtain a balanced shape
• Case 2: Double Right-Left (Left-Right) rotation to obtain a balanced shape
40
Rotations to obtain AVL property
• Case 0: No rotation needed
41
Rotations to obtain AVL property
• Case 1: Single rotation to the Left (Right) to obtain a balanced shape
42
Rotations to obtain AVL property
• Case 2: Double Right-Left (Left-Right) rotation to obtain a balanced shape
43
Comparison
Splay Trees• Slowest
• Average=worst=best• Commonly
accessed nodes near root
• All operations need rotation
AVL Trees• Trade off between
two• Average=worst=best• Nodes randomly
distributed
• Only some add/delete need rotation
BST Trees• Fastest if balanced,
otherwise slowest • Average!=worst!=best• Nodes randomly
distributed
• No rotation