Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 Old CS 160 Balanced Binary Search Trees
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Balanced Binary Search Trees

by admin last modified 2005-05-25 15:40

Balanced Binary Search Trees


As we have seen, binary search trees have good average case performance for search and insert operations, but poor worst case performance.  To avoid the worst case performance, it is necessary to keep the tree in balance.  There are two well-known techniques for doing this:  AVL trees and Red-Black trees.

AVL trees

AVL trees are also known as height-balanced binary search trees.  The idea is this:  A tree cannot be too much out of balance if its left and right subtrees are of nearly the same height.

definition  A binary tree T is a height-balanced(k) tree or HB[k] tree, if each node in the tree has the HB[k] property

A tree has the HB[k] property if
  • it is an empty tree, or
  • its left and right subtrees differ in height by at most k, and
  • every subtree is HB[k]
definition  A HB[1] tree is called an AVL tree (named for Adel'son-Vel'skii and Landis).

examples

theorem  The height of an AVL tree with N nodes is at most 1.44 log2(N+2).

AVL tree insertion

Define the balance factor of a node to be the height of its left subtree - the height of its right subtree.  The balance factor is stored in each node of an AVL tree.

The insertion algorithm proceeds like a normal binary search tree insertion.  As the algorithm proceeds from root to leaf, it keeps track of the last node visited in which the balance factor is +1 or -1.  Call that the pivot node.  (If all the balance factors along the path are zero, let the root node be the pivot.)  There are two cases:

If the new node is added to the left of the pivot, and the balance factor of the pivot is 0 or -1; or if the new node is added to the right of the pivot, and the balance factor of the pivot is 0 or +1, then no rebalancing is necessary.  Retrace the path of the insertion, adding 1 to the balance factor of nodes from which the left path was taken, and subtracting 1 from the balance factor of nodes from which the right path was taken.

Otherwise, the new node has caused the tree to become unbalanced at the pivot.  Rebalancing is necessary.  The rebalancing is done by a rotation operation.  There are four cases:

LL:  The new node is added to the left subtree of the left subtree of the pivot.  Perform a single rotation:


RR:  The new node is added to the right subtree of the right subtree of the pivot.  Perform a single rotation.


LR:  The new node is added to the right subtree of the left subtree of the pivot.  Perform a double rotation.


RL:  The new node is added to the left subtree of the right subtree of the pivot.  Perform a double rotation.


In each case, the balance factors of the nodes involved in the rotation can be computed easily.  It can then be proved that the rotated tree is an AVL tree, having both the binary search tree ordering property and the AVL height-balance property.

The running time of the balance factor adjustments and rotation operations are O(log n), so the overall insertion time is also O(log n).  Thus, the insert and search operations for an AVL tree are both O(log n) in the worst case.


Red-Black trees


Red-Black trees are a type of binary search tree derived from a nonbinary search tree called a 2-3-4 tree, which is a variation on a 2-3 tree, which is a special case of a B-tree.

A Red-Black tree is a binary search tree in which each node is assigned a color of either red or black, and the following properties hold:
  • The root is black.
  • The parent of a red node must be black.
  • Every path from the root to a leaf contains the same number of black nodes.
In addition, the failure nodes at the end of each such path are painted black.

theorem  The height of a red-black tree with n nodes is at most 2 log2(n+1).

Red-Black tree insertion

The insertion algorithm proceeds like a normal binary search tree insertion.  When the algorithm arrives at a failure node, it inserts the data item, paints the node red, and attaches two black failure nodes.  If the parent of the new node is also red, the tree no longer satisfies the red-black tree property and must be adjusted by restructuring or recoloring.  The type of adjustment depends on the direction of the path taken from the new node's grandparent to itself, and on the color of the new node's uncle; that is, the sibling of the node's parent.

Let n be the new node, p its parent, and g its grandparent.

1.  The uncle is red.  In this case, repaint the grandparent red, the parent and uncle black.  Then let n = g and continue.  (It is possible that the grandparent's parent is also red, requiring additional adjustments along a path from n to the root.)

2.  The uncle is black.  In this case, apply one of the four AVL-type rotations (LL, LR, RL, or RR), considering g as the pivot.

LLb:

RRb:

LRb:

RLb:


Q:  In each case, is the resulting tree a red-black tree?



 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: