BINARY SEARCH TREE
INTRODUCTION
A binary search tree is a special subset of the standard binary tree. The
difference lies in the fact that a binary search tree requires that all nodes be
sorted into a particular order.
Typically the nodes in a binary search tree are sorted such that for any given
sub-tree the left child is smaller than or equal to the root node, and the right
child is larger than or equal to the root node.
A binary search tree is a hierarchical data structure in which each node
stores a key, and pointers to left and right child.
Each node has one parent, except the root of the tree which has no parents.
The basic operations on a binary search tree take time proportional to the
height of the tree.
BINARY SEARCH TREE
Binary Search tree is a binary tree in which each internal node x stores an
element such that the element stored in the left subtree of x are less than or
equal to x and elements stored in the right subtree of x are greater than or
equal to x. This is called binary-search-tree property.
The keys stored in the binary search tree must satisfy the binary search tree
property. Let x be a node in a binary search tree. Then,
If y in left subtree of x, then key[y] key[x].
If y in right subtree of x, then key[y] key[x].
1
For example
Binary Search tree can be implemented as a linked data structure, in which each
node is an object with three pointer fields as,
left pointer to left child: root of left subtree.
right pointer to right child: root of right subtree.
p pointer to parent. p [root[T]] = NIL (optional).
Any pointer field with NILL signifies that there exists no corresponding child or
parent. The root node is the only node in the BTS structure with NIL in its p field.
2
BST WALKS
There are three types of tree walks as follows
Inorder tree walk
Preorder tree walk
Postorder tree walk
Inorder tree walk:
The binary-search-tree property allows the keys of a binary search tree to be
printed, in sorted (monotonically increasing) order by inorder tree walk. The keys
are printed in sorted order because the key of the root of a subtree is printed
between the values in its left subtree and those in its right subtree.
The above INORDER-TREE-WALK works as follows:
Check to make sure that x is not NIL.
Recursively, print the keys of the nodes in x.s left subtree.
Print x.s key.
Recursively, print the keys of the nodes in x.s right subtree.
3
Example: Printing the keys of a Binary Search Tree in ascending order
The inorder tree walk prints the keys in each of the two binary search trees from
above figure as 7, 11, 21, 22, 41, and 51.
It takes (n) time to walk a tree of n nodes binary search tree.
Preorder tree walk:
In the preorder tree walk we visit the root node before the nodes in either subtree.
The PREORDER-TREE-WALK works as follows:
Check to make sure that x is not NIL.
Print x.s key.
Recursively, print the keys of the nodes in x.s left subtree.
Recursively, print the keys of the nodes in x.s right subtree
It takes (n) time to walk a binary search tree of n nodes.
4
Postorder Tree Walk:
In the postorder tree walk we visit the root node after the nodes in its subtrees.
The POSTORDER-TREE-WALK works as follows:
Check to make sure that x is not NIL.
Recursively, print the keys of the nodes in x.s left subtree.
Recursively, print the keys of the nodes in x.s right subtree.
Print x.s key.
It also takes (n) time to walk a binary search tree of n nodes.
QUERYING A BINARY SEARCH TREE
Searching for a key stored in the tree is a common operation of a binary search tree.
The binary search trees can also support some queries as MINIMUM, MAXIMUM,
SUCCESSOR, and PREDECESSOR.
Searching for a key
We use the following procedure to search for a node with a given key in a binary
search tree. The TREE-SEARCH procedure returns a pointer to a node with key k
if exists; otherwise returns NIL.
5
Runtime of TREE-SEARCH (x, k) is O (h) where h is the height of the tree
The non-recursive (iterative) version of tree-search is usually more efficient in
practice since it avoids the runtime system overhead for recursion. The iterative
version of above algorithm is given below.
6
Minimum / Maximum
The TREE-MINIMUN (x) algorithm returns a point to the node of the tree at x
whose key value is the minimum of all keys in the tree. Due to BST property, an
minimum element can always be found by following left child pointers from the root
until NIL is encountered.
An element in a binary search tree whose key is a maximum can always be found by
following right child pointers from root until a NIL is encountered.
Both of these procedures run in O (h) time on a tree of height h.
Example: Search for the minimum in a given binary search tree
Successor and predecessor
Successor of node x is the node y such that key[y] is the smallest key greater
than key[x].
The successor of the largest key is NIL.
Search consists of two cases.
7
o Case I: - If node x has a non-empty right subtree, then xs successor is
the minimum in the right subtree of x.
o Case II:- If node x has an empty right subtree, then:
As long as we move to the left up the tree (move up through
right children), we are visiting smaller keys.
In other words, xs successor y, is the lowest ancestor of x
whose left child is also an ancestor of x or is x itself.
The predecessor is the node that has the largest key smaller than that of x.
Example:
1. If x has a right subtree, succ(x) is the leftmost element in that subtree.
2. If x has no right subtree, succ( x ) is the lowest ancestor of x (above x on the
path to the root) that has x in its left subtree.
8
The running time of TREE-SUCCESSOR on a tree of height h is O (h) and the
procedure TREE-PREDECESSOR, which is symmetric to TREE-SUCCESSOR,
also runs in time O (h).
BST INSERTION AND DELETION
The operations of insertion and deletion change the dynamic set represented by a
binary search tree. After change the binary-search-tree property must be holds.
Insertion into a binary search tree is easier than deletion.
Insertion:
To insert a new node z with value v into the binary search tree has the following
fields
key[z] = v
left[z] = NIL
right[z] = NIL
p[z] = NIL
Steps to insert a new node into binary search tree:
Beginning at root of the binary search tree, trace a downward path,
maintaining two pointers.
Pointer x which traces the downward path.
Pointer y which keep track of parent of x.
Traverse the tree downward by comparing the value of node at x with v, and
Move to the left or right child accordingly.
9
When x is NIL, then it is at the correct position for node z.
Compare zs value with ys value, and insert z at either ys left or right,
appropriately.
Like the other primitive operations on search trees, the procedure TREE-INSERT
runs in O (h) time on a tree of height h.
Example: insert the nodes with key values 13 and 37 in the given binary search tree
Deletion:
TREE-DELETE (T, Z) procedure executes one of the following three cases:
Case I: if x is a leaf.
10
Then simply remove the x and point the parent to NULL:
Case II: if x has one left or right child, y.
Then parent of x point to xs child
Case III: if x has two children.
In this case, replace x's data with the successor's data and delete the successor:
11
The TREE-DELETE procedure runs in O (h) time on a tree of height h.
Example: Delete operation on a given binary search tree
Case 1: z has no child
Delete 65 from the given binary search tree
12
Case II: z has one child
Delete 57 from the given binary search tree
13
Case III: z has two child
Delete 32 from the given binary search tree
14
15