CS 240 Fall 2015 Mike Lam, Professor I find your lack of balance - - PowerPoint PPT Presentation

cs 240 fall 2015
SMART_READER_LITE
LIVE PREVIEW

CS 240 Fall 2015 Mike Lam, Professor I find your lack of balance - - PowerPoint PPT Presentation

CS 240 Fall 2015 Mike Lam, Professor I find your lack of balance disturbing. Balanced (AVL) Trees Review Binary Search Trees (BSTs) Ordered binary tree Operations are O(h) where h is the height of the tree For mostly-random


slide-1
SLIDE 1

CS 240 Fall 2015

Mike Lam, Professor

Balanced (AVL) Trees

I find your lack of balance disturbing.

slide-2
SLIDE 2

Review

  • Binary Search Trees (BSTs)

– Ordered binary tree – Operations are O(h) where h is the height of the tree – For mostly-random insertions and deletions, h ≈ log n

  • As in the last part of Monday's lab

– For other situations, we need to use a more "balanced"

binary tree implementation

  • General idea: restore balance after every add or remove
  • However, this will take extra work!
  • Tradeoff between cost and benefit of balancing
slide-3
SLIDE 3

Issues

  • How much should we re-balance?

– How often? How many nodes? How strict? – How do we measure balance?

  • We could re-balance the entire tree after every insertion

– This would lead to O(n log n) insertion times – Essentially re-build the tree every time – This seems quite excessive!

  • Goal: faster insertions and "good enough" balance

– AVL trees (easiest to understand) – Red-Black trees – Many others...

slide-4
SLIDE 4

AVL Trees

  • AVL Trees

– Inventors G. M. Adelson-Velsky and E. M. Landis (1962) – Balance factor: height(node->left) – height(node->right) – Height-balance property

  • Heights of children differ by at most one
  • Balance factor is -1, 0, or 1

– Enforce this property using tree rotations

Image from: http://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/

slide-5
SLIDE 5

Tree Height

  • Height: # of edges from root to furthest leaf

– Minor extension: height of an empty tree will be -1 – Applies to whole trees as well as subtrees

Caveat: Heights can't really be negative (what would it mean to have negative edges?), but it does make the math work better...

slide-6
SLIDE 6

Binary Search Tree

44 17 8 32 28 29 88 65 97 93 54 82 76 80

slide-7
SLIDE 7

Binary Search Tree

44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0

slide-8
SLIDE 8

Binary Search Tree

44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0 h=1 h=1 h=1

slide-9
SLIDE 9

Binary Search Tree

44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0 h=1 h=1 h=1 h=-1 h=-1

slide-10
SLIDE 10

Binary Search Tree

44 17 8 32 28 29 88 65 97 93 54 82 76 80 h=0 h=0 h=0 h=0 h=0 h=1 h=1 h=1

slide-11
SLIDE 11

Imbalances

  • How do we fix imbalances?

– Need to re-arrange tree

  • Solution: Rotations!
  • Example:
slide-12
SLIDE 12

Imbalances

  • How do we fix imbalances?

– Need to re-arrange tree

  • Solution: Rotations!
  • Example:
slide-13
SLIDE 13

Rotations

  • Rotations

– Single rotation (below) – Single/double rotations (right)

  • Four cases of trinode restructuring

x x y y

x->right = y->left y->left = x y->left = x->right x->right = y

"right rotation" "left rotation"

slide-14
SLIDE 14

AVL Trees

  • Insertion

– Insert into BST as usual

  • Binary search until empty subtree is found, then add a new node

– Check ancestors of new node for imbalances – Fix imbalances via trinode restructuring

  • Removal

– Remove from BST as usual – Check ancestors of removed node for imbalances – Fix imbalances via trinode restructuring

slide-15
SLIDE 15

AVL Tree

44 17 32 78 50 88 48 62

Insert 54

h=0 h=0 h=0 h=0 h=1 h=1 h=2 h=3

slide-16
SLIDE 16

AVL Tree

44 17 32 78 50 88 48 62

Update heights of ancestors and check for imbalances

h=0 h=0 h=0 h=0 h=1 h=1 h=2 h=3 54 h=0

slide-17
SLIDE 17

AVL Tree

44 17 32 78 50 88 48 62

Update heights of ancestors and check for imbalances

h=0 h=1 h=0 h=0 h=1 h=1 h=2 h=3 54 h=0

slide-18
SLIDE 18

AVL Tree

44 17 32 78 50 88 48 62

Update heights of ancestors and check for imbalances

h=0 h=1 h=0 h=0 h=2 h=1 h=2 h=3 54 h=0

slide-19
SLIDE 19

AVL Tree

44 17 32 78 50 88 48 62

Imbalance detected

h=0 h=1 h=0 h=0 h=2 h=1 h=3 h=4 54 h=0

slide-20
SLIDE 20

AVL Tree

44 17 32 78 50 88 48 62

Nodes involved

h=0 h=1 h=0 h=0 h=2 h=1 h=3 h=4 54 h=0

slide-21
SLIDE 21

AVL Tree

44 17 32 78 50 88 48 62

Rebalance

h=0 h=1 h=0 h=0 h=2 h=1 h=3 h=4 54 h=0

slide-22
SLIDE 22

AVL Tree

44 17 32 78 62 88 50 54

Rebalance

h=1 h=0 h=0 h=0 h=2 h=1 h=3 h=4 48 h=0

slide-23
SLIDE 23

AVL Tree

44 17 32 62 50 78 48 54

Balanced subtree; continue up tree to root

h=0 h=0 h=0 h=1 h=1 h=1 h=2 h=3 88 h=0

slide-24
SLIDE 24

AVL Tree

44 17 32 62 50 78 48 54

Done!

h=0 h=0 h=0 h=1 h=1 h=1 h=2 h=3 88 h=0

slide-25
SLIDE 25

Exercises

  • 3, 9, 12, 5, 4, 1
  • 10, 5, 7, 15, 9, 25
  • Show every step!

– Mark rotations as L or R

To check your answers:

http://www.qmatica.com/DataStructures/Trees/AVL/AVLTree.html

slide-26
SLIDE 26

AVL Tree Analysis

  • In general: n(h) = 1 + n(h-1) + n(h-2)

– n(h) = number of nodes in AVL tree of height h – Reasoning: AVL tree with minimal number of nodes has one

node and two subtrees: one with height h-1 and one with height h-2

  • (They differ by at most one b/c of the balance property, and you can't

skip heights)

  • This is a Fibonacci progression

– n(h) >= Fib(h) = (Φ = golden ratio) – Exponential w.r.t. height: n(h) is Ω(Φh) – Thus, h is O(log n)

Φ

h

√ 5⌋

slide-27
SLIDE 27

Alternative: Red-Black Trees

  • Coloring scheme

– Root is colored black – All children of a red node must be colored black (no "double reds") – All nodes with zero or one children have the same number of black-colored ancestors

  • Path from root to furthest leaf is no more than twice as long as the path from

root to nearest leaf

  • Less-strictly balanced
  • Faster insertion/removal but slower lookups