1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

43
1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

Transcript of 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

Page 1: 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.

Page 2: 1 CompSci 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

Page 3: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

3

An Aside: Iterators

• The Iterator interface abstracts this concept for us

Interface: Iterator

boolean hasNext()

Object next()

void remove() (optional)

Page 4: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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){...}

Page 5: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 6: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

6

Heap Implementation

U

J

G

Q

MB

As a tree

Page 7: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

7

Binary Trees in Arrays?

M

Q

UO

G

JB

0

1 2

3 4 5 6

As a tree

Page 8: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 9: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

9

Other Implementations of Trees

• Multiple array implementation– Easy– left and right child references are indices to

array– Does not require following object

references

Page 10: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 11: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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...

Page 12: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 13: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 14: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

14

• Return the item in the root

• Produce a semi-heap

• Transform the semi-heap into a Heap

Page 15: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

15

• Return the item in the root

• Produce a semi-heap

• Transform the semi-heap into a Heap

Page 16: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 17: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 18: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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)

Page 19: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

19

Heap Insert Operation

Page 20: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 21: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 22: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 23: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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.

Page 24: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

24

Rotations

• Note that rotation operation can make certain children “parentless”

• These children/subtrees must be inserted into tree at appropriate locations

Page 25: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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??

Page 26: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

26

Left Rotation???

Homework exercise...

Page 27: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 28: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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.

Page 29: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

29

Splaying a nodeCase 1: The parent of x is root =>perform a single rotation

Page 30: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 31: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 32: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

32

Splaying a node

Example: Splay the tree around node 5

Page 33: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

33

Intuitive method...

Always rotate node toward the root.

Page 34: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

34

Efficiency??

What is the efficiency of splaying a node to the root?

How often will this happen?

Page 35: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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)

Page 36: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 37: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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?

Page 38: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

38

Page 39: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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

Page 40: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

40

Rotations to obtain AVL property

• Case 0: No rotation needed

Page 41: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

41

Rotations to obtain AVL property

• Case 1: Single rotation to the Left (Right) to obtain a balanced shape

Page 42: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

42

Rotations to obtain AVL property

• Case 2: Double Right-Left (Left-Right) rotation to obtain a balanced shape

Page 43: 1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.

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