next next up down toc toc mail

7.12.  Looking Up an Element in a BST

Suppose we want to look for an element x that may be in a dictionary represented by a binary search tree T. We can express the lookup operation by the following recursive algorithm.

Basis: If the tree is empty, then x is not present. If the tree is not empty and x appears at the root, then x is present.

Induction: If x is not empty and x appears not at the root, let y be the element at the root of T. If x < y, look in the left subtree of the root. If x > y, look in the right subtree of the root.

In the example x > y means ape > animal The algorithm as a Nassi Shneidermann diagram:

Procedure Look_up(node n_r, label x):

[picture]

Examples:

Tree 1:

[picture]


back next up down toc toc mail


Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1998).

Last modified: 27/July/98 (12:14)