BST Source-Code

A

AshifToday

another program from my side
==

/*





*/

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <assert.h>
#include <process.h>
#include <dos.h>



class node
{
public:
int data;
node *lc;
node *rc;
};

class btree
{
private:
node *root;
int insert(node*);
void preorder(node*);
void inorder(node*);
void postorder(node*);
node* search(int,node*);

public:
btree() //Constructor
{ root = NULL;
cnt_nde = 0;
}
int cnt_nde;
//Functions/Operations
int create(int);
void del(int);
void modify();
void display(int);
void showall();
void showroot();
node* parent(node*);
};

int btree::create(int val)
{
node *temp = new node();
assert(temp!=NULL);
temp->lc = NULL;
temp->rc = NULL;
temp->data = val;
int check=0;
check = insert(temp);
if(check == 0)
{
cout<<"\n\nNot Inserted, KEY already Exists";
getch();
return 0;
}
cnt_nde++;
return 1;

}

int btree::insert(node *temp)
{
if(root == NULL)
{
root = temp;
return 1;
}
else
{
node *t2 = root;
while(t2)
{
if(temp->data < t2->data)
{
if(t2->lc == NULL)
{
t2->lc = temp;
return 1;
}
else
{ t2 = t2->lc;}

}
else if(temp->data > t2->data)
{
if(t2->rc == NULL)
{
t2->rc = temp;
return 1;
}
else
{ t2 = t2->rc; }
}
else
{
cout<<"Equal Case";
return 0;
}
}
}
}

void btree::showall()
{
clrscr();
int ch = 0;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
cout<<" WELCOME TO SHOW ALL MENU \n\n";
cout<<"\nEnter Your Choice : \n";
cout<<"A.PREOERDER.................................1\n";
cout<<"B.INORDER...................................2\n";
cout<<"C.POST ORDER................................3\n";
cout<<"D.Exit......................................4\n\n";
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"Total Number Of Nodes : "<<cnt_nde;
cout<<"\n\nYour Desired Choice ? [1 - 4] : ";
cin>>ch;
if(root == NULL && ch !=4)
ch = 5;
switch(ch)
{
case 1:
clrscr();
cout<<"IN PRE-ORDER\n\n";
preorder(root);
getch();
break;
case 2:
clrscr();
cout<<"IN IN-ORDER\n\n";
inorder(root);
getch();
break;
case 3:
clrscr();
cout<<"IN POST-ORDER\n\n";
postorder(root);
getch();
break;
case 4:
break;
case 5:
clrscr();
cout<<"SORRY ROOT IS NULL, NOTHING TO DISPLAY";
getch();
break;
default:
cout<<"Wrong Input\nPress any key to select again...";
showall();
}
}

void btree::preorder(node *temp)
{
if(temp != NULL)
{
cout<<temp->data<<", "; //N
preorder(temp->lc); //L
preorder(temp->rc); //R
}

}
void btree::inorder(node *temp)
{
if(temp != NULL)
{
inorder(temp->lc); //L
cout<<temp->data<<", "; //N
inorder(temp->rc); //R
}
}
void btree::postorder(node *temp)
{
if(temp != NULL)
{
postorder(temp->lc); //L
postorder(temp->rc); //R
cout<<temp->data<<", "; //N
}
}

/* GLOBEL */
node *current = NULL;
node *prnt = NULL;
/* GLOBEL */
void btree::del(int val)
{

current = search(val,root);
if(current == NULL)
{
clrscr();
cout<<"NODE DOES NOT EXISTS ";
cout<<"\nPress Any Key...";
getch();
}
else
{
if(current->lc == NULL && current->rc == NULL)
{
if(current == root)
{
node *dlte = current;
delete dlte;
root = NULL;
root->lc = NULL;
root->rc = NULL;
}
else
{
prnt = parent(current);
if(prnt->rc == current)
{
node *dlte = current;
delete dlte;
prnt->rc = NULL;
}
else
{
node *dlte = current;
delete dlte;
prnt->lc = NULL;
}
}
}
else if(current->lc == NULL && current->rc != NULL)
{
prnt = parent(current);
if(prnt == NULL)
{
root = root->rc;
node *dlte = current;
delete dlte;
////////
// root->rc = root->lc = NULL;
////////
}
else if(prnt->rc == current)
{
prnt->rc = current->rc;
node *dlte = current;
delete dlte;
////////
// root->rc = root->lc = NULL;
////////
}
else
{
prnt->lc = current->rc;
node *dlte = current;
delete dlte;
////////
// root->rc = root->lc = NULL;
////////

}
}
else if(current->rc == NULL && current->lc != NULL)
{
prnt = parent(current);
if(prnt == NULL)
{
root = root->lc;
node *dlte = current;
delete dlte;
////////
// root->rc = root->lc = NULL;
////////
}else if(prnt->rc == current)
{
prnt->rc = current->lc;
node *dlte = current;
delete dlte;
////////
// root->rc = root->lc = NULL;
////////
}
else
{
prnt->lc = current->lc;
node *dlte = current;
delete dlte;
////////
// root->rc = root->lc = NULL;
////////
}
}
else
{
prnt = parent(current);
if(prnt == NULL)
{
node *rmin = current->rc;
while(rmin->lc != NULL)
{ rmin = rmin->lc; }
rmin->lc = current->lc;
root = current->rc;
node *dlte = current;
delete dlte;
}
else
{
node *max = current->lc;
while(max->rc !=NULL)
{ max = max->rc; }
max->lc = current->lc;
max->rc = current->rc;
if(prnt->lc == current)
{
prnt->lc = max;
node *dlte = current;
delete dlte;
}
else
{
prnt->rc = max;
node *dlte = current;
delete dlte;
}
}
}
cnt_nde--;
}
}

node* btree::search(int val,node *temp)
{
if(val == temp->data)
return temp;
else if(val < temp->data)
{
if(temp->lc != NULL)
search(val,temp->lc);
else
return NULL;
}
else
{
if(temp->rc != NULL)
search(val,temp->rc);
else
return NULL;
}
}

node* btree::parent(node *temp)
{
if(temp == root)
return NULL;
else
{
node *tm2 = root;
while(tm2->lc != temp && tm2->rc != temp)
{
if(temp->data < tm2->data)
tm2 = tm2->lc;
else
tm2 = tm2->rc;
}
return tm2;
}
}


void btree::display(int val)
{
clrscr();
node *current = NULL;
current = search(val,root);
if(current != NULL)
cout<<"Node Found, Node Data = "<<current->data;
else
cout<<"Node Does Not Exists..\nPress Any Key...";
getch();
}

void btree::showroot()
{
clrscr();
cout<<"\n\nCURRENTLY ROOT IS POINTING TO ";
if(root != NULL)
cout<<root->data;
else
cout<<"NULL";
cout<<" DATA";
getch();
}

void btree::modify()
{

char ch = NULL;
int val = 0;
int _val = 0;
while(ch != '2')
{
clrscr();
ch = NULL;
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n";
cout<<" MODIFY NODE MENU \n\n";
cout<<"* SEARCH & MODIFY NODE....................1\n";
cout<<"* GO BACK.................................2\n\n";
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n";
cout<<"Enter Your Choice [1/2] : ";
cin>>ch;
switch(ch)
{
case '1':
cout<<"\nEnter Value To Search : ";cin>>val;
node *current = search(val,root);
if(current != NULL)
{
cout<<"\n\nNode Found, Enter New Value : ";
cin>>_val;
_val = create(_val);
if(_val)
{
del(val);
cout<<"\n\nNode Successfully Modified ";
getch();
}
else
{
cout<<"\n\nSorry Could Not Modify, Try Again";
getch();
}
}
else
{
cout<<"\n\nSorry Node Not Found, Try Again ";
getch();
}
break;

case '2':
break;

default:
cout<<"\n\nWrong Input Try Again....\nPress Any Key...";
getch();
continue;
}
}

}




/////////////////////MAIN/////////////////////////////

void main (void)
{
void in_out(int);
in_out(10);
char choice = NULL;
int ins = 0;
int val=0;
int howmany = 0;
btree bst;
while(choice != '0')
{
choice = NULL;
clrscr();
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n";
cout<<" WELCOME TO BST MENU ";
cout<<"\n\nENTER YOUR CHOICE : \n\n";
cout<<"* Create & INSERT NODE..........................1\n";
cout<<"* SHOW ALL NODES................................2\n";
cout<<"* DELETE A NODE.................................3\n";
cout<<"* MODIFY A NODE.................................4\n";
cout<<"* SEARCH A VALUE................................5\n";
cout<<"* SHOW ROOT.....................................6\n";
cout<<"* EXIT PROGRAM..................................7\n\n";
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
cout<<"Total Number of Nodes : "<<bst.cnt_nde<<"\n\n";
cout<<"YOUR DESIRED INPUT [1 - 7] : ";
cin>>choice;
if(ins == 1 && bst.cnt_nde == 0)
ins = 0;
switch(choice)
{
case '1':
cout<<"\nHow Many Nodes You Want to Create : ";cin>>howmany;
while(howmany)
{
gotoxy(10,22);
clreol();
cout<<"Enter Value Of Node : ";cin>>val;
val = bst.create(val);
howmany--;
}
ins = 1;
break;

case '2':
if(ins)
bst.showall();
else
{
cout<<"\n\nNo Node Yet Created Or Inserted";
getch();
}
break;

case '3':
if(ins)
{
cout<<"\n\nEnter Key Value Of Node To Be Deleted : ";
cin>>val;
bst.del(val);
}
else
{ cout<<"\n\nNo Node Yet Created Or Inserted";
getch();
}
break;
case '4':
if(ins)
{
bst.modify();
}
else
{
cout<<"\n\nNo Node Yet Created Or Inserted";
getch();
}
break;


case '5':
if(ins)
{
cout<<"\n\nEnter Key Value of Node To Be Searched : ";
cin>>val;
bst.display(val);
}
else
{
cout<<"\n\nNo Node Yet Created Or Inserted";
getch();
}
break;

case '6':
if(ins)
{
bst.showroot();
}
else
{
cout<<"\n\nNo Node Yet Created Or Inserted";
getch();
}
break;

case '7':
in_out(3);
exit(1);

default:
cout<<"\n\nWrong Input.....\n\nPress any key to do again";
getch();
continue;
}
}
}

void in_out(int val)
{
clrscr();
cout<<" \n"
<<" FAISALABAD COLLEGE OF SCIENCE, FAISALABAD.\n\n"
<<"TITLE : COMPLETE BINARY SEARCH TREE\n\n"
<<"PRESENTED TO:\n\n"
<<" MR. EHSAN ASIM, (IT-DEPARTMENT).\n\n"
<<"PRESENTED BY:\n"
<<" MR. AHSIN LATIEF - 1464\n"
<<" MR. ASHIF ZUBAIR - 1468\n"
<<"\n"
<<"-----------------------------------------------------------\n"\
<<" THIS PROGRAM IS THE PRIVATE PROPERTY OF\n"
<<" A & A GROUP. COPYRIGHTS 2006 - ALL RIGHTS ARE RESERVED!\n"
<<"-----------------------------------------------------------\n"
<<" YOU AUTOMATICALLY AGREE TO THE TERMS AND CONDITIONS WHILE\n"
<<" USING THIS PROGRAM.A&A GROUP IS NOT RESPONSIBLE FOR ANY\n"
<<" DAMAGE THIS PROGRAM MAY CAUSE TO YOUR SYSTEM.\n"
<<" ALL RIGHTS ARE RESERVED!\n"
<<"-----------------------------------------------------------\n"
<<"You Will Be Redirected In "<<val<<" Seconds\nThank You For Using
Our Program";
if(val == 3)
delay(3000);
delay(10000);
}

/*=======================*/
more enhancement acn be made in this program by adding the functions
for displaying the leaf nodes, internal nodes function , and the
lowest value, and the highest value in the tree
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top