AVL Trees
Splay Trees
Roadmap
- Last time
- Tree
- Heaps and Priority Queues....
- Today
- AVL Trees
- Or how to make binary trees efficient!
- Splay Trees
- Simpler implementation
- Good amortized bounds
Balanced structures
- Standard Binary Tree
- worst case performance is O(n)
- Take advantage of partioning scheme
- Idea
- Identical to binarySearch
- Get down to logarithmic running time for all operation
- Insight
- Keep both partition balanced (approximately same size)
- Enfore additional properties to achieve this goal
AVL Tree
- Name: Authors of the structure
- Adel'son, Vel'skii and Landis
- Additional key property
- Height-balance property
- For every internal node v of T, the heights of the children of v can differ by at most 1
- Novelty in the implementation
- Modification to the insertion/removal code necessary to enforce the height-balance property at all times.
An AVL Tree Example
Balancing and Logarithms
- Why is balancing important ?
- Yields logarithmic bounds for the algorithm...
- ...because the height of the tree and its size
- ...are related as follow:
- Proof
- By induction on the tree height...
Maintaining a Balance
- How to achieve this goal ?
- Requirements
- being able to detect that a tree is becoming unbalanced
- reorganize the tree to restore the property
- do it recursively to preserve the property across the whole structure
- Side issue: How to detect that a tree is unbalanced ?
- Awaiting your suggestions!
Detecting Unbalanced Trees
- Idea
- Compute the height of each subtree
- Test for |h(left(v)) - h(right(v))| <= 1
- Downside ?
- Cost for the computation of h(v) ?
- We will fix that later.
Repair Method
- Imbalance: at most 1
- Two symmetric cases
Single rotation
- Repair Idea
- Reduce depth(root(C))
- How ?
S.R. Restructuring
- Find another tree that preserves the ordering
- LeftRotate(a)
- RightRotate(b)
Double rotation
- Repair Idea
- Reduce depth(root(B))
- How ?
D.R. Restructuring
Restructuring
Restructing cont'd
Restructuring summary
- Let
- x be the root of the red tree (C or d)
- y be the parent of x
- z be the grand-parent of x
- restructure(x) Perform either
- One rotation (first case)
- A sequence of two rotations to reduce the height
- Beware!
- Symmetric cases on the left
- Implementation: update all pointers!
Insertion in an AVL
- Idea
- Perform standard insertion
- Walk back up the root
- When we find an imbalance
- do a case analysis to pick the right rotation sequence
- perform the rotation
- Done when we hit the root.
AVL Implementation
AVL Implementation
AVL Insertion Analysis
- Insertion Cost
- basicInsertion Q( ) +
- reBalance
- number of iteration: Q( )
- cost of an iteration: O( )
- Overall cost: O( )
- Can we do any better ?
- Of course! Avoid the computation of height()
- Store the height in the BTNode
- Maintain height as tree gets restructured
- Final cost with optimization: Q( )
Removal from an AVL
- Same idea!
- Perform a standard removal (splicing if necessary)
- Start at the splicing site & walk up the tree
- rebalance just like for insertions
- Complexity analysis
- Cost of standard removal: O( )
- Depth of splicing site: O( )
- Number of iterations to walk up the tree: O( )
- Cost of a rebalancing: O( )
- Overall complexity: O( )
Splay Trees
Splay Tree
- Another, simpler, balancing scheme
- Standard binary tree are sufficient
- Balancing is based on a restructuring operation called splaying
- Dowside
- It does not guarantee a O(log n) bound in the worst case. The bound is O(log n) in the amortized sense
- Roadmap
- Describe & implement splay tree
- Analyze the O(log n) bound
Splaying
- After each operation (insertion/removal/find)
- Splay the target node where the target is:
- newly inserted node
- spliced node (the one that gets removed)
- the found node
- the last node seen in a failed search
- Splaying means
- move up the tree with one of three restructuring step
- stop when the splay node reaches the root position
- Intution behind splaying
- attempt to reduce the depth along the traversed path
- exploit locality of access patterns
Zig-Zig Splaying
- Applies when the splay node x has
- a parent node y and a grandparent node z
Preserve the ordering...
Zig-Zag Splaying
- Applies when the splay node x has
- a parent node y and a grand parent node z
x,y and z are not "in line"
Zig Splaying
- Applies when the splay node x has
- a parent node y and no grand parent (almost at the root)
Insertion
Insertion
Analysis
- Does the splaying process terminate ?
- Yes, because....
- Each splaying operation reduces the depth of the splay node
- zig-zig : depth reduced by 2
- zig-zag: depth reduced by 2
- zig: depth reduced by 1
- Running time of splay(v) (v is at depth d )
- splaying a node takes: O( )
- number of splaying is: O( )
- Running time: O( )
- Worst-case bound: O( )
Amortized analysis
- Remember
- Amortization: worst case performance for a sequence of m operations is O(m log n). Therefore the amortized complexity for 1 operation becomes O(log n)
- Amortization scheme
- Overcharge some operation
- Save the extra cash in a 'bank account'
- Withdrawal from the account to pay expensive operation (when the actual cost is > log n)
- Show that there is always enough in the bank account to pay for the upcoming operation.
Amortized Analysis
- Proof mechanism
- Let Di denote the splay tree after the ith operation
- Let F(Di) denote the saving available in tree Di
- Let ci denote the actual cost of operation i
Amortization Key
- Find a function
- that has these desirable properties
- Show that, for each operation i
-
- where f(n) is the amortized cost
Amortization for Splay Trees
- Definition
- Let n(v) be the size of the subtree rooted at v
- For the root t of the tree Di, we have n(t) = 2 ni + 1
- Let the rank of node v be defined as r(v) = log n(v)
- For the root t of the tree Di, we have r(t) = log (2 ni + 1)
- Let the savings function be defined as
- Clearly, we have
Proving the Amortized Cost
- Objective
- Show that, for a search, the amortized cost is O(log n)
- Technique
- Establish the real cost
- Establish the variation in the savings
- Do so for each splaying
- Sum up all the splaying cost
- Sum them up. It should be O(log n)
Real Cost
- Let x be the node we splay in Di
- Let d be the depth of x in Di
- Depth reduction
- At zig-zig & zig-zag: 2
- At zig: 1.
- There is at most 1 zig
- Number of splaying step: d/2
- Real Cost is therefore: d
Savings Variation
Zig-zig Proof
- Facts
- Mathematical fact:
- Delta:
Zig-zig Proof cont'd
Zig-zag & Zig Proofs
- Similar mechanism (check the book)
- What is the total variation ?
- Sum up the d/2 splaying step!
Total cost:
Wrapping Up the proof
- Total Cost
- Fact
- Consequence
What is Left...
- Do the same proof for
- Insertion
- same technique
- note that
- Collapsing summation for delta.
- Removal (easy)
- the ranks of the ancestor of x get smaller
- Delta becomes negative, savings pay for the operation.
- Read the material in the book!!!! (13.4)