Tirgul 5 AVL trees.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
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.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
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.
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
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 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
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.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CSE 326: Data Structures AVL Trees
Tirgul 5 Comparators AVL trees. Comparators You already know interface Comparable which is used to compare objects. By implementing the interface, one.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
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.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
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 v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
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.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
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.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
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.
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.
Data Structures AVL Trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
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 trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 23 Red Black Tree Chapter 10 of textbook
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Data Structures – LECTURE Balanced trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
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.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 332: Data Abstractions AVL Trees
(edited by Nadia Al-Ghreimil)
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
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:

Tirgul 5 AVL trees

Binary search trees (reminder) Each tree node contains a value. For every node, its left subtree contains smaller values and its right subtree contains larger values. The time complexity of a search operation is proportional to the tree’s depth. The good case: If the tree is balanced, then every operation takes O(logn) time. The bad case: The tree might get very unbalanced. For example, when inserting ordered numbers to the tree, the resulting height will be exactly n. Reminder: Insertion is always to a leaf, we go down the tree, deciding to turn left or right by comparing the new value to the node value. Deletion of a non-leaf node is different for two cases: If it has only one son we move the son one level higher, overwriting the node we want to delete. If it has two sons, we overwrite it with the largest value in his left subtree or with the smallest value in his right subtree (this node is deleted using the previous case since it has at most one son).

AVL Trees Balanced Trees: After insert and delete operations we “fix” the tree to keep it (almost) balanced. AVL Tree: A binary search tree with the following additional balance property: For any node in the tree, the height of the left and right subtrees can differ by 1 at most. Note that we require this balance property for every node, not just the root.

Example 12 12 8 16 8 16 4 10 14 4 10 14 2 6 2 6 An AVL tree 1 Not an AVL tree (look at node 8, 12)

AVL trees are “reasonably” balanced We would like to prove that the “deepest” tree with n nodes still has only logarithmic depth. Another way to look at the same problem is proving that the smallest tree with depth h has size at least ch for some c >1 (in fact in our case c= 1.3) If we prove the second claim, then a tree with n nodes must have at least depth at least log1.3n (otherwise it is a counter example for the second claim.) Let’s try to build the minimal tree with depth h

Minimal AVL tree of height h Sh h-1 Sh-2 h-2 Sh-1 Look at the minimal tree of depth h, denote it Sh Since the root’s height is h, one of its sons’ height must be h-1. From the balance condition, the other son has height either h-1 or h-2. Therefore , in the minimal tree the root has one son with a sub tree of depth h-1 and one son of depth h-2. How do these sub trees look like? They are minimal i.e. they are the minimal trees Sh-1 and Sh-2, respectively.

The Maximal Height of an AVL Tree The smallest AVL tree of depth 1 has 1 node, and the smallest AVL tree of depth 2 has 2 nodes. Therefore we get: Claim: Sh = Sh-1+Sh-2+1 ( S1 = 1 ; S2 = 2 ) Fact : Sh ≥ 2h/2 Theorem: For any AVL tree with n nodes and height h: h = O(log n). Proof: 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)

A lower bound on Sh We know that Sh = Sh-1+Sh-2+1 ( S1 = 1 ; S2 = 2 ) It is easy to show by induction that Sh≥Sh-1 We shall see by induction on h > 2 that Sh ≥ (20 + 21+22+ … + 2└ h/2 ┘) Base : S3= 4 ≥ 20+21, S4 = 7 ≥ 20+21+22 Sh = Sh-1 + Sh-2 + 1 ≥ 2(Sh-2) + 1 (monotonicity) ≥ 2(20+…+ 2└ (h-2)/2 ┘)+1 = (21+…+ 2└ h/2 ┘) + 20. By geometric series some we get that Sh ≥ (2└ h/2 ┘+1- 1 )/(2-1) ≥ 2h/2

How to maintain balance General rule: after an insert or delete operation, we fix all nodes that got unbalanced. Since the height of any subtree has changed by at most 1, if a node is not balanced this means that one son has a height larger by exactly two than the other son. Next we show the four possible cases that cause a height difference of 2. In all figures, marked nodes are unbalanced nodes.

(only) Four imbalance cases k2 k1 Case 1: The left subtree is higher than the right subtree, and this is caused by the left subtree of the left child. Case 4: The symmetric case to case 1 k1 k2 C A B B A C Case 2: The left subtree is higher than the right subtree, and this is caused by the right subtree of the left child. k2 k1 Case 3: The symmetric case to case 2 k1 k2 R R P P Q Q

Single Rotation - Fixing case 1 k2 k1 right rotation k1 k2 C B A B C A The rotation takes O(1) time. Notice that the new tree is a legal search tree. For insert - it must be the case that subtree A had been increased, so after the rotation, the tree has height as before the insert. For delete, it must be the case that C had been decreased, so after the rotation, the tree has height shorter by 1. 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).

Example (caused by insertion) 12 12 k2 k1 8 16 4 16 k2 k1 C A 4 10 14 2 8 14 B C A B 2 6 1 6 10 1 Notice that the tree height has not changed after the rotation (since it was an insert operation).

Single Rotation for Case 4 k1 k2 left rotation k2 k1 A B A B C C

Example (caused by deletion) Deleting X and performing a single rotation: A A B X B B A C C C For the rotation, k1 is node A, and k2 is node B. We make k2 the root, and k1 its left son. Notice that the tree height has changed after the rotation (since it was a delete operation).

Fixing case 2 - first try... k2 k1 right rotation k1 k2 C A A C B B Single rotation doesn’t help - the tree is still not balanced! What can we do? Use rotations on k1’s sub tree to reduce case 2 to case 1!

Example (caused by insertion) 12 6 16 14 8 4 5 2 10 k3 k1 D B k2 A 12 8 16 14 10 4 6 2 5 k3 k1 D A B k2 k1 12 8 16 14 10 6 4 5 k3 D A B k2 2 Note that above is a good friend of case 1 Left rotation on 4 Right rotation on 8

Double Rotation to fix case 2 k3 k3 k2 left rotation on k1 right rotation on k3 k2 k1 k1 k3 k1 k2 D D C B C A A D B C B A 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). After insertion - original height (of the root) stays the same.

Double Rotation to fix case 3 k1 k2 right rotation on k3 left rotation on k1 k3 k1 k3 k2 A B C D A D B C

Insert and delete operations First, we insert/delete the element, as in a regular binary search tree, and then we re-balance. Observation: only nodes on the path from the root to the node we changed may become unbalanced If we went left from the root, then the right subtree was not changed, thus it remains balanced. This continues when we go down the tree.

Insert and delete operations (continued) After adding/deleting a leaf, start to go up back to the root, and while going up, re-balance every node on the way (if needed). The path is O(log n) long, and each node balance takes O(1), thus the total time for every operation is O(log n). In fact, in the insertion we can do better - after the first balance (when going up), 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.

Delete requires two passes In more sophisticated balanced trees (like red-black and B-trees), delete also requires one pass. Here this is not the case. For example, deleting X in the following tree: L D M A G 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 X B E I C F H J K

A note about implementation In programming exercise 2, you will be required to implement a balanced tree. Converting an algorithm to a real program requires much thought. When done correctly, many bugs are avoided. Important principles: Do it as general as possible, without cut & paste. Although more complicated to design, it will reduce your total work time. Methods should be short and simple. If some method becomes too complicated, you missed something, and this is a sure bug! If there’s time left, you might want to do the following example on the board (numbers are inserted in the order written): 5,2,3,9,7,1,8,6,4,0

Common mistakes in THW1