Docsity

AVL Trees: Balanced Binary Search Trees, Slides of Data Structures and Algorithms

Partial preview of the text

Download AVL Trees: Balanced Binary Search Trees and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 18:

AVL Trees (cont.)

Today …

• In-place mergesort

• Midterm overview

• AVL Trees (cont.) [Ch 12: pp. 681-686]

• Heapsort exercise

CPSC 223 -­‐-­‐ Fall 2010

Midterm Overview …

CPSC 223 -­‐-­‐ Fall 2010

Midterm

  • There will be 6-7 questions
  • Open book / open notes
  • Closed computer / smart phone / etc.
  • Be sure you understand:
    • Answers to quizzes and exercises
    • Answers to questions on slides
  • Be sure you have done the reading assignments
  • Worth 10% of your grade CPSC 223 -­‐-­‐ Fall 2010

Topics we’ve covered since last time

  • Binary search trees (cont.)
    • The costs of operations
    • Degenerate cases
  • Heaps
    • Structure and ordering constraint
    • Array-based representation
    • FindMax, DeleteMax, Insert
    • Trickle down (delete) and trickle up (insert)
    • Heapsort (again) CPSC 223 -­‐-­‐ Fall 2010

Topics we’ve covered since last time

  • AVL trees
    • Why balanced search trees are good
    • Balance factors
    • Rotations (single, double)
    • Expect at most one question on AVL trees

Questions?

CPSC 223 -­‐-­‐ Fall 2010

CPSC 223 -­‐-­‐ Fall 2010

AVL Trees

AVL Trees [Adelson-Velskii & Landis, 1962]

  • Use “ tree rotations ” to rebalance the tree
  • Do tree rotations (if needed) after insert or delete
  • Four cases:
    • Single rotation (“ left-left ”)
    • Single rotation (“ right-right ”)
    • Double rotation (“ left-right ”)
    • Double rotation (“ right-left ”)
  • Traverse up the tree from inserted/deleted node
    • Only necessary if an insertion/deletion changes the balance
  • Compute a “ balance factor ” at each node CPSC 223 -­‐-­‐ Fall 2010

AVL Trees

  • General case for single rotation (“ right-right ”)
    • Insertion in the right subtree of the right child of k 2 (subtree A)
    • This is just the “ mirror image ” of the left-left case CPSC 223 -­‐-­‐ Fall 2010 k 2 k 1 b = - b = - A B The rota7on sets the subtree to its original height! C k 2 b = 0 k 1 b = 0 B Single rota-on (right-­‐right case) A C

AVL Trees -- Exercise

// return new root after left-left rotation Node* rotateWithLeftChild(Node* k2) { ... } // return new root after right-right subtree Node* rotateWithRightChild(Node* k1) { … } CPSC 223 -­‐-­‐ Fall 2010

AVL Trees -- Exercise

// return new root after left-left rotation Node* rotateWithLeftChild (Node* k2) { Node* k1 = k2->leftChild; k2->leftChild = k1->rightChild; k1->rightChild = k2; return k1; } CPSC 223 -­‐-­‐ Fall 2010

AVL Trees -- Exercise

// return new root after right-right rotation Node* rotateWithRightChild (Node* k1) { Node * k2 = k1->rightChild; k1->rightChild = k2->leftChild; k2->leftChild = k1; return k2; } CPSC 223 -­‐-­‐ Fall 2010

AVL Trees

  • General case for double rotation (left-right)

continued …

CPSC 223 -­‐-­‐ Fall 2010 k 3 b = 0 or 1^ k 2 b = 2 D A B k 1 C b = 1 or 2 Double rota-on (le/-­‐right case) Second rota-on k 2 k 3 A B D k 1 C

AVL Trees – Double Rotation (“left-right”)

// return new root of left-right rotation Node* doubleRotateWithLeftChild (Node* k3) { ... } CPSC 223 -­‐-­‐ Fall 2010

AVL Trees – Double Rotation (“left-right”)

// return new root of left-right rotation Node* doubleRotateWithLeftChild (Node* k3) { k3->leftChild = rotateWithRightChild(k3->leftChild); return rotateWithLeftChild(k3); } CPSC 223 -­‐-­‐ Fall 2010

AVL Trees – Double Rotation (“ right-left” )

// return new root of right-left rotation Node* doubleRotateWithRightChild (Node* k1) { k1->rightChild = rotateWithLeftChild (k1->rightChild); return rotateWithRightChild (k1); } CPSC 223 -­‐-­‐ Fall 2010

Comments on AVL Trees

  • AVL vs other approaches
    • Rotations and traversals are hard to get right
    • But more importantly, the traversals create overhead
  • A Red Black tree is an alternative self-balancing

approach

  • In practice, (slightly) faster insertion and deletion
  • But (slightly) slower retrieval time
  • Red-black trees are often implemented in practice CPSC 223 -­‐-­‐ Fall 2010