AVL Trees: Balanced Binary Search Trees - Prof. David J. Galles, Study notes of Data Structures and Algorithms

An in-depth exploration of avl trees, a self-balancing binary search tree algorithm. Various aspects such as tree rotations, insertions, and deletions. Avl trees ensure good performance by maintaining a balance between the height of left and right subtrees.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-0q2
koofers-user-0q2 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS245-2009S-22 AVL Trees 1
22-0: Binary Search Trees
Good performance O(lg n)when the tree is balanced
Bad performance O(n)when the tree is unbalanced
What if we could ensure that the trees were always balanced?
22-1: Balanced BSTs
Try #1: insist that left and right subtrees of all nodes have the same height.
Will that work? why/why not?
22-2: Balanced BSTs
Try #2: insist that left and right subtrees of all nodes have a height that differs by at most 1
examples
AVL tree (Adelson-Velskii and Landis)
22-3: AVL Tree insert
Do a stanard BST insert
Result may not be an AVL Tree
Why?
22-4: AVL Tree insert
Do a stanard BST insert
Result may not be an AVL Tree
Insertions may unbalance the tree (example!)
Solution: Tree rotations
22-5: Tree Rotation
A
B
X Y
Z
Valid BST. X,Y,Z are subtrees
pf3
pf4
pf5
pf8

Partial preview of the text

Download AVL Trees: Balanced Binary Search Trees - Prof. David J. Galles and more Study notes Data Structures and Algorithms in PDF only on Docsity!

22-0: Binary Search Trees

  • Good performance O(lg n) when the tree is balanced
  • Bad performance O(n) when the tree is unbalanced
  • What if we could ensure that the trees were always balanced?

22-1: Balanced BSTs

  • Try #1: insist that left and right subtrees of all nodes have the same height.
    • Will that work? why/why not?

22-2: Balanced BSTs

  • Try #2: insist that left and right subtrees of all nodes have a height that differs by at most 1
    • examples
  • AVL tree (Adelson-Velskii and Landis)

22-3: AVL Tree insert

  • Do a stanard BST insert
  • Result may not be an AVL Tree
    • Why?

22-4: AVL Tree insert

  • Do a stanard BST insert
  • Result may not be an AVL Tree
    • Insertions may unbalance the tree (example!)
    • Solution: Tree rotations

22-5: Tree Rotation

A

B

X Y

Z

Valid BST. X,Y,Z are subtrees

22-6: Tree Rotation

A

B

X Y

Z

T

A

X B

Y

Z

T

If T1 is a valid BST, T2 is also a valid BST

22-7: Tree Rotation

A

B

X Y

Z

A

X B

Y Z

Right Rotation

22-8: Tree Rotation

A

B

X Y

Z

A

X B

Y Z

Left Rotation

22-9: AVL Tree Insert

  • Inserting an element may cause an imbalance
  • Do a rotation to correct the imbalance

22-10: AVL Tree Insert

1

2

3

Left Rotate

around this edge

22-13: AVL Tree Insert

1

2

3

22-14: AVL Trees

  • Like “regular” Binary Search Trees, except:
    • Each node stores the height of the tree rooted at that node
    • For every node, height of left subtree is no more than 1 different than the height of the right subtree
    • Insertions (and deletions!) need to update the heights, and ensure that the AVL property is always main- tained

22-15: AVL Tree Insert

  • Do a standard BST Insert
  • Travel up the tree from the inserted node
    • Correct heights as you go
    • If you hit an imbalance, do a rotation (correcting heights as necessary)
    • A single rotation will balance the tree

(Examples using whiteboard / visualization) 22-16: AVL Tree Insert

  • Do a standard BST Insert
  • Travel up the tree from the inserted node
    • How do we travel up the tree from the inserted node?

22-17: AVL Tree Insert

  • Do a standard BST Insert
  • Travel up the tree from the inserted node
    • How do we travel up the tree from the inserted node?
      • Go down the tree recursively
      • Path back to the root is stored on the call stack

22-18: Double Rotation

1

2

5

3

Insert a 4

22-19: Double Rotation

height = 1

height = 2

height = 3

height = 4

height = 1

22-20: Double Rotation

height = 1

height = 2

height = 3

height = 4

height = 1

Right Rotate

  • Standard BST insert
  • Travel up the tree from the inserted node
    • (Using the recursive call stack)
  • Recompute heights as you go
  • If a height imbalance is found, do a rotation
    • If the inserted element is between the elements along the offending edge, do a double rotate
    • If the inserted element is not between the elements alon the offending edge, do a single rotation
  • After a rotation (either single or double) is done, tree will be balanced again

22-26: AVL Tree Delete

  • Standard BST delete
  • Travel up the tree from the deleted node
    • Using the recursive call stack
  • Single or double rotation as necessary
    • Just like insert

22-27: AVL Trees

  • How unbalanced can AVL trees be?
  • When are AVL trees as unbalanced as possible?
  • Examples?

22-28: AVL Trees

  • Minimum number of nodes in an AVL tree of height h?

N (0) = 1 N (h) = N (h − 1) + N (h − 2)

  • Hard to solve, settle for a bound
  • N (k) ≥ (3/2)k

22-29: AVL Trees

N (0) = 1 N (h) = N (h − 1) + N (h − 2) + 1

N (h) = N (h − 1) + N (h − 2) + 1 ≥ (3/2)h−^1 + (3/2)h−^2 + 1 ≥ (3/2)h−^2 (1 + 3/2) + 1 ≥ (3/2)h−^2 (5/2) + 1 ≥ (3/2)h^ + 1 ≥ (3/2)h 22-30: AVL Trees

  • of nodes in an AVL tree of height h is at least (3/2)h

  • Maximum height of an AVL tree with n nodes is:

22-31: AVL Trees

  • of nodes in an AVL tree of height h is at least (3/2)h

  • Maximum height of an AVL tree with n nodes is:
    • log 3 / 2 n ∈ lg n