0% found this document useful (0 votes)
26 views25 pages

Daniel Kane: Data Structures

This document discusses AVL trees, which are self-balancing binary search trees. It outlines how to implement insertion and deletion in AVL trees to maintain the property that the heights of any node's children differ by at most one. Insertion may require rebalancing the tree by performing rotations. Deletion can also change the balance, so rebalancing may be needed after deleting a node as well. The goal of AVL trees is to support basic binary search tree operations like insertion, deletion and search in O(log n) time.

Uploaded by

ljklefkj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views25 pages

Daniel Kane: Data Structures

This document discusses AVL trees, which are self-balancing binary search trees. It outlines how to implement insertion and deletion in AVL trees to maintain the property that the heights of any node's children differ by at most one. Insertion may require rebalancing the tree by performing rotations. Deletion can also change the balance, so rebalancing may be needed after deleting a node as well. The goal of AVL trees is to support basic binary search tree operations like insertion, deletion and search in O(log n) time.

Uploaded by

ljklefkj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Binary Search Trees:

AVL Tree
Implementation

Daniel Kane
Department of Computer Science and Engineering
University of California, San Diego

Data Structures
http://bit.ly/algospecialization
Learning Objectives
Implement AVL trees.
Understand the cases required for
rebalancing algorithms.
Outline

1 AVL Trees

2 Insert

3 Delete
AVL Trees
Need ensure that children have nearly the
same height.
Problem
Updates to the tree can destroy this property.
Problem
Updates to the tree can destroy this property.

Need to correct this.


Errors
Heights stay the same except on the
insertion path.

Only need to worry about this path.


Problem
Which insertion would require the tree to be
rebalanced in order to maintain the AVL
property?
Problem
Which insertion would require the tree to be
rebalanced in order to maintain the AVL
property?
Outline

1 AVL Trees

2 Insert

3 Delete
Insertion

We need a new insertion algorithm that


involves rebalancing the tree to maintain the
AVL property.
Idea

AVLInsert(k, R)
Insert(k, R)
N ← Find(k, R)
Rebalance(N)
Rebalancing
If
|N.Left.Height − N.Right.Height| ≤ 1

ne.
Problem
Diculty if heights dier by more.
Problem
Diculty if heights dier by more.

Never more than 2.


Code
Rebalance(N)
P ← N.Parent
if N.Left.Height > N.Right.Height + 1:
RebalanceRight(N)
if N.Right.Height > N.Left.Height + 1:
RebalanceLeft(N)
AdjustHeight(N)
if P ̸= null :
Rebalance(P)
Adjust Height
AdjustHeight(N)

N.Height ← 1+ max(
N.Left.Height,
N.Right.Height)
Rebalancing
If left subtree too heavy, rotate right:
Bad Case
Doesn't work in this case.
Fix
Must rotate left rst.
Rebalance
RebalanceRight(N)
M ← N.Left
if M.Right.Height > M.Left.Height:
RotateLeft(M)
RotateRight(N)
AdjustHeight on affected nodes
Outline

1 AVL Trees

2 Insert

3 Delete
Delete
Deletions can also change balance.
New Delete

AVLDelete(N)
Delete(N)
M ← Parent of node replacing N
Rebalance(M)
Conclusion

Summary
AVL trees can implement all of the basic
operations in O(log(n)) time per operation.

You might also like