23
Lab#9 AVL + Heap + Big O 1

Lab#9

  • Upload
    lovie

  • View
    20

  • Download
    0

Embed Size (px)

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

Page 1: Lab#9

1

Lab#9AVL + Heap + Big O

Page 2: Lab#9

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)

Page 3: Lab#9

3

Unbalanced BSTHL-HR = 2

AVL TreeHL-HR=1

HL=1

HR=3

HL=1

HR=2

Page 4: Lab#9

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

Page 5: Lab#9

5

Case1: Left of left

Single Right

rotation

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

SimpleRight

rotation

Page 6: Lab#9

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

Page 7: Lab#9

7

Case2 : Right of right

Simple left

rotation

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

Page 8: Lab#9

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

Page 9: Lab#9

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

Page 10: Lab#9

10

Case4 : Left of right

Rotate right

Page 11: Lab#9

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

Page 12: Lab#9

12

Case4 : Left of right

Rotate Right

Page 13: Lab#9

13

Case4 : Left of right

Rotate Left

Page 14: Lab#9

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

Page 15: Lab#9

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 .

Page 16: Lab#9

16

1530

8020

10

996040

8020

10

50 700

85

not a heap

Heap or Not ?

20

Min Heap

heap

Page 17: Lab#9

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.

Page 18: Lab#9

18

Enqueue

352520

5040

60

10 12

30

15 45

354020

5045

60

10 12

30

15 25

Page 19: Lab#9

19

Dequeue

352520

5040

60

10 12

30

15 19

352520

5040

19

10 12

30

15

Page 20: Lab#9

20

352520

1940

50

10 12

30

15

192520

3540

50

10 12

30

15

Page 21: Lab#9

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.

Page 22: Lab#9

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)

Page 23: Lab#9

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)