18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Binary Search Trees<br />

Next: Priority Queues Up: Fundamental Data Types Previous: Dictionaries<br />

Binary Search Trees<br />

Fast support of all dictionary operations is realized by binary search trees. A binary tree is a rooted tree<br />

where each node contains at most two children. Each child can be identified as either a left or right child.<br />

As shown in Figure , a binary tree can be implemented where each node has left and right pointer<br />

fields, an (optional) parent pointer, and a data field.<br />

Figure: Relationships in a binary search tree<br />

A binary search tree labels each node in a binary tree with a single key such that for any node labeled x,<br />

all nodes in the left subtree of x have keys < x while all nodes in the right subtree of x have keys > x. <strong>The</strong><br />

search tree labeling enables us to find where any key is. Start at the root. If it does not contain the key we<br />

are searching for, proceed either left or right depending upon whether what we want occurs before or after<br />

the root key. This algorithm works because both the left and right subtrees of a binary search tree are<br />

binary search trees; the recursive structure yields a recursive algorithm. Accordingly, the dictionary Query<br />

operation can be performed in O(h) time, where h is the height of the tree.<br />

file:///E|/BOOK/BOOK/NODE26.HTM (1 of 3) [19/1/2003 1:28:24]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!