1. DEFINITIONS :

1. DEFINITIONS 1.1. Binary Search Tree
A Binary Search Tree is a binary tree that may be empty. A non-empty binary search tree satisfies the following properties:
1. Every node has a value and no two elements
(nodes) have the same value, therefore all
values are distinct.
2. The values in the left subtree of the root are
smaller than the key in the root.
3. The values in the right subtree of the root
are larger than the key in the root.

Slide 5:

4. The left and right subtrees of the root are
also binary search trees.
EXAMPLE: 20 12 15 25 18 22 It’s not a Binary search Tree, because the right subtree fails to satisfy property – 3 and 4 FIGURE- (a)

Slide 6:

Another Definition :

Another Definition A Binary Search Tree (BST) is an ordered Binary Tree, either it’s an empty tree (or)
- each data value in it’s left subtree is less than
the root value
- each data value in it’s right subtree is greater
than the root value
- Left and Right subtrees are again binary
search tree All < K All > K K

2. ABSTRACT DATA TYPES :

2. ABSTRACT DATA TYPES Abstract Data Type BSTree
{
instance:
binary trees, each node has an element
with a key fields; all keys are distinct;
keys in the left subtree of any node are
smaller than the key in the node; those in
the right subtree are larger.
operations:
Create( ): create an empty binary search
tree

3. BINARY SEARCH TREE OPERATIONS :

3. BINARY SEARCH TREE OPERATIONS 3.1. SEARCHING
* Suppose we wish to search an element with key
(or) value (k), we begin at the Root. If the root is
NULL, the search tree contains no elements, and
the search is unsuccessful.
* Otherwise, we compare K-with the key/value in
the Root. If K – is less than the key/value in
the root, then no element in the right subtree can
have key/value (k) and only left subtree is to be
searched.

Slide 11:

* If k – is larger than the key in the root, only
the right subtree need to be searched.
* If k – equals to the key/value in the root, then
the search terminates successfully. The subtree
may be searched similarly.

Algorithm for BST - searching :

Algorithm for BST - searching algorithm SearchBST
search a binary search tree for a given value
precondition: root is the root to a binary tree (or)
subtree argument is the key value
requested.
Return: the node address, if the value is found null
if the node is not in the tree.
1. if (root is null)
1. return null
2. end if

Example – BST SEARCH :

Slide 15:

Suppose we are looking for node : 20, we begin by comparing the search argument 20, with the value in the tree “Root”.
Because 20 is less than the Root value 23, and because we know that all values less than the Root lie in it’s Left subtree, we go Left.
Now compare the search argument with the value in the subtree:18. This time the search argument is greater than the tree value:18
We know that values greater than the tree Root must lie in it’s Right Subtree, we go right and find our desired value.

The logic of BST – search work in the following ways: :

The logic of BST – search work in the following ways: To search a particular node in the BST, we use the procedure similar to “ Insertion”.
Beginning at the root node, the current node & the entered node are compared.
If the values are equal, SUCCESS is output. If the entered value is less than the value in the node then it must be in the left-child of subtree.
If there’s no left child subtree, the value is not in the tree, (i.e. a failure is reported).
If the entered value is greater than the value in the current node, the right child is searched.

Slide 18:

{ there is no left subtree;
return( string not found)
}
}
else if ( key-value stored in current node)
{ if ( right child exists)
{
find-key( key-value, right child);
}
else
{ there is no right subtree;
return (string not found)
}
}
}

3.2.insertion :

3.2.insertion All BST-insertion take place at a Leaf (or) Leaf like node.
Inserting a Node to the Tree:
* To insert a node in a BST, we must check whether
the tree already contains any nodes.
* If the tree is empty, the node is placed in the
Root node. If the tree is not empty, then the proper
location is found and the added node becomes either
a Left (or) a Right child of an existing node.

Slide 21:

Slide 22:

else
{
allocate new node and make right child point to it;
return (SUCCESS);
}
}

Algorithm for BST - insertion :

Algorithm for BST - insertion Algorithm addBST
insert node containing new data into BST using
recursion
Precondition : root is address of current node
in a BST.
new is address of node containing data
to be inserted.
Postcondition : new is address of node containing
data to be inserted.

Example – BST INSERTION :

Example – BST INSERTION Let us assume the following BST: 23 12 18 44 20 52 35 New node 19 is going to be added, to the existing BST

Slide 26:

Suppose if we want to insert the element 19, it will first compared to the Root node 23.
The element 19 is less than the root node 23, so insertion will take place left child of the Root node 23
(19 < 23).
The left side tree of root node 23 is: 23 12 18 20

Slide 27:

Again the new node 19 is compared with the left side tree root 18. Hence new node 19 is greater than the root 18. so node insertion is going to take place “Right side of the root 18”. ( 18 > 19).
The right side of node 18 contains the element 20. Hence new node 19 is less than the node 20. (19 < 20).
so the new node 19 is inserted into left side of the node 20. 18 20

Slide 28:

23 12 18 44 20 52 35 19

Slide 29:

3.3.Deletion :

3.3.Deletion METHOD-I
To delete a node from a binary search tree there are
four possible cases when we delete a node:
1. If node is a leaf node (or) node has no children, it
can be deleted by making it’s parent to null.
EXAMPLE:
Consider the following figure, to remove 35 from the tree, the left child field of its parent is set to null, and the node is discarded. The resulting tree will appear as follow 30 40 5 2 30 40 5 2 35 80 80

Slide 31:

2. If the node has one child, it’s parent pointer needs to
be adjusted.
EXAMPLE:
consider the following tree:
if node (1) is deleted, the right child of node(1) (that’s node (4)) become the left child of node (6). The new BST will be as follow: 8 9 6 1 7 4 8 9 6 4 7

Slide 32:

3. If the node to be deleted has two children, the node
value is replaced by the
(i) Smallest value in the Right subtree (or)
(ii) Largest value in the Left subtree
Example:
consider the following tree: 10 12 6 5 9 7 4 11 8

Slide 33:

If the node(6) is to be deleted, then it’s value is replaced by smallest value in the right subtree (i.e. by 7). The new BST will be; 10 12 7 5 9 8 4 11

Slide 37:

4. BINARY SERACH TREE WITH DUPLICATES :

4. BINARY SERACH TREE WITH DUPLICATES Definition
The BST is permitted to contain two (or) more elements that have the same key is called “ Duplicate Binary Search Tree” (DBST). 6 3 6 1 2 20 5 1 1 8

Slide 39:

We can remove the requirement that all elements in a BST need “distinct keys”. Now we can make smaller changes in property-2 and property-3 (refer slide-4).
Property-2:
the value in the left subtree of the root are smaller than and equal to (<=) key in the root.
propety-3:
the value in the right subtree of the root are greater than and equal to(>=) the key in the root.
The resulting tree is called “DBST”.

5. INDEXED BINARY SERACH TREE :

5. INDEXED BINARY SERACH TREE An Indexed BST, is derived from an ordinary BST-by adding the field “leftsize” to each tree node. This fields gives the number of elements in the nodes left subtree. 20 12 15 25 18 30 3 1 0 0 0 0 3- nodes

Slide 41:

30 40 5 2 2 1 0 0 2- nodes The above figures shows a Indexed BST. The number inside a node is the element key, the outside value of the node is the value of Leftside. Notice that the leftside also gives the index of an element with respect to the elements in its subtree.
For (e.g.) from the above figures, the element (in sorted order) in the subtree with root 20 are (12,15,18,25,30). The index of the root is 3, which is equal to its Leftsize value.

Slide 42:

In the subtree with root 15, the elements (in sorted order) are 12,18. so the index of root element 15 is 1, and its leftsize value is also 1.
ADT for Indexed Binary Search Tree
Abstract Data Type Indexed BST
{
instances: same as for BST, except that each node
has a leftsize field
operations:
get (k): return the element with key (k)
get (index) : return the index th element

6. APPLICATIONS :

Algorithm to find the smallest node in a BST :

Algorithm to find the smallest node in a BST precondition : root is a pointer to a nonempty BST
(or) subtree.
Return : address of smallest node
1. if ( root left null)
1. return (root)
2. end if
3. return smallestBST( root left)
end

Algorithm to find largest node in a BST :

Algorithm to find largest node in a BST precondition : root is a pointer to a nonempty BST
(or) subtree.
Return : address of largest node
1. if ( root right null)
1. return (root)
2. end if
3. return largestBST( root right)
end