11/7/20151 Balanced Trees 15-111 Data Structures Ananda Gunawardena.

Slides:



Advertisements
Similar presentations
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.
Advertisements

AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
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
AVL Tree Iris Jiaonghong Shi. AVL tree operations AVL find : – Same as BST find AVL insert : – First BST insert, then check balance and potentially.
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.
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 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.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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.
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.
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.
AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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:
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.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
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.
Fundamental Structures of Computer Science Feb 03, 2005 Ananda Guna Tries.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
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.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
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
AVL Tree A Balanced Binary Search Tree
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Balanced Binary Search Trees
CSE 332: Data Abstractions AVL Trees
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

11/7/20151 Balanced Trees Data Structures Ananda Gunawardena

11/7/20152 A Good Tree In a "good" BST we have depth of T = O( log n ) n = num of nodes in the tree Theorem: If the tree is constructed from n inputs given in random order, then we can expect the depth of the tree to be log 2 n. (no proof given) But if the input is already (nearly, reverse,…) sorted we are in trouble.

11/7/20153 Forcing good behavior We can show that for any n inputs, there always is a BST containing these elements of logarithmic depth. But if we just insert the standard way, we may build a very unbalanced, deep tree. Can we somehow force the tree to remain shallow? At low cost?

11/7/20154 Balanced Trees A balanced tree is where equal (“almost”) number of nodes exists in left and right sub trees of each node –However in practice this is hard to enforce We can expect the trees to remain shallow by “randomizing” a data set before inserting to a tree –Need O(n) operations to randomize the data set A relaxed balanced condition can be used in building a “good” tree –AVL trees

11/7/20155 AVL Trees AVL trees requires a relaxed balanced condition Define “balanced factor” of a node to be the absolute difference in heights between left and right sub trees AVL tree is defined as a tree such that, for each node, balanced factor ≤ 1

11/7/20156 AVL-Trees G. M. Adelson-Velskii and E. M. Landis, or less

11/7/20157 AVL-Trees An AVL-tree is a BST with the property that at every node the difference in the depth of the left and right subtree is at most 1. OK not OK

11/7/20158 Bad News Insertion in an AVL-tree is more complicated: inserting a new element as a leaf may break the balance of the tree. But we can't just place the new element somewhere else, we have to maintain the BST property. Solution: insert in standard place, but then rebalance the tree.

11/7/20159 But How? The magic concept is rotation. A rotation rearranges a BST, but preserve the BST property. Can be done out in O(1) steps. To fix up an AVL tree, we have to perform several rotations, but only along a branch: total damage is O(log #nodes).

11/7/ But How? rotate right on y x A B y C x A B y C rotate left on x

11/7/ So? Rotation does not change the flattening, so we still have a BST. But the depth of the leaves change by -1 in A, stay the same in B, and change by +1 in C.

11/7/ Well, not quite Unfortunately, there are other cases to consider, depending on where exactly the balance condition is violated. To fix some of them requires a double rotation. There is a nice demo at: s/avl/BT.html

11/7/ Tree Balance Rotations Note that after an insertion only the nodes in the path from root to the node have their balance information altered Find the node that violates the AVL condition and rebalance the tree. Assume that X is the node that has violated the AVL condition –I.e The left and right sub trees of X is differ by 2 in height.

11/7/ Four Cases – Case I Case I – The left subtree of the left child of X violates the property. X y A B C X y A B C Rotate RIGHT

11/7/ Code for Case 1 /** * Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. */ static BinaryNode rotateWithLeftChild( BinaryNode k2 ) { BinaryNode k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; }

11/7/ Four Cases – Case 2 Case 2 – The right subtree of the right child of X violates the property. X y A B C Rotate LEFT X y A B C

11/7/ Code for Case 2 /** * Rotate binary tree node with right child. * For AVL trees, this is a single rotation for case 4. */ static BinaryNode rotateWithRightChild( BinaryNode k1 ) { BinaryNode k2 = k1.right; k1.right = k2.left; k2.left = k1; return k2; }

11/7/ Four Cases – Case 3 Case 3 – The right subtree of the left child of X violates the property X y A C D Z B X y A C D Z B Rotate the tree LEFT about X’s child and Grandchild Ctd..

11/7/ Four Cases – Case 3 ctd.. X y A C D Z B Rotate the tree RIGHT about X and its new child X y A C D Z B

11/7/ Code for Case 3 /** * Double rotate binary tree node: first right child * with its left child; then node k1 with new right child. * For AVL trees, this is a double rotation for case 3. */ static BinaryNode doubleRotateWithRightChild( BinaryNode k1 ) { k1.right = rotateWithLeftChild( k1.right ); return rotateWithRightChild( k1 ); }

11/7/ Four Cases – Case 4 Case 4 – The left subtree of the right child of X violates the property Rotate the tree RIGHT about Y and Z X y A C D Z B X y A C D Z B Ctd..

11/7/ Four Cases – Case 4 ctd.. Rotate the tree LEFT about X and Z X y A C D Z B X y A C D Z B

11/7/ Code for Case 4 /** Double rotate binary tree node: first left child * with its right child; then node k3 with new left child. * For AVL trees, this is a double rotation for case 2. */ static BinaryNode doubleRotateWithLeftChild( BinaryNode k3 ) { k3.left = rotateWithRightChild( k3.left ); return rotateWithLeftChild( k3 ); }

11/7/ AVL Tree Examples Insert 12, 8, 7, 14, 18, 10, 20 with AVL rotations

11/7/ Implementing AVL trees The main complications are insertion and deletion of nodes. For deletion: –Don’t actually delete nodes. –Just mark nodes as deleted. –Called lazy deletion.

11/7/ Lazy deletion On average, deleting even half of the nodes by marking leads to depth penalty of only 1.

11/7/ AVL Summary Insert a new node in left or right subtree. Test the height information along the path of the insertion. If not changed we are done Otherwise do a single or double rotation based on the four cases Question: What is the best thing to do about height info?