next up previous
Next: About this document Up: My Home Page

For each of the four types of linked lists in the following table, what is the asymptotic worst-case running time for each dynamic-set operation listed?  

tabular4

lec8-3.au

Binary Search Trees

``I think that I shall never see
a poem as lovely as a tree Poem's
are wrote by fools like me but only
G-d can make a tree ``
- Joyce Kilmer

Binary search trees provide a data structure which efficiently supports all six dictionary operations.   

A binary tree is a rooted tree where each node contains at most two children.

Each child can be identified as either a left or right child.

f_132.0in

A binary tree can be implemented where each node has left and right pointer fields, an (optional) parent pointer, and a data field. lec8-4.au Binary Search Trees

A binary search tree labels each node in a binary tree with a single key such that for any node x, and nodes in the left subtree of x have keys tex2html_wrap_inline157 and all nodes in the right subtree of x have key's tex2html_wrap_inline161 .

f_14f_15 Left: A binary search tree. Right: A heap but not a binary search tree.

The search tree labeling enables us to find where any key is. Start at the root - if that is not the one we want, search either left or right depending upon whether what we want is tex2html_wrap_inline163 or tex2html_wrap_inline165 then the root.

lec8-5.au Searching in a Binary Tree

Dictionary search operations are easy in binary trees ...

TREE-SEARCH(x, k)
\> if (x = NIL) and (k = key[x])
\>\> then return x
\> if (k < key[x])
\>\> then return TREE-SEARCH(left[x],k)
\>\> else return TREE-SEARCH(right[x],k)

The algorithm works because both the left and right subtrees of a binary search tree are binary search trees - recursive structure, recursive algorithm.

This takes time proportional to the height of the tree, O(h). lec8-6.au Maximum and Minimum

Where are the maximum and minimum elements in a binary tree?  

f_16

TREE-MAXIMUM(X)
\> while tex2html_wrap_inline175
\> \> do x = right[x]
\> return x

TREE-MINIMUM(x)
\> while tex2html_wrap_inline177
\>\> do x = left[x]
\> return x

Both take time proportional to the height of the tree, O(h). lec8-7.au

Where is the predecessor?

Where is the predecessor of a node in a tree, assuming all keys are distinct?   

f_17

If X has two children, its predecessor is the maximum value in its left subtree and its successor the minimum value in its right subtree.

lec8-8.au What if a node doesn't have children?

f_181.5in

If it does not have a left child, a node's predecessor is its first left ancestor.

The proof of correctness comes from looking at the in-order traversal of the tree.

Tree-Successor(x)
\> if tex2html_wrap_inline185
\>\> then return Tree-Minimum(right[x])
\>\> tex2html_wrap_inline189
\> while tex2html_wrap_inline191 and (x = right[y])
\>\> do tex2html_wrap_inline195
\> \> tex2html_wrap_inline197
\> return y

Tree predecessor/successor both run in time proportional to the height of the tree.

lec8-9.au

In-Order Traversal

f_191.5in  

Inorder-Tree-walk(x)
\> if (x <> NIL)
\> then Inorder-Tree-Walk(left[x])
\>\> print key[x]
\>\> Inorder-Tree-walk(right[x])

A-B-C-D-E-F-G-H

lec8-10.au Tree Insertion

Do a binary search to find where it should be, then replace the termination NIL pointer with the new item.  

tree-insert0.75in

Tree-insert(T,z)
\> y = NIL
\> x = root[T]
\> while tex2html_wrap_inline217
\>\> do y = x
\>\>\> if key[z] < key[x]
\>\>\>\> then x = left[x]
\>\>\>\> else x = right[x]
\> tex2html_wrap_inline227
\> if y = NIL
\>\> then tex2html_wrap_inline229
\>\> else if key[z] < key[y]
\>\>\> then tex2html_wrap_inline233
\>\>\> else tex2html_wrap_inline235

y is maintained as the parent of x, since x eventually becomes NIL.

The final test establishes whether the NIL was a left or right turn from y.

Insertion takes time proportional to the height of the tree, O(h). lec8-11.au Tree Deletion

Deletion is somewhat more tricky than insertion, because the node to die may not be a leaf, and thus effect other nodes.  

Case (a), where the node is a leaf, is simple - just NIL out the parents child pointer.

Case (b), where a node has one chld, the doomed node can just be cut out.

Case (c), relabel the node as its successor (which has at most one child when z has two children!) and delete the successor!

This implementation of deletion assumes parent pointers to make the code nicer, but if you had to save space they could be dispensed with by keeping the pointers on the search path stored in a stack.

Tree-Delete(T,z)
\> if (left[z] = NIL) or (right[z] = NIL)
\>\> then tex2html_wrap_inline253
\>\> else tex2html_wrap_inline255 Tree-Successor(z)
\> if tex2html_wrap_inline257
\>\> then tex2html_wrap_inline259
\>\> else tex2html_wrap_inline261
\> if tex2html_wrap_inline263
\>\> then tex2html_wrap_inline265
\> if p[y] = NIL
\>\> then tex2html_wrap_inline269
\>\> else if (y = left[p[y]])
\>\>\> then tex2html_wrap_inline273
\>\>\> else tex2html_wrap_inline275
\> if (y <> z)
\>\> then tex2html_wrap_inline279
\>\>\> /* If y has other fields, copy them, too. */
\> return y
lec8-12.au Lines 1-3 determine which node y is physically removed.

Lines 4-6 identify x as the non-nil decendant, if any.

Lines 7-8 give x a new parent.

Lines 9-10 modify the root node, if necessary

Lines 11-13 reattach the subtree, if necessary.

Lines 14-16 if the removed node is deleted, copy.

Conclusion: deletion takes time proportional to the height of the tree. lec8-13.au

Balanced Search Trees

All six of our dictionary operations, when implemented with binary search trees, take O(h), where h is the height of the tree.  

The best height we could hope to get is tex2html_wrap_inline295 , if the tree was perfectly balanced, since

tex2html_wrap_inline297

But if we get unlucky with our order of insertion or deletion, we could get linear height!

insert(a)
insert(b)
insert(c)
insert(d)

f_212.0in

In fact, random search trees on average have tex2html_wrap_inline309 height, but we are worried about worst case height.

We can't easily use randomization - Why? lec8-14.au

Perfectly Balanced Trees

Perfectly balanced trees require a lot of work to maintain:

f_26

If we insert the key 1, we must move every single node in the tree to rebalance it, taking tex2html_wrap_inline311 time.

Therefore, when we talk about "balanced" trees, we mean trees whose height is tex2html_wrap_inline313 , so all dictionary operations (insert, delete, search, min/max, successor/predecessor) take tex2html_wrap_inline315 time.

Red-Black trees are binary search trees where each node is assigned a color, where the coloring scheme helps us maintain the height as tex2html_wrap_inline317 . lec8-15.au

Red-Black Tree Definition

Red-black trees have the following properties:  

  1. Every node is colored either red or black.
  2. Every leaf (NIL pointer) is black.
  3. If a node is red then both its children are black.
  4. Every single path from a node to a decendant leaf contains the same number of black nodes.
lec8-16.au

What does this mean?

If the root of a red-black tree is black can we just color it red?

No! For one of its children might be red.

If an arbitrary node is red can we color it black?

No! Because now all nodes may not have the same black height.

f_231.0in

What tree maximizes the number of nodes in a tree of black height h?

f_241.5in lec8-17.au

What does a red-black tree with two real nodes look like?

f_25

Not (1) - consecutive reds Not (2), (4) - Non-Uniform black height lec8-18.au Red-Black Tree Height

Lemma: A red-black tree with n internal nodes has height at most tex2html_wrap_inline323 .

Proof: Our strategy; first we bound the number of nodes in any subtree, then we bound the height of any subtree.

We claim that any subtree rooted at x has at least tex2html_wrap_inline327 - 1 internal nodes, where bh(x) is the black height of node x.

Proof, by induction:

Now assume it is true for all tree with black height < bh(x).

If x is black, both subtrees have black height bh(x)-1. If x is red, the subtrees have black height bh(x).

Therefore, the number of internal nodes in any subtree is

lec8-19.au

Now, let h be the height of our red-black tree. At least half the nodes on any single path from root to leaf must be black if we ignore the root.

Thus tex2html_wrap_inline345 and tex2html_wrap_inline347 , so tex2html_wrap_inline349 .

This implies that tex2html_wrap_inline351 ,so tex2html_wrap_inline353 .

Therefore red-black trees have height at most twice optimal. We have a balanced search tree if we can maintain the red-black tree structure under insertion and deletion.




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Tue Sep 15 15:53:51 EDT 1998