Lecture 15 Nov 3, 2013 Height-balanced BST Recall:

Slides:



Advertisements
Similar presentations
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Advertisements

AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Tirgul 5 AVL trees.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CSE 326: Data Structures AVL Trees
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.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
AVL Trees ITCS6114 Algorithms and Data Structures.
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.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Data Structures AVL Trees.
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.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
AVL Tree: Balanced Binary Search Tree 9.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
AA Trees.
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Balancing Binary Search Trees
UNIT III TREES.
Red Black Trees
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
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.
CS 201 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
Chapter 6 Transform and Conquer.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees.
Data Structures Lecture 21 Sohail Aslam.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Lecture 15 Nov 3, 2013 Height-balanced BST Recall: Binary Search Tree can be used to perform dictionary operations (search, insert and delete) in O(h) time. h = height of tree is usually much smaller than the size n of the tree (h << n), but in the worst-case, h = n – 1. When the tree is built with a random sequence of keys, height h = O(log n). (Note: best case h = O(log n). With AVL tree and red-black tree, h is guaranteed to be O(log n) in the worst-case.

Randomly generated binary search tree

Balanced Binary Search Tree Worst case height of binary search tree: N-1 Insertion, deletion can be O(N) in the worst case We want a tree with small height Height of a binary tree with N node is at least O(log N) Complete binary tree has height log N. Goal: keep the height of a binary search tree O(log N) Balanced binary search trees Examples: AVL tree, red-black tree

AVL tree for each node, the height of the left and right subtrees can differ by at most d = 1. Maintaining a stricter condition (e.g above condition with d = 0) is difficult. Note that our goal is to perform all the operations search, insert and delete in O(log N) time, including the operations involved in adjusting the tree to maintain the above balance condition.

AVL Tree An AVL tree is a binary search tree in which for every node in the tree, the height of the left and right subtrees differ by at most 1. Height of subtree: Max # of edges to a leaf Height of an empty subtree: -1 Height of one node: 0 AVL tree property violated here AVL tree

AVL Tree with Minimum Number of Nodes N3 = N1+N2+1=7 N0 = 1 N1 = 2

Smallest AVL tree of height 7 Smallest AVL tree of height 8

Height of AVL Tree with N nodes Denote Nh the minimum number of nodes in an AVL tree of height h N0= 1, N1 =2 (base) Nh= Nh-1 + Nh-2 +1 (recursive relation) N > Nh= Nh-1 + Nh-2 +1 > 2 Nh-2 > 4 Nh-4 > . . . >2i Nh-2i

Height of AVL Tree with N nodes If h is even, let i=h/2–1. The equation becomes N>2h/2-1N2  N > 2h/2-1x4  h=O(log N) If h is odd, let i=(h-1)/2. The equation becomes N>2(h-1)/2N1  N > 2(h-1)/2x2  h=O(log N) Recall the cost of operations search, insert and delete is O(h). cost of searching = O(log N). (Just use the search algorithm for the binary search tree.) But insert and delete are more complicated.

Rotations Rebalance of AVL tree are done with simple modification to tree, known as rotation Insertion occurs on the “outside” (i.e., left-left or right-right) is fixed by single rotation of the tree Insertion occurs on the “inside” (i.e., left-right or right-left) is fixed by double rotation of the tree

Single Rotation to Fix Case 1(LL-rotation) k2 is the pivot node Questions: Can Y have the same height as the new X? Can Y have the same height as Z?

Single Rotation Case 1 Example k2 k1 k1 k2 X X

Informal proof/argument for the LL-case We need to show the following: After the rotation, the balance condition at the pivot node is restored and at all the nodes in the subtree rooted at the pivot. After the rotation, the balance condition is restored at all the ancestors of the pivot. We will prove both these informally.

Single Rotation to Fix Case 4 (RR-rotation) k1 violates AVL tree balance condition An insertion in subtree Z Case 4 is a symmetric case to case 1 Insertion takes O(h) time (h = height of the tree), single rotation takes O(1) time. (including locating the pivot node). Details are not obvious, but see the code …

Code for left (single) rotation /* Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. * Update heights, then set new root. */ void rotateWithLeftChild( AvlNode * & k2 ) { AvlNode *k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = max( height( k2->left ), height( k2->right ) ) + 1; k1->height = max( height( k1->left ), k2->height ) + 1; k2 = k1; }

Single Rotation Fails to fix Case 2&3 Single rotation result Case 2: violation in k2 because of insertion in subtree Y Single rotation fails to fix case 2&3 Take case 2 as an example (case 3 is a symmetry to it ) The problem is subtree Y is too deep Single rotation doesn’t make it any less deep

Code for double rotation void doubleWithLeftChild( AvlNode * & k3 ) { rotateWithRightChild( k3->left ); rotateWithLeftChild( k3 ); }

Insertion in AVL Tree Basically follows insertion strategy of binary search tree But may cause violation of AVL tree property Restore the destroyed balance condition if needed 7 6 8 6 Insert 6 Property violated Original AVL tree Restore AVL property

Balancing a node in AVL tree

AVL tree insertion

AVL tree - deletion

Summary AVL trees with n nodes will have height at most 1.5 log2 n To implement AVL tree, we use an extra field at each node that stores the height of the subtree rooted at the node. To insert a key, at most a constant number of pointer changes need to be performed. This is done by a single rotation or a double rotation at the pivot node. All dictionary operations can be performed in O(log n) time in the WORST-CASE using AVL trees.