logo
  • Writing Services
    • Writing Services
      • Essays & Papers
      • Essay Service
      • Write my research paper
      • Apa paper writing service
      • Dissertation writing services
      • Coursework writing service
      • Homework & Assignment
      • Pay for homework
      • English assignment help
      • Do my assignment
      • Assignment help
      • Finance assignment help
      • Do my math homework
      • Spss assignment help
      • Do my homework
      • Biology assignment help
      • Chemistry assignment help
      • More services
      • Personal statement writing service
      • Do my excel assignment
      • Law essay writing service
      • Annotated bibliography writing service
      • Nursing assignment help
      • Write my lab report
      • Capstone project writing service
      • Do my powerpoint presentation
  • Study Resources
    • Study Resources
      • Universities
      • Courses
      • Documents

Lecture Note

Lecture 7: Binary Trees II: AVL

  • University:

    Massachusetts Institute of Technology
  • Course:

    6.006 | Introduction to Algorithms
  • Academic year:

    2020

  • Views:

    202

    Pages:

    5

  • Author:

    Genevieve Barron

  • Last update:

    12.12.2024

Introduction to Algorithms: 6.006 Massachusetts Institute of Technology Instructors: Erik Demaine, Jason Ku, and Justin Solomon Lecture 7: Binary Trees II: AVL Lecture 7: Binary Trees II: AVL Last Time and Today’s Goal Operations O(·) Sequence Data Structure Container Static Dynamic build(X) get at(i) insert first(x) insert last(x) insert at(i, x) set at(i,x) delete first() delete last() delete at(i) Binary Tree n h h h h AVL Tree n log n log n log n log n Set Data Structure Container Static build(X) find(k) Operations O(·) Dynamic Order insert(x) find min() find prev(k) delete(k) find max() find next(k) Binary Tree n log n h h h h AVL Tree n log n log n log n log n log n Height Balance • How to maintain height h = O(log n) where n is number of nodes in tree? • A binary tree that maintains O(log n) height under dynamic operations is called balanced – There are many balancing schemes (Red-Black Trees, Splay Trees, 2-3 Trees, . . . ) – First proposed balancing scheme was the AVL Tree (Adelson-Velsky and Landis, 1962) Rotations • Need to reduce height of tree without changing its traversal order, so that we represent the same sequence of items • How to change the structure of a tree, while preserving traversal order? Rotations! 1 2 3 4 5 _______ ____ / \ / \ / \ /___\ /___\ /___\ rotate_right() => <= rotate_left() _______ ____ / \ /___\ / \ / \ /___\ /___\ • A rotation relinks O(1) pointers to modify tree structure and maintains traversal order 2 Lecture 7: Binary Trees II: AVL Rotations Suffice • Claim: O(n) rotations can transform a binary tree to any other with same traversal order. • Proof: Repeatedly perform last possible right rotation in traversal order; resulting tree is a canonical chain. Each rotation increases depth of the last node by 1. Depth of last node in final chain is n − 1, so at most n − 1 rotations are performed. Reverse canonical rotations to reach target tree. • Can maintain height-balance by using O(n) rotations to fully balance the tree, but slow :( • We will keep the tree balanced in O(log n) time per operation! AVL Trees: Height Balance • AVL trees maintain height-balance (also called the AVL Property) – A node is height-balanced if heights of its left and right subtrees differ by at most 1 – Let skew of a node be the height of its right subtree minus that of its left subtree – Then a node is height-balanced if its skew is −1, 0, or 1 • Claim: A binary tree with height-balanced nodes has height h = O(log n) (i.e., n = 2Ω(h) ) • Proof: Suffices to show fewest nodes F (h) in any height h tree is F (h) = 2Ω(h) F (0) = 1, F (1) = 2, F (h) = 1+F (h−1)+F (h−2) ≥ 2F (h−2) =⇒ F (h) ≥ 2h/2 • Suppose adding or removing leaf from a height-balanced tree results in imbalance – Only subtrees of the leaf’s ancestors have changed in height or skew – Heights changed by only ±1, so skews still have magnitude ≤ 2 – Idea: Fix height-balance of ancestors starting from leaf up to the root – Repeatedly rebalance lowest ancestor that is not height-balanced, wlog assume skew 2 Lecture 7: Binary Trees II: AVL 3 • Local Rebalance: Given binary tree node : – whose skew 2 and – every other node in ’s subtree is height-balanced, – then ’s subtree can be made height-balanced via one or two rotations – (after which ’s height is the same or one less than before) • Proof: – Since skew of is 2, s right child exists – Case 1: skew of is 0 or Case 2: skew of is 1 ∗ Perform a left rotation on 1 2 3 4 5 6 ________ ______ / \ /___\ / \ / \ /___\ / \ /_____\ /_____\ __________ _____ / \ / \ / \ / \ /___\ /___\ /_____\ /_____\ => ∗ Let h = height(). Then height() = h + 1 and height() is h + 1 in Case 1, h in Case 2 ∗ After rotation: · the skew of is either 1 in Case 1 or 0 in Case 2, so is height balanced · the skew of is −1, so is height balanced · the height of before is h + 3, then after is h + 3 in Case 1, h + 2 in Case 2 – Case 3: skew of is −1, so the left child of exists ∗ Perform a right rotation on , then a left rotation on 1 2 3 4 5 6 _____________ _______ / \ ____ /___\ / \ /_\ /_\ /___\ /___\ /___\ => ___________ ____ ____ / \ /_\ /_\ / \ /___\ /___\ /___\ /___\ ∗ Let h = height(). Then height() = h while height() and height() are each either h or h − 1 ∗ After rotation: · the skew of is either 0 or −1, so is height balanced · the skew of is either 0 or 1, so is height balanced · the skew of is 0, so D is height balanced · the height of is h + 3 before, then after is h + 2 4 Lecture 7: Binary Trees II: AVL • Global Rebalance: Add or remove a leaf from height-balanced tree T to produce tree T 0 . Then T 0 can be transformed into a height-balanced tree T 00 using at most O(log n) rotations. • Proof: – Only ancestors of the affected leaf have different height in T 0 than in T – Affected leaf has at most h = O(log n) ancestors whose subtrees may have changed – Let be lowest ancestor that is not height-balanced (with skew magnitude 2) – If a leaf was added into T : ∗ Insertion increases height of , so in Case 2 or 3 of Local Rebalancing ∗ Rotation decreases subtree height: balanced after one rotation – If a leaf was removed from T : ∗ Deletion decreased height of one child of , not , so only imbalance ∗ Could decrease height of by 1; parent of may now be imbalanced ∗ So may have to rebalance every ancestor of , but at most h = O(log n) of them • So can maintain height-balance using only O(log n) rotations after insertion/deletion! • But requires us to evaluate whether possibly O(log n) nodes were height-balanced Computing Height • How to tell whether node is height-balanced? Compute heights of subtrees! • How to compute the height of node ? Naive algorithm: – Recursively compute height of the left and right subtrees of – Add 1 to the max of the two heights – Runs in Ω(n) time, since we recurse on every node :( • Idea: Augment each node with the height of its subtree! (Save for later!) • Height of can be computed in O(1) time from the heights of its children: – Look up the stored heights of left and right subtrees in O(1) time – Add 1 to the max of the two heights • During dynamic operations, we must maintain our augmentation as the tree changes shape • Recompute subtree augmentations at every node whose subtree changes: – Update relinked nodes in a rotation operation in O(1) time (ancestors don’t change) – Update all ancestors of an inserted or deleted node in O(h) time by walking up the tree Lecture 7: Binary Trees II: AVL 5 Steps to Augment a Binary Tree • In general, to augment a binary tree with a subtree property P, you must: – State the subtree property P() you want to store at each node – Show how to compute P() from the augmentations of ’s children in O(1) time • Then stored property P() can be maintained without changing dynamic operation costs Application: Sequence • For sequence binary tree, we needed to know subtree sizes • For just inserting/deleting a leaf, this was easy, but now need to handle rotations • Subtree size is a subtree property, so can maintain via augmentation – Can compute size from sizes of children by summing them and adding 1 Conclusion • Set AVL trees achieve O(lg n) time for all set operations, except O(n log n) time for build and O(n) time for iter • Sequence AVL trees achieve O(lg n) time for all sequence operations, except O(n) time for build and iter Application: Sorting • Any Set data structure defines a sorting algorithm: build (or repeatedly insert) then iter • For example, Direct Access Array Sort from Lecture 5 • AVL Sort is a new O(n lg n)-time sorting algorithm

Lecture 7: Binary Trees II: AVL

Lecture 7: Binary Trees II: AVL - Page 1
Lecture 7: Binary Trees II: AVL - Page 2
Lecture 7: Binary Trees II: AVL - Page 3
Lecture 7: Binary Trees II: AVL - Page 4
Lecture 7: Binary Trees II: AVL - Page 5
of 5
0/0
Fair Use Policy

EduBirdie considers academic integrity to be the essential part of the learning process and does not support any violation of the academic standards. Should you have any questions regarding our Fair Use Policy or become aware of any violations, please do not hesitate to contact us via support@edubirdie.com.

logo

Popular Services

  • Essay writing service
  • Pay someone to do my homework
  • Do my assignment for me
  • Research paper writing services
  • Dissertation writing services
  • Do my homework

Study Resources

  • Essay Examples
  • Blog
  • Study Notes

About Us

  • How it works?
  • Testimonials
  • FAQ
  • Money back guarantee

Contact us

mail icon
  • support@edubirdie.com
phone-icon
  • +3 (595) 691 8356
  • +1 (888) 337 5415
DMCA.com Protection Status

For press

  • Press and media
  • Brand assets
Facebook Twitter Instagram LinkedIn

Local sites

  • Ca.EduBirdie.com- The Best Essay Writing Service for Canadian Students

We accept

2024 © EduBirdie.com. All rights reserved

RADIOPLUS EXPERTS LTD. Louki Akrita, 23 Bellapais Court, Flat/Office 46 1100, Nicosia, Cyprus

Privacy Policy
|
Terms of Use
|
Fair use policy
Subscription rules
|
Referral program Rules
|
Payment and Refund Policy
|
Cookie Policy

Report

Tell us what’s wrong with it:

Thanks, got it!
We will moderate it soon!

Report

Tell us what’s wrong with it:

Almost There!

Two easy ways to download this document

or
Upgrade to premium for unlimited access
Calendar Icon

Free up your schedule!

Our EduBirdie Experts Are Here for You 24/7! Just fill out a form and let us know how we can assist you.

Unlock Icon

Take 5 seconds to unlock

Enter your email below and get instant access to your document

This field is required
Please enter a valid email address
This field is required
This field is required

By clicking 'Sign Up', you agree to our Terms and Conditions and Privacy Policy.