AVL Trees When bad trees happen to good programmers.

Slides:



Advertisements
Similar presentations
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
Advertisements

AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.
CS 206 Introduction to Computer Science II 04 / 10 / 2009 Instructor: Michael Eckmann.
Rizwan Rehman Centre for Computer Studies Dibrugarh University
1 Lecture 12 AVL Trees. 2 trees static dynamic game treessearch trees priority queues and heaps graphs binary search trees AVL trees 2-3 treestries Huffman.
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.
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.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
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.
1 AVL Trees. 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation.
CS2420: Lecture 24 Vladimir Kulyukin Computer Science Department Utah State University.
AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp )
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
Data Structures Using C++1 Chapter 11 Binary Trees.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
AVL Trees CSCI 2720 Fall 2005 Kraemer. Binary Tree Issue  One major problem with the binary trees we have discussed thus far: they can become extremely.
Data Structures Using Java1 Chapter 10 Binary Trees.
Data Structures AVL Trees.
Binary Search Trees (BST)
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Recursive Objects (Part 4)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Additional Tree Traversal Example #1
AVL Tree Mohammad Asad Abbasi Lecture 12
Tree.
AVL Tree 27th Mar 2007.
ITEC 2620M Introduction to Data Structures
TREES General trees Binary trees Binary search trees AVL trees
AVL Tree A Balanced Binary Search Tree
AVL Trees CENG 213 Data Structures.
Friday, April 13, 2018 Announcements… For Today…
Binary Search Tree AVL Tree
CS 367 – Introduction to Data Structures
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
Binary Trees.
The const Keyword The answer is that, yes, we don’t want the function to change the parameter, but neither do we want to use up time and memory creating.
AVL Trees Lab 11: AVL Trees.
Lecture No.20 Data Structures Dr. Sohail Aslam
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ.
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
short illustrative repetition
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
More Trees B.Ramamurthy 4/6/2019 BR.
AVL-Trees (Part 1).
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
AVL Trees B.Ramamurthy 4/27/2019 BR.
AVL Tree Chapter 6 (cont’).
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
CS202 - Fundamental Structures of Computer Science II
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

AVL Trees When bad trees happen to good programmers.

Good tree.

Bad tree.

Bad tree. Left and right subtrees have the same height!

For every node in the tree, the left and right subtrees of that node have the same height. Too rigid.

AVL Trees (Adelson-Velskii and Landis) Binary search trees with an additional property. For every node in the tree, the height of the left and right subtrees differ by at most 1.

AVL Property If N is a node in a binary tree, node N has AVL property if the heights of the left sub-tree and right sub-tree are equal or if they differ by 1.

Height of a Tree Definition is same as level. Height of a tree is the length of the longest path from root to some leaf node. Height of an empty tree is -1. Height of a single node tree is 0. Recursive definition: height(t) = 0 if number of nodes = 1 = -1 if T is empty = 1+ max(height(LT), height(RT)) otherwise

Checking Balance

Examples

Computing Height height( T:bin_tree ) { if T = null then return -1 HL = height( T->Left) HR = height( T->Right) H = max( HL, HR ) + 1 return H }

Closer Inspection of “Balance” A tree is balanced if for every node in the tree, the height of the left and right subtrees differ by at most 1. A node is balanced if The left and right subtrees are balanced and The height of the left and right subtrees differ by at most 1 A tree is balanced if its root node is balanced.

Checking Balance ( Height, Boolean ) balance_test( T:bin_tree ) { if T = null then return (-1, true) ( HL, BL ) = balance_test( T->Left) ( HR, BR ) = balance_test( T->Right) H = max( H_L, H_R ) + 1 if (abs(HL-HR)<=1 && BL=true && BR=true) then B=true else B=false return ( H, B ) }

Operations Find, FindMin, FindMax O(log N) time Insert Need to maintain balance O(log N) time Delete a little Complicated

For starters… binary search tree permutations of 1, 2, 3

Details Balance Factor the difference between the height of a node’s left subtree and the height of its right subtree Height-Balanced Trees – all of its nodes have a balance factor of 1, 0, or -1.

Examples BF = 0 BF = -1 BF = -2

BF = 0 BF = 1 BF = 2

Inserting a value into an AVL tree 1. Follow the insertion path (if < go left, otherwise go right). 2. Remember the deepest node with a balance factor of +1 or -1. (This is called the pivot node.) 3. Insert the node at the appropriate point.

4. Recompute balance factors from the pivot node on down, including the pivot node. 5. Has the absolute value of the pivot node’s balance factor increased from 1 to 2? Inserting a value into an AVL tree

If yes, rebalance the tree!!!

Rebalancing the tree This has the visual effect of rotating the subtree of the pivot node. This is also called an AVL rotation. (Betcha didn’t see that one coming! ☺)

AVL Balancing : Four Rotations Single right 1 2 3

AVL Balancing : Four Rotations Single right Single left Double right Double left

Let’s take a closer look at the cases The one selected depends on the direction of the “Guilty” insertion, relative to the pivot node.

Case 1 The insertion that unbalanced the tree occurred in the left subtree of the left child of the pivot node insert 1 BF=1 BF=0 pivot node

Case BF=1 BF=0 BF=2 BF=1 BF=0

Case 1 steps for AVL rotation pivot’s left child becomes new pivot pivots left child retains its left subtree old pivot becomes the right child of new pivot (it’s old left child) old pivot takes the right subtree of its old left child (the new pivot now) and makes it the left subtree old pivot retains its right subtree

Case pivot node pivot’s left child becomes new pivot

Case new pivot node pivots left child retains its left subtreepivot’s left child becomes new pivot

Case new pivot node pivots left child retains its left subtree old pivot becomes the right child of new pivot (it’s old left child) 10

Case new pivot node old pivot becomes the right child of new pivot (it’s old left child) 10 old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree

Case new pivot node 10 old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree old pivot retains its right subtree

Case 2 The insertion that unbalanced the tree occurred in the right subtree of the right child of the pivot node. Case 3 The insertion that unbalanced the tree occurred in the right subtree of the left child of the pivot node. 3 subcases

Case 4 The insertion that unbalanced the tree occurred in the left subtree of the right child of the pivot node. 3 subcases

Parting thoughts on AVL trees Performance really depends on the unreliability of the input data. i.e., you need almost sorted data to make it worth it! A lot of design time is involved. A lot of work, too! Make sure you need it!