Lab#9

Preview:

DESCRIPTION

Lab#9. AVL + Heap + Big O. AVL Tree. Height-balanced binary search tree where the heights of sub-trees differ by no more than 1: | H L – H R |

Citation preview

1

Lab#9AVL + Heap + Big O

2

AVL Tree• Height-balanced binary search tree where the heights of

sub-trees differ by no more than 1:| HL – HR | <= 1

• Each node has a balance factor• Balance factors may be:

– Left High (LH) = +1 (left sub-tree higher than right sub-tree)

– Even High (EH) = 0 (left and right sub-trees same height)

– Right High (RH) = -1 (right sub-tree higher than left sub-tree)

3

Unbalanced BSTHL-HR = 2

AVL TreeHL-HR=1

HL=1

HR=3

HL=1

HR=2

4

Balancing Trees• Insertions and deletions potentially cause a tree to be

imbalanced• When a tree is detected as unbalanced, nodes are

balanced by rotating nodes to the left or right• Four imbalance cases:

▫ Left of left: the sub-tree of a left high tree has become left high

▫ Right of right: the sub-tree of a right high tree has become right high

▫ Right of left: the sub-tree of a left high tree has become right high

▫ Left of right: the sub-tree of a right high tree has become left high

• Each imbalance case has simple and complex rotation cases

5

Case1: Left of left

Single Right

rotation

Rotate the out of balance node (the root) to the right

SimpleRight

rotation

6

Case1: Left of left

18

12

8

4

20

14

Insert 4

12

8

4

18

1420Complex

Right rotation

•Rotate root to the right, so the old root is the right sub-tree of the new root; the new root's right sub-tree is connected to the old root's left sub-tree

7

Case2 : Right of right

Simple left

rotation

Rotate the out of balance node (the root) to the left

8

Case2 : Right of right

Complex left

rotation

Rotate root to the left, so the old root is the left sub-tree of the new root; the new root's left sub-tree is connected to the old root's right sub-tree

9

Case3 : Right of left

11

7

5

11

5

7

7

115

Insert 7

Right rotation

left rotation

• Simple double rotation right: Rotate left sub-tree to the left; rotate root to the right, so the old root's left node is the new root

10

Case4 : Left of right

Rotate right

11

Case4 : Left of right

Rotate Left

Rotate right sub-tree to the right; rotate root to the left, so the old root's right node is the new root

12

Case4 : Left of right

Rotate Right

13

Case4 : Left of right

Rotate Left

14

HW:The following keys are inserted (in the order given) into an initially empty AVL tree.Show the AVL tree after each insertion.24,39,31,46,48,34,19,5,29

15

Heap•A particular kind of binary tree, called a heap It has two properties:

1. The value of each node is greater(max heap)/less (min heap) than or equal to the value stored in each of its children.

2. The tree is perfectly balanced, and the leaves in the last level are all in the leftmost positions.

• Heaps can be implemented by arrays.• Elements in a heap are not perfectly

ordered. There is no relation between siblings .

16

1530

8020

10

996040

8020

10

50 700

85

not a heap

Heap or Not ?

20

Min Heap

heap

17

Heaps as Priority Queues

•A heap is a way to implement priority queues.Two procedures has to be implemented :1. Enqueue an element : the element is added at the end of

the heap as the last leaf .

2. Dequeue an element : remove the root element from the heap.

18

Enqueue

352520

5040

60

10 12

30

15 45

354020

5045

60

10 12

30

15 25

19

Dequeue

352520

5040

60

10 12

30

15 19

352520

5040

19

10 12

30

15

20

352520

1940

50

10 12

30

15

192520

3540

50

10 12

30

15

Algorithm Efficiency

•Algorithm efficiency▫A function of the number of elements to be

processed f(n)=efficiency

▫If a function is linear – that is, if it contains no loop – then its efficiency is a functions of the number of instructions it contains.

▫On the other hand, functions that loop vary widely in their efficiency.

22

What is the Big-O complexity of each of the following methods? void q1A(int n) { int count = 0; for(int i=0; i<20000; i++) count++; }

void q1B(int n) {int count = 0;for(int i=0; i<n; i++)for (int j=0; j<100; j++)count++;}

void q1C(int n) { int count = 0; for(int i=0; i<n; i++) count++; for (int j=0; j<n; j++) count++; }

O(n)

O(n^2)

O(n)

23

void q1F(int[] a) { int result = 0; int n = a.length; for (int i=0; i < n; i++) result = doG( a[i]); } } int doG(int a) { int result = 0; for (int i=0; i < a; i++) { result += i; } return result; }

O(n^2)