0% found this document useful (0 votes)
12 views30 pages

csc2001f 2024 7 Avl Trees

CSC2001 avl trees

Uploaded by

hmdprkr
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)
12 views30 pages

csc2001f 2024 7 Avl Trees

CSC2001 avl trees

Uploaded by

hmdprkr
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/ 30

AVL Trees

CSC2001F Data Structures


Jan Buys <[email protected]>

Department of Computer Science


School of IT
University of Cape Town

2024
(Based on slides from Hussein Suleman; Additional content from
Sedgewick Algorithms in Java)
AVL Trees
 To improve the worst case time complexity of BST
operations, the nodes should be arranged to minimize
the height of the tree
 A complete or perfect tree would be ideal, but this
is too restrictive: instead we aim for a balanced tree
 An AVL (Adelson-Velskii and Landis) Tree is a Binary
Search Tree with an additional balance property:
 For every node in the tree, the heights of the left
and right sub-trees are at most different by 1
department of computer science
AVL Trees
 Modify insert and delete operations to ensure the tree
stays balanced

 Advantage: Balanced tree in worst case; Faster


operations
 Disadvantage: More work for adding/removing nodes

department of computer science


Complexity Analysis
 Worst Case:
 search - O(log n)
 insert - O(log n)
 delete - O(log n)

 Maximum depth of n-item tree is O(log n)


 Proof is beyond scope here

department of computer science


Example AVL Tree
 12: lhs = 1, rhs = 0
12

 10: lhs = 0, rhs = 0


10
 14: lhs = -1, rhs = -1 14

 6: lhs = -1, rhs = -1 6 11


 11: lhs = -1, rhs = -1

department of computer science


Am I AVL or Not?
12
5

3 7

1 4 6 9

department of computer science


Am I AVL or Not?
12
5

3 7

1 6 9

0 2

department of computer science


Am I AVL or Not?
12
5

3 7

1 4 6 9

2 8

department of computer science


Basic Operations
 insert
 delete

 Auxiliary routines:
 manage height: precalculate it; fix it after changes
 rebalance
 rotate left/right

department of computer science


Additional Information
 Maintain height at each subtree
 Use an instance variable in the node

 Balance factor: the difference in heights between


the left and right subtrees at each node

department of computer science


height (precalculated)
public int height ( BinaryTreeNode<dataType> node )
{
if (node != null)
return node.height;
return -1;
}

department of computer science


balanceFactor and fixHeight
public int balanceFactor ( BinaryTreeNode<dataType> node )
{
return height (node.right) - height (node.left);
}

public void fixHeight ( BinaryTreeNode<dataType> node )


{
node.height = Math.max (height (node.left),
height (node.right)) + 1;
}

department of computer science


Insert Algorithm
 Use standard BST insertion algorithm
 Rebalance all nodes potentially affected:
 Apply to all nodes from insertion point to root
 Use tree rotations at each node as necessary

department of computer science


Tree Rotations
 Single rotations:
 insertion into left subtree of left child
(rotateWithLeftChild - rotate right)
 insertion into right subtree of right child
(rotateWithRightChild - rotate left)

 Double rotations:
 insertion into right subtree of left child
 insertion into left subtree of right child
department of computer science
Single rotation: rotateWithLeftChild

12
10

10
12
C
H-3
A
H-2

A B
B C
H-2 H-3
Initial height: H

department of computer science


Single rotation: rotateWithRightChild
10
12

12
10
A
H-3

C
H-2
B
C A
B
H-2 H-3
Initial height: H

department of computer science


Example: Single rotation
12
5

3 7

1 12
5
1 4

rotate with left child


0 4 7
0

department of computer science


rotateRight
public BinaryTreeNode<dataType> rotateRight
( BinaryTreeNode<dataType> p )
{
BinaryTreeNode<dataType> q = p.left;
p.left = q.right;
q.right = p;
fixHeight (p);
fixHeight (q);
return q;
}

department of computer science


rotateLeft
public BinaryTreeNode<dataType> rotateLeft
( BinaryTreeNode<dataType> q )
{
BinaryTreeNode<dataType> p = q.right;
q.right = p.left;
p.left = q;
fixHeight (q);
fixHeight (p);
return p;
}

department of computer science


Double rotation: doubleRotateWithLeftChild 1
12 12
Initial height: H

10 first rotation left 11


D D
H-3 H-3

A 11 10
C
H-3

B A B
C
H-3 H-3 H-3
department of computer science
Double rotation: doubleRotateWithLeftChild 2
12

11
11 second rotate right
D
H-3

10 12
10
C

A B D
C
A B H-3 H-3 H-3
H-3 H-3
department of computer science
Double rotation: doubleRotateWithRightChild
1
10 10
Initial height: H first rotation right

A 12 A 11
H-3 H-3

11 B 12
D
H-3 H-3

B D
C C H-3
H-3
department of computer science
Double rotation: doubleRotateWithRightChild
2
10

11
second rotate left
A 11
H-3

10 12
B 12
H-3

A B D
C
D H-3 H-3 H-3
C H-3

department of computer science


Example: Double rotation
12
5 12
5 4
rotate 2 and 4 rotate 4 and 5

2 5
2 7 4 7

1 4 2
1 3 7

3 1 3

department of computer science


balance
public BinaryTreeNode<dataType> balance ( BinaryTreeNode<dataType> p )
{
fixHeight (p);
if (balanceFactor (p) == 2)
{
if (balanceFactor (p.right) < 0)
p.right = rotateRight (p.right);
return rotateLeft (p);
}
if (balanceFactor (p) == -2)
{
if (balanceFactor (p.left) > 0)
p.left = rotateLeft (p.left);
return rotateRight (p);
}
return p;
}

department of computer science


insert
public void insert ( dataType d )
{
root = insert (d, root);
}
public BinaryTreeNode<dataType> insert ( dataType d,
BinaryTreeNode<dataType> node )
{
if (node == null)
return new BinaryTreeNode<dataType> (d, null, null);
if (d.compareTo (node.data) <= 0)
node.left = insert (d, node.left);
else
node.right = insert (d, node.right);
return balance (node);
}

department of computer science


Delete Algorithm
 Rebalance nodes all the way from node to root
 Also rebalance nodes also when removing the
minimum
 Use same balance function and rotations as
before

department of computer science


delete
public BinaryTreeNode<dataType> delete ( dataType d,
BinaryTreeNode<dataType> node )
{
if (node == null) return null;
int cmp = d.compareTo (node.data);
if (cmp < 0)
node.left = delete (d, node.left);
else if (cmp > 0)
node.right = delete (d, node.right);
else

department of computer science


delete (continued)
{
BinaryTreeNode<dataType> q = node.left;
BinaryTreeNode<dataType> r = node.right;
if (r == null)
return q;
BinaryTreeNode<dataType> min = findMin (r);
min.right = removeMin (r);
min.left = q;
return balance (min);
}
return balance (node);
}

department of computer science


removeMin
public BinaryTreeNode<dataType> removeMin (
BinaryTreeNode<dataType> node )
{
if (node.left == null)
return node.right;
node.left = removeMin (node.left);
return balance (node);
}

department of computer science

You might also like