• No results found

AVL Trees 2.pdf

N/A
N/A
Protected

Academic year: 2020

Share "AVL Trees 2.pdf"

Copied!
81
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Degenerate Binary Search Tree

BST for 14, 15, 4, 9, 7, 18, 3, 5, 16, 20, 17

14

15 4

9

7

18 3

5

16 20

(3)

Degenerate Binary Search Tree

BST for 3 4 5 7 9 14 15 16 17 18 20

14

15 4

9 7

18 3

5

16

(4)

Degenerate Binary Search Tree

BST for 3 4 5 7 9 14 15 16 17 18 20

(5)

Balanced BST

—

We should keep the tree balanced.

—

One idea would be to have the left and

(6)

Balanced BST

Does not force the tree to be shallow.

14

15

18 16

20 17

4

9 7

3

(7)

Balanced BST

—

We could insist that every node must have left

and right subtrees of same height.

—

But this requires that the tree be a complete

binary tree

—

To do this, there must have (2d+1 – 1) data

(8)

AVL Tree

—

AVL (Adelson-Velskii and Landis) tree.

—

Purpose: AVL is a self-balancing binary tree

—

This was an effort to propose the development

of a balanced search tree by considering the height as a standard.

—

An AVL tree is identical to a BST except

§

height of the left and right subtrees can differ by

at most 1.

(9)

AVL Tree

—  An AVL Tree

5

8 2

4

3

1 7

level 0

1

2

(10)

AVL Tree

—

Not an AVL tree

6

8 1

4

3 1

5

level 0

1

2

(11)

Balanced Binary Tree

—

The height of a binary tree is the maximum level

of its leaves (also called the depth).

—

The balance of a node in a binary tree is defined

as the height of its left subtree minus height of its right subtree.

—

For example, a tree is a balanced tree if each

(12)

Balanced Binary Tree

-1

0 1

0 0

0

0

-1 0

1

0

0 0

0 0

0 0

(13)

Balanced Binary Tree

Insertions and effect on balance

-1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0

U1 U2 U3 U4

U5 U6 U7 U8 U

9 U10 U11 U12

B B

B B

(14)

Balanced Binary Tree

—

Tree becomes unbalanced only if the newly

inserted node

§

is a left descendant of a node that previously

had a balance of 1 (U1 to U8),

§

or is a descendant of a node that previously had

(15)

Balanced Binary Tree

Insertions and effect on balance

-1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0

U1 U2 U3 U4

U5 U6 U7 U8 U

9 U10 U11 U12

B B

B B

(16)

Balanced Binary Tree

Consider the case of node that was previously 1

-1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0

U1 U2 U3 U4

U5 U6 U7 U8 U

9 U10 U11 U12

B B

B B

(17)

Inserting New Node in AVL Tree

1

0

A

B

T1

T3

(18)

Inserting New Node in AVL Tree

2

1

A

B

T1

T3

T2

new

1

(19)

Inserting New Node in AVL Tree

2 1 A B T1 T3 T2 new 1 2 0 0 A B T1 T3 T2 new

(20)

AVL Tree Building Example

—

Let us work through an example that inserts

numbers in a balanced search tree.

—

We will check the balance after each insert

(21)

AVL Tree Building Example

Insert(1)

(22)

AVL Tree Building Example

Insert(2)

1

(23)

AVL Tree Building Example

Insert(3) single left rotation

1

2

3

(24)

AVL Tree Building Example

Insert(3) single left rotation

1

2

3

(25)

AVL Tree Building Example

Insert(3)

1

2

(26)

26

AVL Tree Building Example

Insert(4)

1

2

3

(27)

27

AVL Tree Building Example

Insert(5)

1

2

3

4

5

(28)

28

AVL Tree Building Example

Insert(5)

1

2

3

4

(29)

29

AVL Tree Building Example

Insert(6)

1

2

3

4

5

6

(30)

30

AVL Tree Building Example

Insert(6)

1

2

3 4

5

(31)

31

AVL Tree Building Example

Insert(7)

1

2

3 4

5

6

7

(32)

32

AVL Tree Building Example

Insert(7)

1

2

3 4

5

6

(33)

33

AVL Tree Building Example

1

2

3 4

5

6

7

(34)

34

AVL Tree Building Example

Insert(15)

1

2

3 4

5

6

7

16

15

(35)

35

AVL Tree Building Example

Insert(15)

1

2

3 4

5

6

7 16

15

(36)

36

Cases for Rotation

—

Single rotation does not seem to restore the

balance.

—

The problem is the node 15 is in an inner

subtree that is too deep.

(37)

37

Cases for Rotation

—

Let us call the node that must be rebalanced α .

—

Since any node has at most two children, and a

height imbalance requires that α’s two subtrees

differ by 2 (or –2), the violation will occur in four cases:

1.

An insertion into left subtree of the left child of α

2.

An insertion into right subtree of the left child of α

3.

An insertion into left subtree of the right child of α

4.

An insertion into right subtree of the right child

(38)

38

Cases for Rotation

Outside insertion:

—

The insertion occurs on the “outside” (i.e.,

left-left or right-right) in cases 1 and 4

—

Single rotation can fix the balance in cases 1

and 4.

Inside insertion:

—

Insertion occurs on the “inside” in cases 2

(39)

39

Cases for Rotation

Single right rotation to fix case 1. (LL)

—

How to add a new node as a child of node X.

(40)

40

Cases for Rotation

Single left rotation to fix case 4. (RR)

—

Similarly, How to add a new node as a child of

node X.

Level n Level n-1 Level n-2

k1

k2 X

Y

Z

k1

k2

(41)

41

Cases for Rotation

Single right rotation fails to fix case 2. (RL)

—

How about if the new node a child of node Y

k1 k2 Z X Level n Level n-1 Level n-2 k1 k2 Z X

α α

new

Y

new

(42)

42

Cases for Rotation

—

Y is non-empty because

the new node was inserted in Y.

—

Thus Y has a root and two

subtrees.

—

View the entire tree with

four subtrees connected with 3 nodes.

k1

k2

X

Y

(43)

43

Cases for Rotation

—

Y is non-empty because

the new node was inserted in Y.

—

Thus Y has a root and two

subtrees.

—

View the entire tree with

four subtrees connected with 3 nodes.

k1

k3

D

A

B C

(44)

44

Cases for Rotation

k3 k1 A new 1 2

New node inserted a either of the two spots

B C

k2

D

new

§  Exactly one of tree B or C

is two levels deeper than

D; we are not sure which

one.

§  Good thing: it does not

matter.

(45)

45

Cases for Rotation

k3 k1 A new 1 2

New node inserted a either of the two spots

B C

k2

D

new

§

A rotation between

k

3

and

k

1

(

k

3

was

k

2

then)

was shown to not work.

§

The only alternative is

to place

k

2

as the new

root.

§

This forces

k

1

to be

(46)

46

Cases for Rotation

—

Left-right

double

rotation to fix case 2.

New node inserted a either of the two spots

(47)

47

Cases for Rotation

—

Left-right

double

rotation to fix case 2.

A new B D C newA new B D C newk3 k2 k1

Rotate right k2

(48)

48

Cases for Rotation

—

Right-left

double

rotation to fix case 3 (LR)

k1

k3

D A

B C

k2

k1

k3

D A

B

C k2

(49)

49

Cases for Rotation

—

Right-left

double

rotation to fix case 3.

k1

k2

D

A B C

k3 k1

k3

D A

B

C k2

(50)

50

AVL Tree Building Example

Insert(15)

1

2

3 4

5

6

X


(null)

Z


(null) Y

15

k1

k2

7

(51)

51

AVL Tree Building Example

Insert(15) right-left double rotation

1

2

3 4

5

6

7

16

15

k1

k3

k2

(52)

52

AVL Tree Building Example

Insert(15) right-left double rotation

1

2

3 4

5

6

7

16 15

k1

k3 k2

(53)

53

AVL Tree Building Example

Insert(15) right-left double rotation

1

2

3 4

5

6

16

k3

15

k2

7

(54)

54

AVL Tree Building Example

Insert(14): right-left double rotation

1

2

3 4

5

6

16

k3

15

k2

7

k1

14

(55)

55

AVL Tree Building Example

Insert(14): right-left double rotation

1

2

3 4

5

6

16

k3

15

k2

7

k1

14

(56)

56

AVL Tree Building Example

Insert(14): right-left double rotation

1

2

3 4

5 16

k3

15

k2

7

6

k1

(57)

57

AVL Tree Building Example

Insert(13): single rotation

1

2

3 4

5 16

15 7

6

14

13

(58)

58

AVL Tree Building Example

Insert(13): single rotation

1

2

3 4

5

16 15

7

6 14

(59)

59

Deletion in AVL Tree

—

Delete is the inverse of insert: given a value X

and an AVL tree T, delete the node containing X and rebalance the tree, if necessary.

—

Turns out that deletion of a node is

(60)

60

Deletion in AVL Tree

—

Insertion in a height-balanced tree requires at

most one single rotation or one double rotation.

—

We can use rotations to restore the balance

when we do a deletion.

—

We may have to do a rotation at every level of

(61)

61

Deletion in AVL Tree

—

Here is a tree that causes this worse case

number of rotations when we delete A. At every node in N’s left subtree, the left subtree is one

shorter than the right subtree.

A C

D

N

E J

G

I F

H

K L

(62)

62

Deletion in AVL Tree

—

Deleting A upsets balance at C. When rotate (D

up, C down) to fix this

A C

D

N

E J

G

I F

H

K L

(63)

63

Deletion in AVL Tree

—

Deleting A upsets balance at C. When rotate (D

up, C down) to fix this

C

D

N

E J

G

I F

H

K L

(64)

64

Deletion in AVL Tree

—  The whole of F’s left subtree gets shorter. We fix this

by rotation about F-I: F down, I up.

C

D

N

E

J G

I F

H

K L

(65)

65

Deletion in AVL Tree

—  The whole of F’s left subtree gets shorter. We fix this by

rotation about F-I: F down, I up.

C

D

N

E

J G

I F

H

K L

(66)

66

Deletion in AVL Tree

—

This could cause imbalance at N.

—

The rotations propagated to the root.

C

D

N

E

J G

I

F

H

K

L

(67)

67

Deletion in AVL Tree

Procedure

—

Delete the node as in binary search tree (BST).

—

The node deleted will be either a leaf or have

just one subtree.

—

Since this is an AVL tree, if the deleted node has

one subtree, that subtree contains only one node (why?)

—

Traverse up the tree from the deleted node

(68)

68

Deletion in AVL Tree

—

There are 5 cases to consider.

—

Let us go through the cases graphically and

(69)

69

Deletion in AVL Tree

Case 1a: The parent of the deleted node had a balance of 0 and the node was deleted in the parent’s left subtree.

Action: change the balance of the parent node and

stop. No further effect on balance of any higher node.

(70)

70

Deletion in AVL Tree

Here is why; the height of left tree does not change.

How about if we delete(1). The parent is 2 with balance of 0.

1

2

3 4

5

6

7

0

1

(71)

71

Deletion in AVL Tree

1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)

Here is why; the height of left tree does not change.

(72)

72

Deletion in AVL Tree

Case 1b: the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s right subtree.

Action: (same as 1a) change the balance of the

parent node and stop. No further effect on balance of any higher node.

(73)

73

Deletion in AVL Tree

Here is why; the height of left tree does not change.

How about if we delete(7). The parent is 2 with balance of 0.

1

2

3 4

5

6

7

0

1

(74)

74

Deletion in AVL Tree

1 2 3 4 5 6 7 2 3 4 5 6 0 1 2 Remove(7)

Here is why; the height of left tree does not change.

How about if we delete(7). The parent is 2 with balance of 0.

(75)

75

Deletion in AVL Tree

Case 2b: the parent of the deleted node had a balance of –1 and the node was deleted in the parent’s right subtree.

Action: same as 2a: change the balance of the parent

node. May have caused imbalance in higher nodes so continue up the tree.

(76)

76

Deletion in AVL Tree

Case 3a: the parent had balance of –1 and the node was deleted in the parent’s left subtree,

(77)

77

Deletion in AVL Tree

Case 3a: the parent had balance of -1 and the node was deleted in the parent’s left subtree,

right subtree was balanced.

Action: perform single rotation, adjust balance. No

effect on balance of higher nodes so stop here.

(78)

78

Deletion in AVL Tree

Case 4a: parent had balance of –1 and the node was deleted in the parent’s left subtree, right

(79)

79

Deletion in AVL Tree

Case 4a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right

subtree was unbalanced.

Action: Double rotation at B. May have effected the balance of higher nodes, so continue up the tree.

(80)

80

Deletion in AVL Tree

Case 5a: parent had balance of –1 and the node was deleted in the parent’s left subtree, right

(81)

81

Deletion in AVL Tree

Case 5a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right

subtree was unbalanced.

Action: Single rotation at B. May have effected the

balance of higher nodes, so continue up the tree. rotate

References

Related documents

Accepts Out of District In District Only Website: https://essdack.org/LC/thomas-county-learning-center.html Curriculum Used: Odysseyware Registration Deadline: None Partner

AVL condition: For every node, the height of its left subtree and right subtree differ by at most 1... How Long Does

As proved in Lemma 1, the right subtree of a tree node T in the Kd-tree can be eliminated when no matching filter or marker is found in T : In contrast, the left subtree cannot

Keys in left subtree v.left are smaller than v.key Keys in right subtree v.right are greater than

• The addition occurred in the left subtree of N's right child (left-right rotation) or ….. • The addition occurred in the right subtree of N's left child

This essay describes the views of Philippines livestock sector stakeholders concerning the events and issues associated with the rapid rise in hog and poultry production, based

insecurity, hunger and famines (UNECA, 2017). In most countries, pastoral populations are a minority and are located in the periphery zones and areas that are difficult to

To append data, a client first determines the next available position in the shared log – using a sequencer node as an optimization for avoiding contention with other appending