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.

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

1 AVL Trees (10.2) CSE 2011 Winter April 2015.
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.
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
AA Trees another 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 balanced.
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.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
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.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
AVL Trees ITCS6114 Algorithms and Data Structures.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
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.
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.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
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.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
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.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
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.
UNIT III TREES.
CSIT 402 Data Structures II
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.
Introduction Applications Balance Factor Rotations Deletion Example
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Tree A Balanced Binary Search Tree
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
Lecture No.20 Data Structures Dr. Sohail Aslam
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL Trees (a few more slides)
ITCS6114 Algorithms and Data Structures
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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 tree DSW expensive  How can we ensure that our BST stays balanced? after inserts and deletes 2

 Adelson-Velskii and Landis  Binary search tree  Additional balance condition for every node height of subtrees differ by at most 1 must store height of each node empty tree has height of -1 3

 Insert as normal (BST)  Update heights  Check balance condition height differences of nodes along path from new node to root  Use rotation to fix imbalances 4

 Fixing imbalance at node A  4 cases: Insertion into 1. left subtree of left child of A 2. right subtree of left child of A 3. left subtree of right child of A 4. right subtree of right child of A  Cases 1 & 4 are outside inserts  Cases 2 & 3 are inside inserts 5

 Insert key less than A & B 6 A A B B X Y Z h: 2 h: 3h: 2 h: 4

 Insert key less than A & B 7 A A B B X Y Z h: 2 h: 3h: 2 h: 4 A A B B X Y Z h: 2 h: 3 h: 4h: 2 h: 5

 What series of rotations can we do to fix this? 8

 Rotate A with “highest” subtree 9 A A B B X Y Z h: 2 h: 3 h: 4h: 2 h: 5

 Rotate A with “highest” subtree 10 A A B B Y Z A A B B Y Z X X h: 2 h: 3 h: 4h: 2 h: 5 h: 2 h: 3 h: 4 h: 3 h: 2

 Are we done?  Worry about imbalances above B? 11 A A B B Y Z X h: 2 h: 3 h: 4 h: 3 h: 2

 B tree same height as A tree before insert  Property of outside insert rebalances  Right-right imbalance similar 12 A A B B Y Z X h: 2 h: 3 h: 4 h: 3 h: 2

 More specifically  Balance factor = h(left) – h(right)  If node N has a balance factor of 2, left subtree (L) is too high if L has a balance factor of 1 rotate N & L  If node N has a balance factor of -2, right subtree (R) is too high if R has a balance factor of -1 rotate N & R 13

14

 Fixing imbalance at node A  4 cases: Insertion into 1. left subtree of left child of A 2. right subtree of left child of A 3. left subtree of right child of A 4. right subtree of right child of A  Cases 1 & 4 are outside inserts  Cases 2 & 3 are inside inserts 15

 Insert key less than A, greater than B 16 A A B B X Y Z h: 2 h: 3h: 2 h: 4

 Insert key less than A, greater than B 17 A A B B X Y Z h: 2 h: 3h: 2 h: 4 A A B B X Y Z h: 3 h: 2 h: 4h: 2 h: 5

 Let’s try to rotate A & B again 18 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y

 Let’s try to rotate A & B again 19 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y

 Let’s expand Y 20 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Q R C C A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y h: 1 h: 2

 What if we made C the root 21 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 R C C h: 2 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 Q R h: 1 Q

 What series of rotations can we do to accomplish this? 22

 Rotate left child (B) with its right child (C) 23 A A B B X Z h: 3 h: 1 h: 4h: 2 h: 5 C C h: 2 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 C C h: 2 Q R h: 1 Q R

 Rotate root (A) with its new left child (C) 24 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Q R C C h: 2 Q R

 Are we done?  Worry about imbalances above ? 25 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 Q R

 C tree same height as A tree before insert  Property of inside insert rebalances  Right-left imbalance similar 26 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 Q R

 More generally  If there is an inside imbalance at A  Find A’s highest child, B  Rotate B with its highest child, C  Rotate A with C 27

 More specifically  Balance factor = h(left) – h(right)  If node N has a balance factor of 2, left subtree (L) is too high if L has a balance factor of -1 rotate L with L’s right child rotate N with its new left child 28

 More specifically  Balance factor = h(left) – h(right)  If node N has a balance factor of -2, right subtree (R) is too high if R has a balance factor of 1 rotate R with R’s left child rotate N with it’s new right child 29

30

 Find the node N w/ key to be deleted  Different actions depending on N’s # of kids Case 1: N has 0 kids (it’s a leaf)  set parent’s N-pointer (left or right) to null Case 2: N has 1 kid  set parent’s N-pointer to point to N’s only kid Case 3: N has 2 kids  Replace N’s key with a key further down in the tree  Delete that node 31

 What node value can replace n’s value? new value of n must be: > all values in left subtree < all values in right subtree  Largest value from the left subtree  Smallest value from the right subtree let’s choose this one (arbitrarily) use findMin on root of right subtree 32

 Deletes can also imbalance an AVL tree  Can we fix these imbalances with rotations too?  How many rotations do we need to do? 33

Smallest value in right subtree delete(50) 34 X Y Z h: 2 h: 3 h: 1 h: 0 h: 2 h: 4 65 h: 1 h: 5 62 h: 0

Copy smallest key in R subtree delete(50) X Y Z h: 2 h: 3 h: 1 h: 0 h: 2 h: 4 65 h: 1 h: 5 62 h: 0

delete(55) from R subtree delete(50) X Y Z h: 2 h: 3 h: 0 h: 2 h: 4 65 h: 1 h: 5 62 h: 0

Fix imbalance delete(50) 37 X Y Z h: 1 h: 2 h: 0 h: 2 h: 3 65 h: 1 h: 5 62 h: 0

New imbalance Need to rebalance all the way up to root delete(50) X Y Z h: 1 h: 2 h: 0 h: 3 65 h: 5 62 h: 0

 More generally  Perform a BST delete  Check for imbalances along path to root  Rebalance using expanded insert rules if delete happened in “light” child “heavy” child may have a balance factor of 0 additional case from insertion rules 39

 Balance factor = h(left) – h(right)  Given node N and its two children L & R  bf(N) == -2 & bf(R) == 0 single right child rotation  bf(N) == -2 & bf(R)== -1 single right child rotation  bf(N) == -2 & bf(R) == 1 double right child rotation (from inside insert) 40

 Balance factor = h(left) – h(right)  Given node N and its two children R & L  bf(N) == 2 & bf(L) == 0 single left child rotation  bf(N) == 2 & bf(L) == 1 single left child rotation  bf(N) == 2 & bf(L) == -1 double left child rotation (from inside insert) 41

 BST insert, delete, lookup O(H), H is height of tree  AVL like BST, but ensures balance balanced tree has height of log N  Balancing rotations on insert and delete 42

 AVL Complexity  Insert inserting node: O(H) == O(logN) rebalance: O(rotations) == O(2) == O(1)  Erase deleting a node: O(H) == O(logN) rebalance: O(rotations) == O(logN) 43

44