Binary Search Trees - 2019
Binary Search Trees - 2019
1 Introduction
We consider a particular kind of a binary tree called a Binary Search Tree (BST). The basic idea
behind this data structure is to have such a storing repository that provides the efficient way of
data sorting, searching and retrieving.
2 Implementation
2.1 Binary search tree node
We implement a binary search tree using a private inner class BSTNode. In order to support the
binary search tree property, we require that data stored in each node is Comparable:
1
2.2 Binary search tree
public BST()
{
root = null;
comparator = null;
}
3 Insertion
The insertion procedure is quite similar to searching. We start at the root and recursively go down
the tree searching for a location in a BST to insert a new node. If the element to be inserted is
already in the tree, we are done (we do not insert duplicates). The new node will always replace a
NULL reference.
2
Exercise. Given a sequence of numbers:
Draw a binary search tree by inserting the above numbers from left to right.
In binary search tree, new node is always inserted as a leaf node. The insertion operation is
performed as follows:
Step 1: Create a newNode with given value and set its left and right to NULL.
Step 2: Check whether tree is Empty.
Step 3: If the tree is Empty, then set root to newNode.
Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or
larger than the node (here it is root node).
Step 5: If newNode is smaller than to the node, then move to its left child. If newNode is
larger than the node, then move to its right child.
Step 6: Repeat the above step until we reach to a leaf node (i.e., reach to NULL).
Step 7: After reaching a leaf node, then insert the newNode as left child if newNode is
smaller to that leaf else insert it as right child.
if (compare(toInsert, p.data) == 0)
return p;
3
if (compare(toInsert, p.data) < 0)
p.left = insert(p.left, toInsert);
else
p.right = insert(p.right, toInsert);
return p;
}
4 Searching
Searching in a BST always starts at the root. We compare a data stored at the root with the key we
are searching for (let us call it as toSearch). If the node does not contain the key we proceed either
to the left or right child depending upon comparison. If the result of comparison is negative we go
to the left child, otherwise - to the right child. The recursive structure of a BST yields a recursive
algorithm.
Searching in a BST has O(h) worst-case runtime complexity, where h is the height of the tree.
Since s binary search tree with n nodes has a minimum of O(log n) levels, it takes at least O(log
n) comparisons to find a particular node. Unfortunately, a binary search tree can degenerate to a
linked list, reducing the search time to O(n).
4
return true;
else
if (compare(toSearch, p.data) < 0)
return search(p.left, toSearch);
else
return search(p.right, toSearch);
}
5 Deletion
Deletion is somewhat trickier than insertion. There are several cases to consider. A node to be
deleted (let us call it as toDelete)
is not in a tree;
is a leaf;
has only one child;
has two children.
If toDelete is not in the tree, there is nothing to delete. If toDelete node has only one child the
procedure of deletion is identical to deleting a node from a linked list - we just bypass that node
being deleted
Deletion of an internal node with two children is less straightforward. If we delete such a node,
we split a tree into two sub trees and therefore, some children of the internal node won't be
accessible after deletion. In the picture below we delete 8:
5
Deletion strategy is the following: replace the node being deleted with the largest node in the left
subtree and then delete that largest node. By symmetry, the node being deleted can be swapped
with the smallest node is the right subtree.
Deleting a node from Binary search tree has following three cases.
We use the following steps to delete a node with one child from BST...
We use the following steps to delete a node with two children from BST...
return p.data;
}
7
Draw a binary search tree by inserting the above numbers from left to right and then show the
two trees that can be the result after the removal of 11.
Traversing a tree means visiting each node in a specified order. This process is not as commonly
used as finding, inserting, and deleting nodes.
An inorder traversal of a binary search tree will cause all the nodes to be visited in ascending
order, based on their key values. A recursive method to traverse the entire tree is called with a
node as an argument. Initially, this node is the root. The method needs to do only three things:
Remember that visiting a node means doing something to it: displaying it, writing it to a file, or
whatever.
8
For these other traversals the same three steps are used as for inOrder, but in a different
sequence. Here’s the sequence for a preOrder() method:
The third kind of traversal, postOrder, contains the three steps arranged in yet another way: