#
include<iostream>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
using namespace std;
typedef struct BST
{
int data;
struct BST *lchild, *rchild;
}
node;
void insert (node *,node *);
void inorder (node *);
void preorder (node *);
void postorder (node *);
node *search (node *, int , node **);
int main()
{
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
system("CLS");
printf("\nProgram For Binary Search Tree");
do //do1
{
printf("\n1. Create");
printf("\n2. Search");
printf("\n3. Recursive Traversals");
printf("\n4. Exit");
cout<<"\nEnter your choice: ";
scanf("%d", &choice);
switch(choice)
{ //switch
case 1:
do //do2
{
new_node=get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
if(root==NULL)
root = new_node;
else
insert (root, new_node);
printf("\nWant to enter more elements?(y/n)");
cin>>ans;
} //do2
while (ans == 'y');
break;
case 2:
printf("\nEnter Element to be searched: ");
cin>>key;
tmp = search (root, key, &parent);
printf("\nParent of node %d is %d ", tmp->data, parent->data);
break;
case 3:
if (root==NULL)
printf("Tree is not created");
else
{
printf("\nThe Inorder display: ");
inorder(root);
printf("\nThe Pre-order display: ");
preorder(root);
printf("\nThe Post-order display: ");
postorder(root);
}
break;
} //switch
}//do1
while (choice !=4);
}
////////////////////////////////////////////////////////////////////
node *get_node()
{
node *temp;
temp = (node *)malloc(sizeof(node));
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
void insert (node *root, node *new_node)
{
if(new_node->data > root->data)
{
if (root->lchild==NULL)
root->lchild = new_node;
else
insert (root->lchild , new_node);
}
if(new_node->data > root -> data)
{
if(root->rchild==NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
node *search(node *root, int key, node**parent)
{
node *temp;
temp = root;
while (temp != NULL)
{
if(temp->data==key)
{
printf("\nThe %d Element is present ", temp->data);
return temp;
}
*parent = temp;
if(temp->data==key)
temp=temp->rchild;
else
temp=temp->rchild;
}
return NULL;
}
void inorder (node *temp)
{
if(temp != NULL)
{
inorder(temp->lchild);
printf("%d ", temp->data);
inorder (temp->rchild);
}
}
void preorder (node *temp)
{
if(temp != NULL)
{
printf("%d ", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
void postorder (node *temp)
{
if(temp != NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d ", temp->data);
}
}
--
To post to this group, send email to vuhelp_pk@googlegroups.com
To unsubscribe from this group, send email to vuhelp_pk+unsubscribe@googlegroups.com
Group Rules Vuhelp4u
Sharing of Video songs links, movies links, dramas links are not allowed in study group. Only Islamic and general information Video links allowed.
SPAM, Advertisement, and Adult messages are NOT allowed and that member will be behaved strictly.
http://groups.google.com/group/vuhelp_pk?hl=en_US
No comments:
Post a Comment