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.

Slides:



Advertisements
Similar presentations
AVL Trees binary tree for every node x, define its balance factor
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.
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.
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
Chapter 10 Efficient Binary Search Trees
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.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
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 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.
Data Structures Using C++1 Chapter 11 Binary Trees.
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.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
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.
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.
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 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.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
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:
BCA-II Data Structure Using C
Search Trees.
Binary Search Trees A binary search tree is a binary tree
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
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 binary tree for every node x, define its balance factor
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.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Cinda Heeren / Geoffrey Tien
Trees (Chapter 4) Binary Search Trees - Review Definition
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
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.
Copyright © Aiman Hanna All rights reserved
Data Structures & Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
CE 221 Data Structures and Algorithms
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red Black Trees Colored Nodes Definition Binary search tree.
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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 changes after insertion or deletion, rebalancing is performed

AVL Trees: Balancing Factor BF(T) Definition: The balance factor, BF(T), of a node T in a binary tree is defined to be hR - hL, where hR and hL , respectively, are the heights of the right and left subtrees of T. For any node T in an AVL tree: BF(T) = -1, 0 or 1 Maintaining balance after insertion/deletion: must update all the balancing information for nodes on path back to the root; if balance factor becomes -2 or +2, rebalancing (referred to as rotation) is required insertion into AVL tree with zero or one node cannot cause an imbalance

Rebalancing Scenarios and Corresponding Rotation Methods Single Last: the first unbalanced node (a) on the path from the insertion point towards the root RR: X is inserted in right subtree of right subtree of a. LL: new node X is inserted in left subtree of left subtree of a. LL RR RR Operations (Left Rotation): Last = a Q = Last->right; Last->right = Q->left; Last = Q: LL Operations (Right Rotation): Last = a Q = Last->left; Last->left = Q->right; Last = Q:

Rebalancing Scenarios and Corresponding Rotation Methods Double LR: X is inserted in right subtree of left subtree of A RL: X is inserted in left subtree of right subtree of A LR Operations (Left-Right Rotation): Last = a Q = Last->left T =Q->right Q->right = T->left Last->left = T T-left = Q Last->left = T->right Last = T RL Operations (Right-Left Rotation): Last = a Q = Last->right T =Q->left Q->left = T->right Last->right = T T-right = Q Last->right = T->left Last = T

Balancing Properties Only nodes with balance values +1 and -1 can become unbalanced due to insertion Only nodes on search path between inserted node and the unbalanced node will need to have their balance fields updated Rotations have 2 properties: the inorder of the elements remains the same after the rotation as before the overall height of the tree is the same after the rotation as it was before the insertion Procedures change pointer in local root node so it points to root of new subtree

Algorithm for Insertion Attach new node as in Binary Search Tree Travel up the tree, searching for pivot node (node whose balance is either +1 or -1 and is closest to new node), updating balance info at every node on the path If path ends at root without having found any imbalanced nodes, we are done; Else do a rotation at pivot node and adjust its balance. Observations: For the balance factor of a node X to become +2, it must have been +1 before the insertion. Thus, before insertion, balance factors of all nodes on the path from X to the new insertion point must have been 0. If insertion did not result in an unbalanced tree, even though some path length increased by 1, it must be that the new balance factor of X is 0.

Example: Example: Insertion of 70, 40, 83, 35, 55, 80, 83, 85, 17, 37, 50, 60, 81, 100, 45, 53 into initially empty AVL Inserting successors of 45, 53, 81 or 100 will cause imbalance in the tree

Example: continued Insert 43: left child of left subtree --- single right rotation after right rotation

Example: continued Insert 54: right child of the left subtree -- double LR rotation after left rotation after right rotation

Example: continued Insert 82: right child of the right subtree -- Single left rotation after left rotation Insert 110, 93 and 54

Delete Basic algorithm Delete the node as in simple binary search tree. Retrace search path from deleted node back to root. If an imbalance occurs, perform rotation(s) until tree is balanced Deletion can require 1.44 log(n+2) rebalances in worst case, while insertion can require at most one rebalance The average case takes log n + .25 searches, which reduces the number of rotations in case of deletions to log n + .25 Experiments indicate that deletions in 78% of cases cause no rebalancing, while only 25% of insertions do not cause imbalance. More costly deletions occur less often Lazy deletion is best if deletions are infrequent

Delete Example Delete 12: Rotate Left 9 Rotate Right 11

Delete Rotate Right 8

AVL Tree: Analysis Let’s denote the number of nodes,n, in an AVL tree of height h by n(h). In the following we show that the height, h, of an an AVL tree is bounded by O(log n(h)). Using AVL tree property we can say that at least n(h) = n(h-1) + n(h-2) + 1 Using the above relationship, we can also say that n(h-1) > n(h-2) Substituting n(h-1) and dropping the 1 above, we rewrite: n(h) > 2 n(h-2) Similarly we can write n(h-2) > 2 n(h-4) and n(h-4) > 2 n(h-6) Scoping these terms in the equation for n(h) n(h) > 2 (2 n(h-4)) n(h) > 2 (2 (2n(h-6)) n(h) > 8 n(h-6) The above equations can be generalized as follows: n(h) > 2in(h-2i) Since we know the base cases of AVL tree n(0) = 1 and n(1) = 2, we get i = h/2 -1 Therefore replacing i in the equation for n(h), n(h) > 2 h/2 -1 x 1 Taking log on both sides yields h = O(log n(h)) QED

AVL Node Implementation in C++ Using BST Class /*1*/ template <class Etype> /*2*/ class AvlNode : public TreeNode<Etype> /*3*/ { /*4*/ private: /*5*/ int height; /*6*/ friend int NodeHt ( AvlNode * P) /*7*/ friend int NodeHt ( TreeNode<Etype>* P ) /*8*/ friend void CalculateHeight ( AvlNode* P ) /*9*/ friend void SRotateLeft ( AvlNode* & k2 ); /*10*/ friend void DRotateLeft ( AvlNode* & k3 ); /*11*/ friend void SRotateRight ( AvlNode* & k1 ); /*12*/ friend void DRotateRight ( AvlNode* & k3 ); /*13*/ public: /*14*/ AvlNode ( Etype E=0, AvlNode* L = NULL, AvlNode* R=NULL, int H=0 ); /*15*/ };