SlideShare a Scribd company logo

More Related Content

What's hot (20)

Lec3
Lec3Lec3
Lec3
 
Lec4
Lec4Lec4
Lec4
 
Lec20
Lec20Lec20
Lec20
 
Lec5
Lec5Lec5
Lec5
 
Lec1
Lec1Lec1
Lec1
 
Lec3
Lec3Lec3
Lec3
 
Segment tree
Segment treeSegment tree
Segment tree
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Heaps
HeapsHeaps
Heaps
 
Recursion
RecursionRecursion
Recursion
 
Heap sort
Heap sortHeap sort
Heap sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Heaps
HeapsHeaps
Heaps
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
 
Lec17
Lec17Lec17
Lec17
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Heap sort
Heap sort Heap sort
Heap sort
 

Viewers also liked

Stroke symposiuma tpaper91411
Stroke symposiuma tpaper91411Stroke symposiuma tpaper91411
Stroke symposiuma tpaper91411Pat Maher
 
Aircraft landing views
Aircraft landing viewsAircraft landing views
Aircraft landing viewsfilipj2000
 
Introduction to Steens Furniture
Introduction to Steens FurnitureIntroduction to Steens Furniture
Introduction to Steens FurnitureSteens Furniture
 
Lapland scandinavia
Lapland scandinaviaLapland scandinavia
Lapland scandinaviafilipj2000
 
Russian photographers (8 18)
Russian photographers (8 18)Russian photographers (8 18)
Russian photographers (8 18)filipj2000
 
Learning from Web Activity
Learning from Web ActivityLearning from Web Activity
Learning from Web Activityjakehofman
 
China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]filipj2000
 
Modeling Social Data, Lecture 1: Case Studies
Modeling Social Data, Lecture 1: Case StudiesModeling Social Data, Lecture 1: Case Studies
Modeling Social Data, Lecture 1: Case Studiesjakehofman
 
Chapter 1 market & marketing
Chapter 1 market & marketingChapter 1 market & marketing
Chapter 1 market & marketingHo Cao Viet
 
Zeynep asa 2011 privacy
Zeynep asa 2011 privacyZeynep asa 2011 privacy
Zeynep asa 2011 privacyZeynep Tufekci
 
Maherwhitepaperon at universaldesign112111v2 0
Maherwhitepaperon at universaldesign112111v2 0Maherwhitepaperon at universaldesign112111v2 0
Maherwhitepaperon at universaldesign112111v2 0Pat Maher
 
삼성핵심가치위반사례
삼성핵심가치위반사례삼성핵심가치위반사례
삼성핵심가치위반사례조현경
 
08 copia de tarjeta kardex
08 copia de tarjeta kardex08 copia de tarjeta kardex
08 copia de tarjeta kardexANGIE MARCELA
 
Investigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel anguloInvestigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel anguloFidel Angulo
 
Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...
Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...
Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...Rijksdienst voor Ondernemend Nederland
 

Viewers also liked (20)

Stroke symposiuma tpaper91411
Stroke symposiuma tpaper91411Stroke symposiuma tpaper91411
Stroke symposiuma tpaper91411
 
Aircraft landing views
Aircraft landing viewsAircraft landing views
Aircraft landing views
 
Introduction to Steens Furniture
Introduction to Steens FurnitureIntroduction to Steens Furniture
Introduction to Steens Furniture
 
Time
TimeTime
Time
 
Lapland scandinavia
Lapland scandinaviaLapland scandinavia
Lapland scandinavia
 
Russian photographers (8 18)
Russian photographers (8 18)Russian photographers (8 18)
Russian photographers (8 18)
 
Learning from Web Activity
Learning from Web ActivityLearning from Web Activity
Learning from Web Activity
 
STEM Powers Jets
STEM Powers JetsSTEM Powers Jets
STEM Powers Jets
 
China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]
 
Modeling Social Data, Lecture 1: Case Studies
Modeling Social Data, Lecture 1: Case StudiesModeling Social Data, Lecture 1: Case Studies
Modeling Social Data, Lecture 1: Case Studies
 
Chapter 1 market & marketing
Chapter 1 market & marketingChapter 1 market & marketing
Chapter 1 market & marketing
 
Polarreise
PolarreisePolarreise
Polarreise
 
Zeynep asa 2011 privacy
Zeynep asa 2011 privacyZeynep asa 2011 privacy
Zeynep asa 2011 privacy
 
Maherwhitepaperon at universaldesign112111v2 0
Maherwhitepaperon at universaldesign112111v2 0Maherwhitepaperon at universaldesign112111v2 0
Maherwhitepaperon at universaldesign112111v2 0
 
삼성핵심가치위반사례
삼성핵심가치위반사례삼성핵심가치위반사례
삼성핵심가치위반사례
 
08 copia de tarjeta kardex
08 copia de tarjeta kardex08 copia de tarjeta kardex
08 copia de tarjeta kardex
 
产品早期市场推广探路实践 by XDash
产品早期市场推广探路实践 by XDash产品早期市场推广探路实践 by XDash
产品早期市场推广探路实践 by XDash
 
Investigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel anguloInvestigación desde la perspectiva del problema fidel angulo
Investigación desde la perspectiva del problema fidel angulo
 
Lake maggiore
Lake maggioreLake maggiore
Lake maggiore
 
Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...
Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...
Verkiezing schaduwburgemeester dorpscafé malden ruud van gisteren d.d. 3 3-20...
 

Similar to Lec12

Lesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, AccelerationLesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, AccelerationMatthew Leingang
 
Linear Transformations, Matrix Algebra
Linear Transformations, Matrix AlgebraLinear Transformations, Matrix Algebra
Linear Transformations, Matrix AlgebraPrasanth George
 
Ct signal operations
Ct signal operationsCt signal operations
Ct signal operationsmihir jain
 
SIGNAL OPERATIONS
SIGNAL OPERATIONSSIGNAL OPERATIONS
SIGNAL OPERATIONSmihir jain
 
Introduction to modern time series analysis
Introduction to modern time series analysisIntroduction to modern time series analysis
Introduction to modern time series analysisSpringer
 
1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculus1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculuscpphysics
 
Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009akabaka12
 
On the Stick and Rope Problem - Draft 1
On the Stick and Rope Problem - Draft 1On the Stick and Rope Problem - Draft 1
On the Stick and Rope Problem - Draft 1Iwan Pranoto
 
Lesson 7: Vector-valued functions
Lesson 7: Vector-valued functionsLesson 7: Vector-valued functions
Lesson 7: Vector-valued functionsMatthew Leingang
 

Similar to Lec12 (20)

Lec12
Lec12Lec12
Lec12
 
C slides 11
C slides 11C slides 11
C slides 11
 
Lesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, AccelerationLesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, Acceleration
 
1 d wave equation
1 d wave equation1 d wave equation
1 d wave equation
 
Linear Transformations, Matrix Algebra
Linear Transformations, Matrix AlgebraLinear Transformations, Matrix Algebra
Linear Transformations, Matrix Algebra
 
Ism et chapter_2
Ism et chapter_2Ism et chapter_2
Ism et chapter_2
 
Ism et chapter_2
Ism et chapter_2Ism et chapter_2
Ism et chapter_2
 
Ism et chapter_2
Ism et chapter_2Ism et chapter_2
Ism et chapter_2
 
Ct signal operations
Ct signal operationsCt signal operations
Ct signal operations
 
SIGNAL OPERATIONS
SIGNAL OPERATIONSSIGNAL OPERATIONS
SIGNAL OPERATIONS
 
Introduction to modern time series analysis
Introduction to modern time series analysisIntroduction to modern time series analysis
Introduction to modern time series analysis
 
1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculus1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculus
 
Figures
FiguresFigures
Figures
 
Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009Engr 213 midterm 1b sol 2009
Engr 213 midterm 1b sol 2009
 
Desktop
DesktopDesktop
Desktop
 
Desktop
DesktopDesktop
Desktop
 
Conductance
ConductanceConductance
Conductance
 
On the Stick and Rope Problem - Draft 1
On the Stick and Rope Problem - Draft 1On the Stick and Rope Problem - Draft 1
On the Stick and Rope Problem - Draft 1
 
Solved problems
Solved problemsSolved problems
Solved problems
 
Lesson 7: Vector-valued functions
Lesson 7: Vector-valued functionsLesson 7: Vector-valued functions
Lesson 7: Vector-valued functions
 

More from Anjneya Varshney (6)

Lec23
Lec23Lec23
Lec23
 
Lec22
Lec22Lec22
Lec22
 
Lec21
Lec21Lec21
Lec21
 
Lec19
Lec19Lec19
Lec19
 
Lec16
Lec16Lec16
Lec16
 
Lec13
Lec13Lec13
Lec13
 

Lec12

  • 1. AVL Trees  AVL Trees 1
  • 2. Insertion  Inserting a node, v, into an AVL tree changes the heights of some of the nodes in T.  The only nodes whose heights can increase are the ancestors of node v.  If insertion causes T to become unbalanced, then some ancestor of v would have a height- imbalance.  We travel up the tree from v until we find the first node x such that its grandparent z is unbalanced.  Let y be the parent of node x. 2
  • 3. Insertion (2) To rebalance the subtree rooted at z, we must perform a rotation. 44 44 z x 17 78 17 62 y y 50 88 50 78 z 32 32 48 62 x 48 54 88 54 3
  • 4. Rotations  Rotation is a way of locally reorganizing a BST.  Let u,v be two nodes such that u=parent(v)  Keys(T1) < key(v) < keys(T2) < key (u) < keys(T3) u v T3 T1 T2 4
  • 5. Insertion  Insertion happens in subtree T1.  ht(T1) increases from h to h+1. z  Since x remains balanced ht(T2) y T4 is h or h+1 or h+2. h+1 to h+2  If ht(T2)=h+2 then x is originally x unbalanced T3  If ht(T2)=h+1 then ht(x) does not increase. T1 T2  Hence ht(T2)=h. h to h+1 h  So ht(x) increases from h+1 to h+2. 5
  • 6. Insertion(2)  Since y remains balanced, h+3 z ht(T3) is h+1 or h+2 or h+3.  If ht(T3)=h+3 then y is originally h+2 to h+3 y T4 unbalanced. h+1  If ht(T3)=h+2 then ht(y) does not x increase. h+1 to h+2 T3  So ht(T3)=h+1. h+1 T1 T2  So ht(y) inc. from h+2 to h+3. h to h+1 h  Since z was balanced ht(T4) is h+1 or h+2 or h+3.  z is now unbalanced and so ht(T4)=h+1. 6
  • 7. Single rotation h+3 z rotation(y,z) h+2 to h+3 y h+1 h+3 y T4 x h+2 x z h+2 h+1 to h+2 T3 h+1 T1 T2 T1 T2 T3 T4 h+1 h h+1 h+1 h to h+1 h The height of the subtree remains the same after rotation. Hence no further rotations required 7
  • 8. Double rotation h+4 z h+3 z h+3 x T4 h+1 rotation(x,y) h+2 y T3 h+2 to h+3 y h T4 h+1 T1 T2 x h+1 to h+2 h+1 h+1 h+1 T1 rotation(x,z) h to h+1 T2 T3 h x h+3 Final tree has same height h+2 y z h+2 as original tree. Hence we need not go further up the T1 T2 T3 T4 h+1 h+1 h h+1 tree. 8
  • 9. Restructuring  The four ways to rotate nodes in an AVL tree, graphically represented -Single Rotations: a=z single rotation b=y b=y a=z c=x c=x T0 T3 T1 T3 T0 T1 T2 T2 c=z single rotation b=y b=y a=x c=z a=x T3 T3 T0 T2 T2 T1 T0 9 T1
  • 10. Restructuring (contd.)  double rotations: a=z double rotation b=x c=y a=z c=y b=x T0 T2 T2 T3 T0 T1 T3 T1 c=z double rotation b=x a=y a=y c=z b=x T0 T2 T3 T2 T3 T1 T0 T1 10
  • 11. Deletion  When deleting a node in a BST, we either delete a leaf or a node with only one child.  In an AVL tree if a node has only one child then that child is a leaf.  Hence in an AVL tree we either delete a leaf or the parent of a leaf.  Hence deletion can be assumed to be at a leaf. 11
  • 12. Deletion(2)  Let w be the node deleted.  Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with larger height, and let x be the child of y with larger height.  We perform rotations to restore balance at the subtree rooted at z.  As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached 12
  • 13. Deletion(3)  Suppose deletion happens in subtree T4 h+2 z and its ht. reduces from h+1 y h to h-1. T4 h to h-1  Since z was balanced h x h or h-1 but is now unbalanced, T3 ht(y) = h+1. T1 T2 h-1 or h-2  x has larger ht. than T3 h-1 or h-2 and so ht(x)=h.  Since y is balanced ht(T3)= h or h-1 13
  • 14. Deletion(4) h+2 z  Since ht(x)=h, and x is balanced ht(T1), ht(T2) h+1 y T4 h to h-1 is h-1 or h-2. h x  However, both T1 and T3 h or h-1 T2 cannot have ht. h-2 T1 T2 h-1 or h-2 h-1 or h-2 14
  • 15. Single rotation (deletion) h+2 z rotation(y,z) h+1 or h+2 h+1 y T4 h to h-1 y h or h+1 h x h or h-1 h x z T3 T1 T2 h-1 or h-2 T1 T2 T3 T4 h-1 or h-2 h-1 or h-2 h-1 or h-2 h or h-1 h-1 After rotation height of subtree might be 1 less than original height. In that case we continue up the tree 15
  • 16. Deletion: another case h+2 z  As before we can claim that ht(y)=h+1 and h+1 y T4 h to h-1 ht(x)=h. T1 x h  Since y is balanced h-1 ht(T1) is h or h-1. T2 T3  If ht(T1) is h then we h-1 or h-2 h-1 or h-2 would have picked x as the root of T1.  So ht(T1)=h-1 16
  • 17. Double rotation z h+2 h+1 x T4 h-1 h+2 z rotation(x,y) h y T3 h-1 or h-2 h+1 y T4 h to h-1 T1 T2 x h h-1 h-1 or h-2 T1 h-1 rotation(x,z) T2 T3 h-1 or h-2 h-1 or h-2 x h+1 h y h Final tree has height less z than original tree. Hence we T1 T2 T3 T4 h-1 need to continue up the tree h-1 h-1 or h-2 h-1 or h-2 17
  • 18. Running time of insertion & deletion  Insertion  We perform rotation only once but might have to go O(log n) levels to find the unbalanced node.  So time for insertion is O(log n)  Deletion  We need O(log n) time to delete a node.  Rebalancing also requires O(log n) time.  More than one rotation may have to be performed. 18