Data Structures – LECTURE Balanced trees

Slides:



Advertisements
Similar presentations
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
Advertisements

AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
Tirgul 5 AVL trees.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
Tirgul 5 Comparators AVL trees. Comparators You already know interface Comparable which is used to compare objects. By implementing the interface, one.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
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!
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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.
Data Structures AVL Trees.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 23 Red Black Tree Chapter 10 of textbook
AVL Trees CSE, POSTECH.
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
B-Trees B-Trees.
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.
Data Structures and Algorithms
B-Trees B-Trees.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
AVL Trees.
Red Black Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Summary of General Binary search tree
AVL Tree 27th Mar 2007.
Dynamic Dictionaries Primary Operations: Additional operations:
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
Red-Black Trees Motivations
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.
Wednesday, April 18, 2018 Announcements… For Today…
Multi-Way Search 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.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Data Structures & Algorithms
Algorithms and Data Structures Lecture VIII
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CSIT 402 Data Structures II With thanks to TK Prasad
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees (Part 1).
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Binary SearchTrees [CLRS] – Chap 12.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
B-Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Data Structures – LECTURE Balanced trees Motivation Types of balanced trees AVL trees Properties Balancing operations Data Structures, Spring 2006 © L. Joskowicz

Motivation Binary search trees are useful for efficiently implementing dynamic set operations: Search, Successor, Predecessor, Minimum, Maximum, Insert, Delete in O(h) time, where h is the height of the tree When the tree is balanced, that is, its height h = O(lg n), the operations are indeed efficient. However, the Insert and Delete alter the shape of the tree and can result in an unbalanced tree. In the worst case, h = O(n)  no better than a linked list! Data Structures, Spring 2006 © L. Joskowicz

Balanced trees Find a method for keeping the tree always balanced When an Insert or Delete operation causes an imbalance, we want to correct this in at most O(lg n) time  no complexity overhead. Add a requirement on the height of subtrees The most popular balanced tree data structures: AVL trees: subtree height difference of at most 1 Red-Black trees: subtree height difference ≤ 2(lg n + 1) 2-3 B-trees: same subtree height, varying # of children Data Structures, Spring 2006 © L. Joskowicz

AVL: Adelson-Velsky and Landis, 1962 AVL trees An AVL tree is a binary tree with one balance property: For any node in the tree, the height difference between its left and right subtrees is at most one. Sh-1 Sh-2 h-2 h-1 h Sh x Sh = Sh–1 + Sh–2 + 1 Size of tree: AVL: Adelson-Velsky and Landis, 1962 Data Structures, Spring 2006 © L. Joskowicz

Examples 12 12 8 16 14 4 10 2 6 1 8 16 4 10 14 2 6 Not an AVL tree (nodes 8 and 12) An AVL tree Data Structures, Spring 2006 © L. Joskowicz

AVL tree properties What is the minimum and maximum height h of an AVL tree with n nodes? Study the recursion: T(n) = T(n-1) + T(n-2) + 1 Minimum height: fill all levels except the last one root Tright Tleft h-1 h = lg n or h-2 Data Structures, Spring 2006 © L. Joskowicz

Maximal height of an AVL tree All levels have a difference of height of 1. The smallest AVL tree of depth 1 has 1 node. The smallest AVL tree of depth 2 has 2 nodes. In general, Sh = Sh-1+ Sh-2+ 1 (S1 = 1; S2 = 2) Sh-1 Sh-2 h-2 h-1 h Sh x The proof shows that the number of nodes in exponential in the height, even for the minimal tree. Since exp and log are inverse functions, the height is logarithmic in the number of nodes, therefore the insert is O(log n) Data Structures, Spring 2006 © L. Joskowicz

Maximal AVL height: complexity Study the Fibonacci series recursion T(n) = T(n-1) + T(n-2) (T1 = 0; T2 = 2) Special case of linear recurrence equation! 2T(n-2) ≤ T(n) ≤ 2T(n-1) 2└n/2┘ ≤ T(n) ≤ 2n-1 The solution is T(n) = cn where c is a constant (prove guess!) T(n) = T(n-1) + T(n-2) cn = cn-1 +cn-2 c2 = c +1  h = lg1.6n  O(lg n) golden ratio! S(n) = T(n) +1 same complexity Data Structures, Spring 2006 © L. Joskowicz

Balancing AVL trees Before the operation, the tree is balanced. After an insertion or deletion operation, the tree might become unbalanced.  fix subtrees that became unbalanced. The height of any subtree has changed by at most 1. Thus, if a node is not balanced, the difference between its children heights is 2. Four possible cases with a height difference of 2. Data Structures, Spring 2006 © L. Joskowicz

Cases of imbalance (1) k2 k1 B C A Case 1: The left subtree is higher than the right subtree because of the left subtree of the left child Case 4: The right subtree is higher than the left subtree because of the right subtree of the right child k1 k2 B A C Data Structures, Spring 2006 © L. Joskowicz

Cases of imbalance (2) Case 2: The left subtree is higher than the right subtree because of the right subtree of the left child k2 k1 C A B Case 3: The right subtree is higher than the left subtree because of the left subtree of the right child k1 k2 C A B Data Structures, Spring 2006 © L. Joskowicz

Fixing Case 1: right rotation k2 k2 k1 A B C right rotation k1 C B A The rotation takes O(1) time and restores the balance Insertion – The height of subtree A is increased. After the rotation, the tree has the same height as before the insert. Deletion – The height of subtree C is decreased. After the rotation, the tree has a lower height by 1 from before Remark: One may ask, If after insert and the balance operation, the height of the tree does not change, when does the height actually increase ?? The answer: the height changes only when the tree has balance of 0 in all nodes on the path from the root to the newly added leaf (thus no re-balance is needed). So, when balance operation is performed, the height doesn’t change, and when the height changes, the balance remains (without a balance operation). Data Structures, Spring 2006 © L. Joskowicz

Case 1: Insertion example 12 12 8 16 14 10 4 6 2 1 k2 k1 C A B k2 8 16 k1 C 4 10 14 A 2 6 B 1 Insert 1 Data Structures, Spring 2006 © L. Joskowicz

Fixing Case 4: left rotation k2 k1 k2 C B A k1 left rotation k1 k2 A B C Analysis as in Case 1 Data Structures, Spring 2006 © L. Joskowicz

Case 4: deletion example k1 k2 delete 5 7 9 k1 k2 left rotation 5 7 9 5 k2 k1 4 7 9 Data Structures, Spring 2006 © L. Joskowicz

Fixing Case 2 – first try … k2 k1 B A C k2 right rotation k1 C A B A single rotation is not enough A series of rotations on the subtree of k1 to reduce Case 2 to Case 1! Data Structures, Spring 2006 © L. Joskowicz

Fixing Case 2: right then left rotation left rotation on k1 right rotation on k3 k3 k3 k1 A C k2 B1 B2 k3 k1 A C k2 B1 B2 k1 k2 C A B1 B2 Note that the height of B and C is not exactly known and that’s why they are located between the two dashed lines and not on one of them. In an insert operation, one of them is located on the lower line and one on the upper line, in the delete, at least one is located on the lower line (but it is possible that they both are). Data Structures, Spring 2006 © L. Joskowicz

Case 2: insertion example 12 6 16 14 8 4 5 2 10 k3 k1 C B1 k2 A 12 k1 12 8 16 14 10 6 4 5 k3 C A B1 k2 2 k3 8 16 k1 4 10 14 k2 C 2 6 A B1 5 no B2 left rotation on 4 right rotation on 8 Data Structures, Spring 2006 © L. Joskowicz

Fixing Case 3: right then left rotation right rotation on k3 left rotation on k1 k1 k3 k1 A C k2 B1 B2 k3 k2 A C B1 B2 Analysis as in Case 2 Data Structures, Spring 2006 © L. Joskowicz

Insert and delete operations (1) Insert/delete the element as in a regular binary search tree, and then re-balance. Observation: only nodes on the path from the root to the node that was changed may become unbalanced. Assume we go left: the right subtree was not changed, and stays balanced. This holds as we descend the tree. Data Structures, Spring 2006 © L. Joskowicz

Insert and delete operations (2) After adding/deleting a leaf, go up, back to the root. Re-balance every node on the way as necessary. The path is O(lg n) long, and each node balance takes O(1), thus the total time for every operation is O(lg n). For the insertion we can do better: when going up, after the first balance, the subtree that was balanced has height as before, so all higher nodes are now balanced again. We can find this node in the pass down to the leaf, so one pass is enough. Data Structures, Spring 2006 © L. Joskowicz

Example: deletion requiring two passes 18 12 21 7 13 3 8 10 15 9 11 14 16 Deleting X upsets the balance at `A'. When we rotate (B up, A down) to fix this, the whole of this subtree - which is D's left subtree - gets shorter. This means the balance at `D' is upset. When we fix this with a rotation (G up, D down) This subtree (L's left subtree) gets shorter - the subtree heights at this stage are 4 on the left and 6 on the right. When we rotate to fix this (L down, M up), we will get a 5-5 balance at M - so now it is one shorter and its parent is out of balance. This can propagate right to the top of the tree 17 delete 3 Data Structures, Spring 2006 © L. Joskowicz

Summary Efficient dynamic operations on a binary tree require a balance tree whose height is O(lg n) Balanced height trees: AVL trees Red-black trees B-trees Insertion and deletion operations might require re-balancing in O(lg n) to restore balanced tree properties Re-balancing with left and/or right rotations on a case by case basis. Data Structures, Spring 2006 © L. Joskowicz