Download presentation

Presentation is loading. Please wait.

Published byEmmalee Tunnell Modified over 2 years ago

1
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

2
Balanced Binary Search Tree In Binary Search Tree Average and maximum search times will be minimized when BST is maintained as a complete tree at all times. : O (lg N) If not balanced, the search time degrades to O(N) Idea : Keep BST as balanced as possible

3
AVL tree "height-balanced“ binary tree the height of the left and right subtrees of every node differ by at most one 3 25 6 47 3 5 29 14 3 12 14 7 8 11 10

4
Non-AVL tree example 6 49 357 8

5
Balance factor BF = (height of right subtree - height of left subtree) So, BF = -1, 0 or +1 for an AVL tree. 0 00 0 0 +1 -2 0+1 0

6
AVL tree rebalancing When the AVL property is lost we can rebalance the tree via one of four rotations Single right rotation Single left rotation Double left rotation Double right rotation

7
Single Left Rotation (SLR) when A is unbalanced to the left and B is left-heavy A BT3 T1T2 B T1A T2T3 SLR at A

8
Single Right Rotation (SRR) when A is unbalanced to the right and B is right-heavy A T1B T2T3 B A T1T2 SRR at A

9
Double Left Rotation (DLR) When C is unbalanced to left And A is right heavy C AT4 T1B T2T3 C BT4 AT3 T1T2 B AC T1T2T3T4 SLR at A SRR at C

10
Double Right Rotation (DRR) When C is unbalanced to right And A is left heavy A T1B T2C T3T4 B AC T1T2T3T4 A T1C BT4 T2T3 SRR at CSRR at A

11
Insertion in AVL tree An AVL tree may become out of balance in two basic situations After inserting a node in the right subtree of the right child After inserting a node in the left subtree of the right child

12
insertion Insertion of a node in the right subtree of the right child Involves SLR at node P

13
insertion Insertion of a node in the left subtree of the right child Involves DRR :

14
insertion In each case the tree is rebalanced locally insertion requires one single or one double rotation O(constant) for rebalancing Cost of search for insert is still O(lg N) Experiments have shown that 53% of insertions do not bring the tree out of balance

15
Deletion in AVL tree Not covered here Requires O(lg N) rotations in worst case

16
example Given the following AVL tree, insert the node with value 9: 6 312 4 713 10

Similar presentations

OK

CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.

CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on career planning for mba students Ppt on indian army weapons images Ppt on area of parallelogram formula Ppt on model view controller design Download ppt on mind controlled robotic arms Ppt on work and energy class 9 Ppt online form in mp Ppt on trial and error synonyms Ppt on save environment download Ppt on natural resources and conservation service