You are on page 1of 12

Balancing a Tree

We said previously that Td


could be as large as n 1 due to a lousy insertion order.

How can we x an imbalanced tree? Can we use a tree traversal?

ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Balancing a Tree
We said previously that Td
could be as large as n 1 due to a lousy insertion order.

How can we x an imbalanced tree? Can we use a tree traversal?


Perform an in order traversal and save the
elements in sorted order Reinsert to guarantee a balanced tree.
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Balancing a Tree
public void balance() { int[] A = new int[size()]; toArray(root, A, 0); root = null; size=0; // reinsert elements in "binary search" order int start = 0; int end = A.length-1; int mid = (start+end)/2; root = new BSTNode(A[mid], null, null); size += 1; add(root, A, start, mid-1); add(root, A, mid+1, end); }
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Balancing a Tree
public void add(BSTNode r, int[] A, int start, int end) { if (start <= end) { int mid = (start+end)/2; if (A[mid] < r.key) { r.left = new BSTNode(A[mid], null, null); size += 1; add(r.left, A, start, mid-1); add(r.left, A, mid+1, end); } else { r.right = new BSTNode(A[mid], null, null); size += 1; add(r.right, A, start, mid-1); add(r.right, A, mid+1, end); } } }
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Balancing a Tree
public void add(BSTNode r, int[] A, int start, int end) { if (start <= end) { int mid = (start+end)/2; if (A[mid] < r.key) { r.left = new BSTNode(A[mid], null, null); size += 1; add(r.left, A, start, mid-1); add(r.left, A, mid+1, end); } else { r.right = new BSTNode(A[mid], null, null); size += 1; add(r.right, A, start, mid-1); add(r.right, A, mid+1, end); } } Handling duplicates is a little trickier, we cant }

just insert the middle element!


Data Structures in Java, Prof. Mettu

ECE242 Fall 2007

Running Time of balance()


The in-order traversal to construct the array requires O(n) time. We insert each element exactly once, just in a particular order. This also requires O(n) time altogether. The rst time we call balance(), we pay just a constant number of operations per node.
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Running Time of balance()


What is the cost of maintaining a balanced tree? Even when a subtree is balanced, we are paying for it (in terms of time). Each call to balance() requires O(n) time. How do we account for the cost of maintaining the tree over time?
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Running Time of balance()


Suppose we add n nodes to our tree, where n is a multiple of k , and we run balance() every k insertions. Then, the cost of balancing is:
k + 2k + 3k + . . . + n
n/k

= k

i
i=1

n/k (n/k + 1) = k 2 2 n n = + 2k 2
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Balancing Trees
If k is a constant, then the cost per insertion after all insertions have been made is O(n). In a tree that is balanced at all times, after n insertions, the cost per insertion is only O(log2 n) ... Can we always keep the tree balanced?
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Tree Rotations
Luckily, we can modify the structure of a tree we have already built:
x y T3 T1 T2
right rotation

y x T1 T2 T3

Right rotations reduce the depth of the left subtree by one.


ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Tree Rotations
Luckily, we can modify the structure of a tree we have already built:
x y T1 T2 T3 T1 T2
left rotation

y x T3

Left rotations reduce the depth of the right subtree by one.


ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Balanced Binary Search Trees


First balanced trees:
G. M. Adelson-Velskii and E. M. Landis. An algorithm for the organization of information. Soviet Math. Doklady 3, pages 1259--1263, 1962. Td 1.44 log2 (n + 1) Bayer 1972; Guibas and Sedgewick 1978. Td 2 log2 n Sleator and Tarjan 1985. Use rotations to cache items in the tree.
ECE242 Fall 2007 Data Structures in Java, Prof. Mettu

Red-black trees: Splay Trees:

You might also like