You are on page 1of 15

BINARY SEARCH TREE (INTRODUCTION)

BY:

ANIL JAISWAL (2010AN-02)

ANURAG GUPTA (2010IS-02)

SHRIDHER (2010IS-22)
INDEX
1. Introduction

2. Binary Search Tree

3. Searching in Binary Search Tree

3.1 Analysis of Search

3.1.1 Recursive Search

3.1.2 Iterative Search

3.2 Finding the Minimum Value

3.3 Finding the Maximum Value

4. Insertion in Binary Search Tree

5. Deletion in Binary Search Tree

6. Applications of Binary Search Trees

7. Implementation of BST in ‘C’

8. Reference
1. Introduction

In Computer science, almost everywhere we require searching data. In earlier days only if we get the
desired result only that matters but now we are focusing on how efficient we are. Which will get the
result in lesser time. There are many algorithms available for searching. In these available algorithms
here we are discussing the Binary Search Tree, whose searching time is lesser than most of the
algorithms.

Before defining Binary search Tree, first we define the Binary Tree.

A Binary tree is a special kind of tree, one that limits each node to no more than two children. A
binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data
element. The "root" pointer points to the topmost node in the tree. The left and right pointers
recursively point to smaller "subtrees" on either side. A null pointer represents a binary tree with no
elements -- the empty tree.

A binary tree also possible either empty or is made of a single node, where the left and right pointers
of single node tree will be null.

Root Node
5

Left and Right

Subtree Pointers

8 3

18 10 6

Binary Tree Representation


2.Binary Search Tree

There are many kinds of Binary trees

1. Complete Binary Tree.

2. AVL Tree.

3. Threaded Binary tree.

4. Red Black tree.

5. Binary Search Tree.

We define the Binary search Tree. A Binary Search Tree is a special kind of binary tree It may be
empty or it have only one node that is root node. If it has more than root node then it has to satisfy the
following properties:

1) Every node has maximum one key and keys should be distinct.

2) The keys in the left subtree should be smaller than root’s key.

3) The keys in the right subtree should be greater than root’s key.

4) Left and Right subtrees also satisfy the binary search tree property.

These properties also follow that:

 Every node should have different values.

The major achievement in Binary Search Tree over other sorting and search algorithm is that binary
search tree is very efficient in in-order traversal. Binary search trees are a fundamental data structure
used to create more abstract data structures such as sets, multisets and associative array.

5 15

2 6 13 20

16
7

Representation of Binary Search Tree


3. Searching in Binary Search Tree

As the Binary Search tree definition is recursive, it is good for searching process in it.

If we wish to search for a node whose key is k. We have to begin with the root of the tree. Suppose if
the root is NULL then the tree is empty that is search is unsuccessful.If this is not the case then
compare the key value with the root of the tree. If root’s key value equal to k then search is successful
and it is the best case in the binary search tree we get the result in the first attempt .Otherwise we
compare the key k with root value if it is less than the root value then we start searching in the left
subtree of the binary subtree. If value is greater than key k then we start searching in the right subtree
of the tree.

3.1 Analysis of Search

A binary search tree algorithm can search a node in an N node tree in order log(N) time (log base 2).
Therefore, binary search trees are good for "dictionary" problems where the code inserts and looks up
information indexed by some key. The log(N) behavior is the average case -- it's possible for a
particular tree to be much slower depending on its shape.

we use the following algorithm to find a node with a given value in a binary search tree. Pointer to the
root of the tree and a value p, search(t, p) returns a pointer to a node with value p if exists. Otherwise,
it returns NULL. we have two algorithm recursively and iteratively.

3.1.1 Recursive Search

(a) search(t, p)

1 if t=NULL OR p=t->value
2 then return t
3 if p > t->value
4 then return search (t->right, p)
5 else return search (t->left, p)

3.1.2 Iterative Search

(b) search (t, p)

1 while t != NULL AND p!=t->value


2 do if (p > t->value)
3 then t=t->right
4 else t=t->left
5 return t
3.2 Finding the Minimum Value

Through this algorithm we can find the minimum value node in the given binary search tree.
A node in a binary search tree whose value is minimum is the left most node is found by traversing
left child pointers from root until last node has its left child equal to NULL.

(c) minimum (t)

1 while t != NULL
2 do t = t->left
3 return t

3.3 Finding the Maximum Value

Through this algorithm we can find the maximum value node in the given binary search tree.
An node in a binary search tree whose value is minimum is the right most node is found by traversing
right child pointers from root until last node has its right child equal to NULL.

(d) maximum (t)

1 while t != 1
2 do t = t->right
3 return t

4. Insertion in Binary Search Tree

Insertion in binary search tree is done by traversing the node of that value then by this we found the
right position to insert a node, then we compare the node value to the value to be inserted , if value
come greater than we insert into its right child and if value comes less than we insert as its left child.
To insert a new value into binary search tree we use this algorithm insert(). the procedure passed a
node z for which z->value equal to some value and left node of z is NULL and right node of z is
NULL. This algorithm insert z into appropriate position in given tree T.

(e) insert (T, z)

1 y = NULL
2 x = T->root
3 while x != NULL
4 do y = x
5 if z < x->value
6 then x = x->left
7 else x = x->right
8 p[z] = y
9 if y = NULL
10 then T->root = z
11 else if z < y->value
12 then y->left = z
13 else y->right = z
5. Deletion in Binary Search Tree

Deletion in binary search tree is have three cases a node having no child, a node having one child and
a node having two child we consider all three cases and delete node according to its number of child.
To delete a value from binary search tree we use this algorithm delete(T,z). The procedure for
deleting a given node z from BST takes as an argument a pointer to z. this algorithm consider three
cases
(a) if z has no children
(b) if z has one child
(c) if z has two children

This algorithm uses one more procedure successor() which gives successor of a given node .

delete(T. z)

1 if z->left = NULL OR z->right = NULL


2 then y = z
3 else y = successor(z)
4 if y->left! = NULL
5 then x = y->left
6 else x = y->right
7 if x != NULL
8 then p[x] = p[y]
9 if p[y] = NULL
10 then t->root = x
11 else if y = p[y]->left
12 then p[y]-> left = x
13 else p[y]->right = x
14 if y!= z
15 then z->value = y->value
16 return y

successor (z)

1 if x->right != NULL
2 then return minimum (x->right)
3 y = p[x]
4 while y != NULL AND x = y->right
5 do x = y
6 y = p[y]
7 return y

6. Applications of Binary Search Trees

One of the applications of a binary search tree is the implementation of a dynamic dictionary. This
application is appropriate because a dictionary is an ordered list that is required to be searched
frequently, and is also required to be updated (insertion and deletion mode) frequently. So it can be
implemented by making the entries in a dictionary into the nodes of a binary search tree. A more
efficient implementation of a dynamic dictionary involves considering a key to be a sequence of
characters, and instead of searching by comparison of entire keys, we use these characters to
determine a multi-way branch at each step. This will allow us to make a 26-way branch according to
the first letter, followed by another branch according to the second letter and so on.
7. Implementation of BST in ‘C’

By taking help of above algorithms we have design a menu driven program for binary search tree
which do following functions
1. Insert a new node
2. Delete a given node
3. Show a tree in inorder
4. Find maximum value in a tree
5. Find minimum value in a tree
6. Find number of nodes in a tree
7. Find number of leafs in a tree

To do this we use certain functions which stated as follows


struct tnode *insert(struct tnode *p,int val) – for inserting a node
struct tnode *del(struct tnode *p,int val) – for deleting a node
struct tnode *getptr(struct tnode *p, int key, struct tnode **y) – for locating a node
void inorder1(struct tnode *p) – to arrange a tree in inorder and display it
void maximum(struct tnode *p) – to find maximum value in a tree
void minimum(struct tnode *p) – to find a minimum value in a tree
void tnn(struct tnode *p) – to find the total number of nodes in a tree
void tnl(struct tnode *p) – to find the total number of leaf nodes in a tree

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct tnode
{
int data;
struct tnode *lchild, *rchild;
};

/* A function to get a pointer to the node whose data value is given as well as the pointer to its root */

struct tnode *getptr(struct tnode *p, int key, struct tnode **y)
{
struct tnode *temp;
if( p == NULL)
return(NULL);
temp = p;
*y = NULL;
while( temp != NULL)
{
if(temp->data == key)
return(temp);
else
{
*y = temp; /*store this pointer as root */
if(temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
}
return(NULL);
}
/* A function to del the node whose data value is given */

struct tnode *del(struct tnode *p,int val)


{
struct tnode *x, *y, *temp;
x = getptr(p,val,&y);
if( x == NULL)
{
printf("The node does not exists\n");
return(p);
}
else
{
/* this code is for deleting root node*/
if( x == p)
{
temp = x->lchild;
y = x->rchild;
p = temp;
while(temp->rchild != NULL)
temp = temp->rchild;
temp->rchild=y;
free(x);
return(p);
}
/* this code is for deleting node having both children */
if( x->lchild != NULL && x->rchild != NULL)
{

if(y->lchild == x)
{
temp = x->lchild;
y->lchild = x->lchild;
while(temp->rchild != NULL)
temp = temp->rchild;
temp->rchild=x->rchild;
x->lchild=NULL;
x->rchild=NULL;
}
else
{
temp = x->rchild;
y->rchild = x->rchild;
while(temp->lchild != NULL)
temp = temp->lchild;
temp->lchild=x->lchild;
x->lchild=NULL;
x->rchild=NULL;
}

free(x);
return(p);
}
/* this code is for deleting a node with on child*/
if(x->lchild == NULL && x->rchild != NULL)
{
if(y->lchild == x)
y->lchild = x->rchild;
else
y->rchild = x->rchild;
x->rchild = NULL;
free(x);
return(p);
}
if( x->lchild != NULL && x->rchild == NULL)
{
if(y->lchild == x)
y->lchild = x->lchild ;
else
y->rchild = x->lchild;
x->lchild = NULL;
free(x);
return(p);
}
/* this code is for deleting a node with no child*/
if(x->lchild == NULL && x->rchild == NULL)
{
if(y->lchild == x)
y->lchild = NULL ;
else
y->rchild = NULL;
free(x);
return(p);
}
}
}

/*an iterative function to print the binary tree in inorder*/


void inorder1(struct tnode *p)
{
struct tnode *stack[100];
int top;
top = -1;
if(p != NULL)
{
top++;
stack[top] = p;
p = p->lchild;
while(top >= 0)
{
while ( p!= NULL)/* push the left child onto stack*/
{
top++;
stack[top] =p;
p = p->lchild;
}
p = stack[top];
top--;
printf("%d\t",p->data);
p = p->rchild;
if ( p != NULL) /* push right child*/
{
top++;
stack[top] = p;
p = p->lchild;
}
}
}
}

/* A function to insert a new node in binary search tree to get a tree created*/
struct tnode *insert(struct tnode *p,int val)
{
struct tnode *temp1,*temp2;
if(p == NULL)
{
p = (struct tnode *) malloc(sizeof(struct tnode)); /* insert the new node as root node*/
if(p == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
p->data = val;
p->lchild=p->rchild=NULL;
}
else
{
temp1 = p;
/* traverse the tree to get a pointer to that node whose child will be the newly created node*/
while(temp1 != NULL)
{
temp2 = temp1;
if( temp1 ->data > val)
temp1 = temp1->lchild;
else
temp1 = temp1->rchild;
}
if( temp2->data > val)
{
temp2->lchild = (struct tnode*)malloc(sizeof(struct tnode));/*inserts the newly created node
as left child*/
temp2 = temp2->lchild;
if(temp2 == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
temp2->data = val;
temp2->lchild=temp2->rchild = NULL;
}
else
{
temp2->rchild = (struct tnode*)malloc(sizeof(struct tnode));/*inserts the newly created node
as left child*/
temp2 = temp2->rchild;
if(temp2 == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
temp2->data = val;
temp2->lchild=temp2->rchild = NULL;
}
}
return(p);
}

/* A function to maximum value node in binary search tree */


void maximum(struct tnode *p){
struct tnode *s,*r;
s=p;
while(s!= NULL){
r=s;
s = s->rchild;
}
printf("%d\t",r->data);
}

/* A function to minimum value node in binary search tree */


void minimum(struct tnode *p){
struct tnode *s,*r;
s=p;
while(s!= NULL){
r=s;
s = s->lchild;
}
printf("%d\t",r->data);
}
/* A function to find total number of node in binary search tree */
int tnn(struct tnode *p)
{
int x=0;
if( p == NULL)
return(0);
else
if( p->lchild == NULL && p->rchild == NULL)
return(1);
else
x=(1 + (tnn(p->lchild) + tnn(p->rchild)));
return(x);
}

/* A function to find total number of leaf node in binary search tree */


void tnl(struct tnode *p){
struct tnode *stack[100];
int top,n=0;
top = -1;
if(p != NULL)
{
top++;
stack[top] = p;
p = p->lchild;
while(top >= 0)
{
while ( p!= NULL)/* push the left child onto stack*/
{
top++;
stack[top] =p;
p = p->lchild;
}
p = stack[top];
top--;
if(p->lchild == NULL && p->rchild == NULL)
n++;
p = p->rchild;
if ( p != NULL) /* push right child*/
{
top++;
stack[top] = p;
p = p->lchild;
}
}
}
printf("Total Number Of leaf nodes = %d", n);
}
void main() {
clrscr();
struct tnode *root = NULL;
int x=0,n,y;
while(1)
{
printf("\n\nEnter your choice\n1. Insert a new node \n2. Delete a node\n3. Show a tree\n4.
Maximum value\n5. Minimum value\n6.Total number of nodes\n7. Total leaf node\n8. Exit\n ");
scanf("%d",&x);
switch(x) {
case 1:
printf("Enter a node value\n");
scanf("%d",&n);
root=insert(root,n);
break;
case 2:
printf("Enter a node value\n");
scanf("%d",&n);
root=del(root,n);
break;
case 3:
inorder1(root);
break;
case 4:
maximum(root);
break;
case 5:
minimum(root);
break;
case 6:
y=tnn(root);
printf(“%d”,y);
break;
case 7:
tnl(root);
break;
case 8:
exit(0);
default:
printf("Enter Right Choice\n");
}
}
}
8. References

 Introduction to Algorithms By Cormen .


 Fundamentals of data structures in C By Horowitz,sahani.
 Microsoft MSDN Library.
 www.wikipedia.org
 www.cprogramminglanguage.net

You might also like