Here is the code of my binary tree can any one plzzz write the delet function for it.....
i tried my best but.....
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<Windows.h>
#include<stdlib.h>
using namespace std;
struct st
{
int marks;
st *left;
st *right;
};
struct st * create()
{
struct st *p=new st;
return p;
}
void read(struct st *cur)
{
cout<<"Enter The Marks :";
cin>>cur->marks;
fflush(stdin);
}
void display(struct st *cur)
{
cout<<"Marks :"<<cur->marks<<endl;
}
void insert(struct st **root,struct st *cur)
{
struct st *p=*root;
if(*root==NULL)
{
*root=cur;
cur->left=NULL;
cur->right=NULL;
}
else
{
if(p->marks>cur->marks)
insert(&p->left,cur);
else if(p->marks<cur->marks)
insert(&p->right,cur);
}
}
void display_lvr(struct st *root)
{
if(root!=NULL)
{
display_lvr(root->left);
display(root);
display_lvr(root->right);
}
}
void display_lrv(struct st *root)
{
if(root!=NULL)
{
display_lvr(root->left);
display_lvr(root->right);
display(root);
}
}
bool search(struct st *root,int n)
{
if(root==NULL)
return false;
else
{
if(root->marks==n)
return true;
else if(root->marks<n)
search(root->right,n);
else
search(root->left,n);
}
}
void display_vlr(struct st *root)
{
if(root!=NULL)
{
display(root);
display_lvr(root->left);
display_lvr(root->right);
}
}
struct st * find_parent(struct st *root,int n)
{
if(root==NULL || root->marks==n)
return NULL;
else
{
struct st *p;
while(root!=NULL)
{
p=root;
if(root->marks>n)
root=root->left;
else if(root->marks<n)
root=root->right;
if(root->marks==n)
return p;
}
}
}
struct st * find_brother(struct st *root,int n)
{
if(root==NULL || root->marks==n)
return NULL;
else
{
struct st *t;
t=find_parent(root,n);
if(t->left->marks==n)
return t->right;
else
return t->left;
}
}
void main()
{
char option;
struct st *root=NULL;
struct st *cur;
while(1)
{
cout<<"***Tree***\n"<<endl;
cout<<"1-Insert"<<endl;
cout<<"2-Search"<<endl;
cout<<"3-Delete"<<endl;
cout<<"4-Dispay in LVR order"<<endl;
cout<<"5-Dispay in VLR order"<<endl;
cout<<"6-Dispay in LRV order"<<endl;
cout<<"7-Quit\n"<<endl;
cout<<"Select An Option :";
option=getche();
if(option=='1')
{
system("cls");
cur=create();
read(cur);
insert(&root,cur);
}
else if(option=='2')
{
system("cls");
int n;
cout<<"Enter The Number to Search:";
cin>>n;
fflush(stdin);
if(search(root,n))
cout<<"\nNode Exists"<<endl;
else
cout<<"\nNot Doesn't Exists"<<endl;
}
else if(option=='3')
{
system("cls");
}
else if(option=='4')
{
system("cls");
display_lvr(root);
}
else if(option=='5')
{
system("cls");
display_vlr(root);
}
else if(option=='6')
{
system("cls");
display_lrv(root);
}
else if(option=='7')
{
system("cls");
break;
}
else
{
cout<<"\n\nWrong Option\n\n"<<endl;
}
}
}