# 6.2: BinarySearchTree - An Unbalanced Binary Search Tree

- Page ID
- 8462

A `BinarySearchTree` is a special kind of binary tree in which each node, \(\mathtt{u}\), also stores a data value, \(\texttt{u.x}\), from some total order. The data values in a binary search tree obey the binary search tree property: For a node, \(\mathtt{u}\), every data value stored in the subtree rooted at \(\texttt{u.left}\) is less than \(\texttt{u.x}\) and every data value stored in the subtree rooted at \(\texttt{u.right}\) is greater than \(\texttt{u.x}\). An example of a `BinarySearchTree` is shown in Figure \(\PageIndex{1}\).

## \(\PageIndex{1}\) Searching

The binary search tree property is extremely useful because it allows us to quickly locate a value, \(\mathtt{x}\), in a binary search tree. To do this we start searching for \(\mathtt{x}\) at the root, \(\mathtt{r}\). When examining a node, \(\mathtt{u}\), there are three cases:

- If \(\mathtt{x}< \texttt{u.x}\), then the search proceeds to \(\texttt{u.left}\);
- If \(\mathtt{x}> \texttt{u.x}\), then the search proceeds to \(\texttt{u.right}\);
- If \(\mathtt{x}= \texttt{u.x}\), then we have found the node \(\mathtt{u}\) containing \(\mathtt{x}\).

The search terminates when Case 3 occurs or when \(\mathtt{u=nil}\). In the former case, we found \(\mathtt{x}\). In the latter case, we conclude that \(\mathtt{x}\) is not in the binary search tree.

T findEQ(T x) { Node u = r; while (u != nil) { int comp = compare(x, u.x); if (comp < 0) u = u.left; else if (comp > 0) u = u.right; else return u.x; } return null; }

Two examples of searches in a binary search tree are shown in Figure \(\PageIndex{2}\). As the second example shows, even if we don't find \(\mathtt{x}\) in the tree, we still gain some valuable information. If we look at the last node, \(\mathtt{u}\), at which Case 1 occurred, we see that \(\texttt{u.x}\) is the smallest value in the tree that is greater than \(\mathtt{x}\). Similarly, the last node at which Case 2 occurred contains the largest value in the tree that is less than \(\mathtt{x}\). Therefore, by keeping track of the last node, \(\mathtt{z}\), at which Case 1 occurs, a `BinarySearchTree` can implement the \(\mathtt{find(x)}\) operation that returns the smallest value stored in the tree that is greater than or equal to \(\mathtt{x}\):

T find(T x) { Node w = r, z = nil; while (w != nil) { int comp = compare(x, w.x); if (comp < 0) { z = w; w = w.left; } else if (comp > 0) { w = w.right; } else { return w.x; } } return z == nil ? null : z.x; }

## \(\PageIndex{2}\) Addition

To add a new value, \(\mathtt{x}\), to a `BinarySearchTree`, we first search for \(\mathtt{x}\). If we find it, then there is no need to insert it. Otherwise, we store \(\mathtt{x}\) at a leaf child of the last node, \(\mathtt{p}\), encountered during the search for \(\mathtt{x}\). Whether the new node is the left or right child of \(\mathtt{p}\) depends on the result of comparing \(\mathtt{x}\) and \(\texttt{p.x}\).

boolean add(T x) { Node p = findLast(x); return addChild(p, newNode(x)); }

Node findLast(T x) { Node w = r, prev = nil; while (w != nil) { prev = w; int comp = compare(x, w.x); if (comp < 0) { w = w.left; } else if (comp > 0) { w = w.right; } else { return w; } } return prev; }

boolean addChild(Node p, Node u) { if (p == nil) { r = u; // inserting into empty tree } else { int comp = compare(u.x, p.x); if (comp < 0) { p.left = u; } else if (comp > 0) { p.right = u; } else { return false; // u.x is already in the tree } u.parent = p; } n++; return true; }

An example is shown in Figure \(\PageIndex{3}\). The most time-consuming part of this process is the initial search for \(\mathtt{x}\), which takes an amount of time proportional to the height of the newly added node \(\mathtt{u}\). In the worst case, this is equal to the height of the `BinarySearchTree`.

## \(\PageIndex{3}\) Removal

Deleting a value stored in a node, \(\mathtt{u}\), of a `BinarySearchTree` is a little more difficult. If \(\mathtt{u}\) is a leaf, then we can just detach \(\mathtt{u}\) from its parent. Even better: If \(\mathtt{u}\) has only one child, then we can splice \(\mathtt{u}\) from the tree by having \(\texttt{u.parent}\) adopt \(\mathtt{u}\)'s child (see Figure \(\PageIndex{4}\)):

void splice(Node u) { Node s, p; if (u.left != nil) { s = u.left; } else { s = u.right; } if (u == r) { r = s; p = nil; } else { p = u.parent; if (p.left == u) { p.left = s; } else { p.right = s; } } if (s != nil) { s.parent = p; } n--; }

Things get tricky, though, when \(\mathtt{u}\) has two children. In this case, the simplest thing to do is to find a node, \(\mathtt{w}\), that has less than two children and such that \(\texttt{w.x}\) can replace \(\texttt{u.x}\). To maintain the binary search tree property, the value \(\texttt{w.x}\) should be close to the value of \(\texttt{u.x}\). For example, choosing \(\mathtt{w}\) such that \(\texttt{w.x}\) is the smallest value greater than \(\texttt{u.x}\) will work. Finding the node \(\mathtt{w}\) is easy; it is the smallest value in the subtree rooted at \(\texttt{u.right}\). This node can be easily removed because it has no left child (see Figure \(\PageIndex{5}\)).

void remove(Node u) { if (u.left == nil || u.right == nil) { splice(u); } else { Node w = u.right; while (w.left != nil) w = w.left; u.x = w.x; splice(w); } }

## \(\PageIndex{4}\) Summary

The \(\mathtt{find(x)}\), \(\mathtt{add(x)}\), and \(\mathtt{remove(x)}\) operations in a `BinarySearchTree` each involve following a path from the root of the tree to some node in the tree. Without knowing more about the shape of the tree it is difficult to say much about the length of this path, except that it is less than \(\mathtt{n}\), the number of nodes in the tree. The following (unimpressive) theorem summarizes the performance of the `BinarySearchTree` data structure:

`BinarySearchTree` implements the `SSet` interface and supports the operations \(\mathtt{add(x)}\), \(\mathtt{remove(x)}\), and \(\mathtt{find(x)}\) in \(O(\mathtt{n})\) time per operation.

Theorem \(\PageIndex{1}\) compares poorly with Theorem 4.2.1, which shows that the `SkiplistSSet` structure can implement the `SSet` interface with \(O(\log \mathtt{n})\) expected time per operation. The problem with the `BinarySearchTree` structure is that it can become unbalanced. Instead of looking like the tree in Figure \(\PageIndex{1}\) it can look like a long chain of \(\mathtt{n}\) nodes, all but the last having exactly one child.

There are a number of ways of avoiding unbalanced binary search trees, all of which lead to data structures that have \(O(\log\mathtt{n})\) time operations. In Chapter 7 we show how \(O(\log \mathtt{n})\) expected time operations can be achieved with randomization. In Chapter 8 we show how \(O(\log \mathtt{n})\) amortized time operations can be achieved with partial rebuilding operations. In Chapter 9 we show how \(O(\log \mathtt{n})\) worst-case time operations can be achieved by simulating a tree that is not binary: one in which nodes can have up to four children.