Teach Time Encyclopedia - Learn About Our World
Home Page
Teach Time
Featured Topics

United States
by state

CITYology

Academic Disciplines

Historical Timelines

Themed Timelines

Calendars

Reference Tables

Biographies

How-tos



Wednesday, December 03, 2008

Tree rotation

A tree rotation is an operation on a binary search tree that changes the structure without interfering with the order of the elements. A tree rotation moves one node up in the tree and one node down. Tree rotations are used to balance some kinds of trees to improve performance.

Examples

The tree
      D
     / \\
    /   \\
   B     E
  / \\
 A   C
can be rotated to look like
    B
   / \\
  /   \\
 A     D
      / \\ 
     C   E 

This is called a right rotation. Going from the second tree to the first would be a left rotation.

If we write our tree nodes as (left subtree, nodevalue, right subtree), then the first structure is ((A, B, C), D, E), and the second is (A, B, (C, D, E)), and computing one from the other is very simple:

def right_rotation(treenode):
   left, D, E = treenode
   A, B, C = left
   return (A, B, (C, D, E))

There are also "double left" and "double right" rotations, which can be written as compositions of left and right rotations.

Tree rotations are used in AVL trees, red-black trees, splay trees, and others.

Reference: this page has some excellent (Free) java applets demonstrating tree rotations. (can we add applets to the Wiki?)



Internet Hotel Solutions

Site Sponsors
AC Units
Baltimore Harbor
Boot Camp Grads
Bra Size
Burkittsville
College Hotels
Digital Harbor
Free Cell Phones
Golden Hare Travel
Golf Vacations
Golf Courses
Gourmet
Hair Styles
Hippodrome
iWoman
Lesson Plans
Maryland Hotels
MD Genealogy
Minor League Stuff
Motel Site
Ocean City
OC Real Estate
Old Agers
Office Supplies
Orlando
Pet Friendly Hotel
Room Prices
Savannah, GA
Ski Vacations
South Baltimore
Student Teaching
Travel Sources
University Hotels
Visit Military Bases
Washington, DC

Brought to you by NoChildLeftBehind.com and the Beaches and Towns Network, LLC.