1 Height-Balanced Binary Search Trees AVL Trees. 2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average.

Slides:



Advertisements
Similar presentations
AVL Trees binary tree for every node x, define its balance factor
Advertisements

Part II. Delete an Node from an AVL Tree Consider to delete
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.
Extendible Hashing For Use as a File Structure 1.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
CS202 - Fundamental Structures of Computer Science II
Chapter 10 Efficient Binary Search Trees
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp )
CS2420: Lecture 30 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
1 Red-black Trees zConsider a b-tree of order 4. yA node must have at least 2 children and as many as 4. yA node must have at least 1 key value and as.
CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University.
1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority Queues are similar, except those of highest priority.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
AVL Trees ITCS6114 Algorithms and Data Structures.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get,
External Sorting and Searching
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),
AVL trees. Today AVL delete and subsequent rotations Testing your knowledge with interactive demos!
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
CS Data Structures Chapter 10 Search Structures.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Copyright Curt Hill Balance in Binary Trees Impact on Performance.
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
Data Structures AVL Trees.
AVL Trees It’s a balancing act. Binary Tree Problems If you get either sorted or reverse-sorted input, you essentially get a linked list (always following.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved Balancing Binary Trees There are many approaches to balancing binary trees One method.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
AVL Tree: Balanced Binary Search Tree 9.
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Balanced Binary Search Trees
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CSIT 402 Data Structures II
AVL Trees binary tree for every node x, define its balance factor
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Balanced Binary Search Trees
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
Dynamic Dictionaries Primary Operations: Additional operations:
AVL Tree By Rajanikanth B.
Data Structures Lecture 21 Sohail Aslam.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
INSERT THE TITLE OF YOUR PRESENTATION HERE:
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Presentation transcript:

1 Height-Balanced Binary Search Trees AVL Trees

2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average case search time (like arrays). zProblem: they still have O(n) worst case search time. zSolution: if we can balance the trees on insert, we can have worst case O(log 2 (n)) search time.

3 AVL Trees zNamed for inventors Adelsson, Velski, and Landis. zOne way to maintain a balanced tree. zIdea: keep the height of every node in the tree. zWhen the difference between the height of a node’s left and right subtrees exceeds 1, rotate the nodes to eliminate the imbalance.

4 AVL Details zEach node will have an additional piece of information – its height. It is calculated as: yH(node) = MAX(H(left), H(right)) + 1 zEach node’s balance factor will be calculated when needed; the formula is yBF(node) = H(left) – H(right) zWhen BF(node) = +2 or –2, a fix is needed.

5 AVL Algorithm zInsert a new key value in the normal way yCall the newly inserted node “newnode” zSet H(newnode) = 1; zFor each node from newnode back to root yRecalculate H(node) and BF(node) yIf BF(node) = +2 or BF(node) = –2 xPerform appropriate rotation xOnce a rotation is performed, stop.

6 AVL Rotations zThe appropriate rotation depends on where “newnode” is with respect to the node that is out of balance. zThe four types are yRight-Right (newnode was inserted into the right subtree of the right subtree of the node) yLeft-Left yLeft-Right yRight-Left

7 AVL Rotations II zThe rotations are usually specified in general, since a rotation can occur anywhere in the tree. zSubtrees move as whole units zNote in the following examples that the height of the tree after rotation is the same as before the insertion. zThis means that no heights or balance factors above this node will change.

8 AVL Tree: LL Rotation zThis is the general tree set up. zNow suppose a new node is inserted into B L that increases its height: A B BLBL BRBR ARAR h h H : h+1 BF : 0 H : h+2 BF : +1

9 AVL Tree: LL Rotation zBF of node A is now +2, so a rotation is necessary: A B BLBL BRBR ARAR h h H : h+2 BF : +1 H : h+2 BF : +2 New

10 AVL Tree: LL Rotation zThis is the result of the rotation. zNote the height of the root is the same as before the insertion. A B BLBL BRBR ARAR h h H : h+1 BF : 0 H : h+2 BF : +0 New

11 LL Rotation Algorithm zSet up pointers for A, B, BL, BR, AR zA->left = BR zB->right = A zRoot = B zSet H and BF for nodes A and B. zNOTE: the book does this more “cleverly”, with fewer pointers. I think this is more clear.

12 RR Rotation zThis is the general tree set up. zNow suppose a new node is inserted into B R that increases its height: A B BLBL BRBR ALAL h h H : h+1 BF : 0 H : h+2 BF : -1

13 RR Rotation zBF of node A is –2, so a rotation is needed A B BLBL BRBR ALAL h h H : h+2 BF : -1 H : h+3 BF : -2 New

14 RR Rotation zThis is the result of the rotation. zNote the height of the root returns to h+2 B A BLBL BRBR ALAL h h H : h+2 BF : 0 H : h+1 BF : 0 New

15 RR Rotation Algorithm zSet up pointers for A, B, BL, BR, AL zA->right = BL zB->left = A zRoot = B zSet H and BF for nodes A and B.

16 LR Rotation zThis is the general tree set up. zNow suppose a new node is inserted into C L that increases its height: A B BLBL CLCL h ARAR h H : h+1 BF : 0 H : h+2 BF : +1 C CRCR h–1 H : h BF : 0

17 LR Rotation zThe balance factor of A goes to +2, so a left-right rotation is needed. A B BLBL CLCL h ARAR h H : h+2 BF : -1 H : h+3 BF : +2 C CRCR h–1 H : h+1 BF : +1 New

18 LR Rotation zAgain, note the height of the root returns to h+2. C B BLBL CLCL h ARAR h H : h+1 BF : 0 H : h+2 BF : 0 A CRCR h–1 H : h+1 BF : -1 New

19 LR Rotation Algorithm zSet up pointers for A, B, C, BL, AR, CL, CR zA->left = CR zB->right = CL zC->left = B zC->right = A zRoot = C zSet H and BF for nodes A, B and C.

20 RL Rotation zThis is the general tree set up. zNow suppose a new node is inserted into C L that increases its height: A B CLCL BRBR h H : h+1 BF : 0 H : h+2 BF : -1 C CRCR h–1 H : h BF : 0 ALAL h

21 RL Rotation zBF of Node A is now –2, so a rotation is necessary. A B CLCL BRBR h H : h+2 BF : +1 H : h+3 BF : -2 C CRCR h–1 H : h+1 BF : +1 ALAL h New

22 RL Rotation zThis shows the result of the rotation. AB CLCL BRBR h H : h+1 BF : -1 H : h+2 BF : 0 C CRCR h–1 H : h+1 BF : 0 ALAL h New

23 RL Rotation Algorithm zSet up pointers for A, B, C, BR, AL, CL, CR zA->right = CL zB->left = CR zC->left = A zC->right = B zRoot = C zSet H and BF for nodes A, B and C.

24 RL Rotation Method 2 zFirst, Perform a LL rotation around B: A B CLCL BRBR h H : h+2 BF : +1 H : h+3 BF : -2 C CRCR h–1 H : h+1 BF : +1 ALAL h New

25 RL Rotation Method 2 zNext, perform a RR Rotation around A: A B CLCL BRBR h H : h+1 BF : -1 H : h+3 BF : -2 C CRCR h–1 H : h+2 BF : -1 ALAL h New

26 RL Rotation Method 2 zThis result is the same as the first method. AB CLCL BRBR h H : h+1 BF : -1 H : h+2 BF : 0 C CRCR h–1 H : h+1 BF : 0 ALAL h New

27 AVL Example zNow let’s do an extended example, inserting a series of key values into an AVL tree and rotating as necessary. zWe will be inserting 25, 50, 90, 10, 15, 20, 75. zLet’s start with 25:

28 AVL Example z25 is a new root; no problem. zNow, insert 50: 25 Left to insert: 50, 90, 10, 15, 20, 75 H : 1 BF : 0

29 AVL Example zNo problem here. zNow, insert 90: Left to insert: 90, 10, 15, 20, 75 H : 2 BF : -1 H : 1 BF : 0

30 AVL Example zBF(25) = –2; what do we do? zThis is a RR rotation: Left to insert: 10, 15, 20, 75 H : 3 BF : -2 H : 2 BF : -1 H : 1 BF : 0

31 AVL Example zThis is the result; note height of the root. zNext, insert 10: Left to insert: 10, 15, 20, 75 H : 2 BF : 0 H : 1 BF : 0 H : 1 BF : 0

32 AVL Example zNo problems here. zNext, insert 15: Left to insert: 15, 20, 75 H : 3 BF : +1 H : 2 BF : +1 H : 1 BF : 0 H : 1 BF : 0

33 AVL Example zBF(25)=+2, so time to rotate zThis is a Left-Right (LR) rotation: z(Note that I didn’t update 50) Left to insert: 20, 75 H : 3 BF : +1 H : 3 BF : +2 H : 1 BF : 0 H : 2 BF : -1 H : 1 BF : 0

34 AVL Example zThis shows the result. zNote that I didn’t need to update 50… zNow, insert 20: Left to insert: 20, 75 H : 3 BF : +1 H : 2 BF : 0 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0

35 AVL Example zBF(50) = +2, so time to rotate… zThis is a left right (LR) rotation again: Left to insert: 75 H : 4 BF : +2 H : 3 BF : -1 H : 1 BF : 0 H : 2 BF : +1 H : 1 BF : 0 H : 1 BF : 0

36 AVL Example zThis is the result. Note the movements of the subtrees. zFinally, insert 75: Left to insert: 75 H : 3 BF : 0 H : 2 BF : 0 H : 2 BF : -1 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0

37 AVL Example zH(50) = –2, so time to rotate again. zThis is a right-left (RL) rotation: Left to insert: done H : 3 BF : 0 H : 2 BF : 0 H : 3 BF : -2 H : 1 BF : 0 H : 1 BF : 0 H : 2 BF : +1 H : 1 BF : 0

38 AVL Example zThis is the result. zDone Left to insert: done H : 3 BF : 0 H : 2 BF : 0 H : 2 BF : 0 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0