21
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada

CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada

Embed Size (px)

Citation preview

CPSC 335

Height Balanced Trees

Dr. Marina Gavrilova

Computer Science

University of Calgary

Canada

2

Outline

General Height Balanced Trees AVL Trees IPR trees Summary

A height balanced tree is one in which the difference in the height of the two subtrees for any node is less than or equal to some specified amount.

One type of height balanced tree is the AVL tree named after its originators (Adelson-Velskii & Landis).

In this tree the height difference may be no more than 1.

Height Balanced Trees

In the AVL trees, searches always stay close to the theoretical minimum of O(log n) and never degenerate to O(n).

The tradeoff for search efficiency is increased time and complexity for insertion and deletion from these trees since each time the tree is changed it may go out of balance and have to be rebalanced.

AVL Trees

To implement an AVL tree each node must contain a balance factor which indicates its state of balance relative to its subtrees.

If balance is determined by

(height left tree) - (height right tree)

then the balance factors in a balanced tree can only have values of -1, 0, or 1.

Balance Factor

AVL tree with 1 to 4 nodes

A larger AVL tree

6

AVL tree examples

Adding a new node can change the balance factor of any node by at most 1.

If a node currently has a balance factor of 0 it cannot go out of balance.

If a node is left high (left subtree 1 level higher than right) and the insertion is in its left subtree it may go out of balance ( same applies to right/right).

If a node is left high and the insertion takes place in its right subtree then a) it cannot go out of balance b) it cannot affect the balance of nodes above it since the balance factor of higher nodes is determined by the maximum height of this node which will not change.

Inserting a New Node

If the balance factor of each node passed through on the path to an insertion is examined then it should be possible to predict which nodes could go out of balance and determine which of these nodes is closest to the insertion point. This node will be called the

pivot node.

Restoring the balance of the pivot node restores the balance of the whole sub-tree and potentially all of the nodes that were affected by the insertion.

Pivot Node

The mechanism of restoring balance to an AVL tree is called rotation.

Restoring Balance – Tree Rotation

When an imbalance occurs, there are two possible situations:

1. The root of the high subtree is out of balance in the same direction as its parent. This is called a Left-Left or Right-Right imbalance since the insertion took place in the left/(right) subtree of a node whose parent (the pivot) was left/(right) high.

2. The root of the high subtree is out of balance in the opposite direction to its parent. This is called a Left-Right or Right-Left imbalance since the insertion took place in the right /(left) subtree of a node whose parent (the pivot) was left/(right) high.

Restoring Balance – Tree Rotation

Correction of the LL or RR imbalance requires only a single rotation about the pivot node but the second case, LR or RL requires a prior rotation about the root of the affected subtree then a rotation about the pivot node.

The first of the double rotations does not affect the degree of imbalance but converts it to a simple LL or RR.

A rotation consists of moving the child of the high subtree into the position occupied by the pivot, and moving the pivot down into the low subtree.

Tree Rotation

To correct an LL imbalance the high child replaces the pivot and the pivot moves down to become the root of this child’s right subtree.

The original right child of this node becomes the left child of the pivot (for RR exchange left and right in the description).

LL (RR) Imbalance

To correct an LR imbalance a rotation is first performed about the left child of the pivot as if it were the pivot for a RR imbalance.

This rotation effectively converts the imbalance of the original pivot to a LL which can now be corrected as above.

LL (RL) Imbalance

The golden ratio is an irrational number φ =(1+√5)/2= 2cos(Pi/5) ∼= 1.618Properties: φ turns up in many geometric figures

including pentagrams and dodecahedra φ − 1 = 1/φ It is the ratio, in the limit, of successive

members of the Fibonacci sequence

14

AVL Trees and Golden ratio

15

This figure is a regular pentagon with an inscribed

pentagram.

All the line segments found there are equal in length to

one of the five line segments described below:

The length of the black line segment is 1 unit.

The length of the red line seqment, a, is Ø.

The length of the yellow line seqment, b, is 1/Ø.

The length of the green line seqment, c, is 1, like the black

segment.

The length of the blue line seqment, d, is (1/Ø)2

,

or equivalently, 1 - (1/Ø), as can be seen from examining

the figure.

(Note that b + d = 1).

AVL Trees and Golden ratio

Image Pattern Recognition, M. Gavrilova World Scietific

cover (left)

Leonardo Da Vinci's illustration from De Divina Proportione

(right)

Golden ratio is used to prove that in AVL tree the height will never be greater than

1.44log(n+2).

Time complexity of restoring balance O(log2 n)

{in spite of the overhead of calculating balance factors,

determining imbalance and correcting it}

Height balancing for a single node is still O(1).

Efficiency of AVL Trees

IPR Trees

Internal path Reduction tree IPR (Gaston Gonnet, 1983) is another type of balanced tree.

It uses as balancing factor not a difference between the left and right subtrees, but the sum of path lengths from the root to the leaves in the subtrees.

Root length is 1, Internal path is sum of depths of all nodes in the subtree

IPR Tree rotation

Four rotation cases are similar to AVL trees:

MR: more nodes to the right and can be restored through single or double rotation (Case a and b on your handout)

ML: more nodes to the left and can be restored through single or double rotation (Case c and d on your handout)

19

IPR Tree rotation

Rotations to restore balance can be multiple, thus this operation is longer than in AVL tree

Search for a key is faster on average as the tree minimized the combined length to reach the node

20

IPR vs AVL comparison

Summary

Balanced trees demonstrate superior performance to binary search trees due to log (n) tree depth Rotations are used as a typical mechanism to restore balance Balanced trees are used in lookup-intensive applications