Lecture 9 PDF
Lecture 9 PDF
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.
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,
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
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.
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:
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.
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
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.
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:
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).
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
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:
Case I: if x is a leaf.
10
Then simply remove the x and point the parent to NULL:
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.
12
Case II: z has one child
13
Case III: z has two child
14
15