Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures AVL Trees.

Similar presentations


Presentation on theme: "Data Structures AVL Trees."— Presentation transcript:

1 Data Structures AVL Trees

2 Balanced BST strong enough! is easy to maintain Observation
BST: the shallower the better! For a BST with n nodes Average height is O(log n) Worst case height is O(n) Simple cases such as insert(1, 2, 3, ..., n) lead to the worst case scenario: height O(n) Solution: Require a Balance Condition that ensures depth is O(log n) is easy to maintain strong enough!

3 The AVL Tree Data Structure
Adelson-Velskii and Landis An AVL tree is: A binary search tree Heights of left and right subtrees of every node differ by at most 1 8 5 11 2 6 10 12 4 7 9 13 14 15

4 Recursive Height Calculation
Recall: height is max number of edges from root to a leaf A hright hleftt How do we compute height at A? Note: height(null) = -1

5 AVL Tree? 6 3 4 1 8 2 1 7 11 1 12 10

6 AVL Tree? 4 6 3 4 8 1 2 1 5 0 7 11 3 1 2

7 An AVL Tree has Height O(log n)
Proof Let S(h) be the min # of nodes in an AVL tree of height h AVL tree of height h=4 with the min # of nodes (12) 8 4 Claim: S(h) = S(h-1) + S(h-2) + 1 5 2 11 3 Solution of recurrence: S(h) = O(2h) (like Fibonacci numbers) 2 6 1 10 1 12 2 7 0 0 15

8 Testing the Balance Property
We need to be able to: 10 1. Track Balance 5 15 2. Detect Imbalance 2 9 20 3. Restore Balance 7 17 30 NULL has a height of -1

9 An AVL Tree 10 3 10 5 20 15 30 2 9 17 7 data height children 3 2 2 1 1
30 2 9 17 7 Track height at all times.

10 AVL trees: find, insert, delete
AVL find: Same as BST find. AVL insert: Same as BST insert, except you need to check your balance and may need to “fix” the AVL tree after the insert. AVL delete: We’re not going to talk about it, but same basic idea. Delete it, check your balance, and fix it.

11 Bad Case #1 Insert(6) Insert(3) Insert(1) 6 3 1 Simple illustration

12 Fix: Apply Single Rotation
AVL Property violated at this node (x) 2 1 6 3 1 3 1 6 1 Intuition: 3 must become root Single Rotation: 1. Rotate between x and child Simple illustration

13 Bad Case #2 Insert(1) Insert(6) Insert(3) 1 6 3

14 Single Rotation Does Not Work
AVL Property violated at this node (x) It’s a BST, but it’s unbalanced 2 2 1 1 1 6 1 3 6 3

15 Fix: Apply Double Rotation
AVL Property violated at this node (x) 2 2 1 1 1 3 1 1 3 6 1 6 3 6 Double Rotation 1. Rotate between x’s child and grandchild 2. Rotate between x and x’s new child

16 AVL tree insert Find spot for new key
Hang new node there with this key Search back up the path for imbalance If there is an imbalance: Case #1: Perform single rotation and exit Case #2: Perform double rotation and exit Both rotations keep the subtree height unchanged. Hence only one rotation is sufficient!

17 Case #1: Single Rotation
h+3 a h+2 h b h+1 h Z X Y single rotation X < b < Y < a < Z b a X Y Z

18 Single rotation example
15 8 22 4 10 19 24 3 6 17 20 16

19 Single rotation example
15 8 22 4 10 19 24 3 6 17 20 16

20 Single rotation example
15 8 22 4 10 19 24 3 6 17 20 16 15 8 19 4 10 17 22 16 3 6 20 24

21 Case #2: Double Rotation
h+3 a h+2 b h h+1 h c h-1 h U Z X V Insert on left child’s right (at U or V) Let’s break subtree into pieces: c

22 Case #2: Double Rotation
h+3 a h+2 b h h+1 h c h-1 h U Z X V Insert on left child’s right (at U or V) Let’s break subtree into pieces: c b

23 Case #2: Double Rotation
h+3 a h+2 b h h+1 h c h-1 h U Z X V Insert on left child’s right (at U or V) c a Let’s break subtree into pieces: b U V X Z

24 Can also do this in two rotations
b h+1 h c h U h-1 Z X V First rotation X < b < U < c < V < a < Z a c h h-1 b h Z h V

25 Second rotation a c b Z V U X Second rotation c b a U V X Z h+3 h+2 h

26 Double rotation example
15 8 19 4 10 17 22 3 6 16 20 24 5

27 Double rotation example
15 8 19 4 10 17 22 3 6 16 20 24 5

28 Double rotation example
15 8 19 4 10 17 22 3 6 16 20 24 5

29 Double rotation example
15 8 19 4 10 17 22 3 6 16 20 24 5 15 8 19 10 17 22 16 20 24

30 Double rotation example
15 8 19 4 10 17 22 3 6 16 20 24 5 15 8 19 6 10 17 22 4 16 20 24 3 5

31 Double rotation example
15 8 19 6 10 17 22 4 16 20 24 3 5 15 6 19 4 8 17 22 3 5 10 16 20 24

32 Case #3: a b c X Z U V Double rotation c a b U V Z X h+3 h+2 h h+1 h h

33 Case #4: a b X Y Z Single rotation b a Z X Y h+3 h+2 h h h+1 h+2 h+1

34 Recap of AVL tree insert
Let x be the node where an imbalance occurs. Four cases to consider. The insertion is in the left subtree of the left child of x. right subtree of the left child of x. left subtree of the right child of x. right subtree of the right child of x. Cases 1 & 4 are solved by a single rotation Cases 2 & 3 are solved by a double rotation

35 AVL complexity O(log n) O(n log n)
What is the worst case complexity of a find? O(log n) What is the worst case complexity of an insert? What is the worst case complexity of build a AVL Tree? O(n log n)


Download ppt "Data Structures AVL Trees."

Similar presentations


Ads by Google