ErroR in Deleting of a node in Tree..
Jan 1, 2013 at 4:51pm UTC
Hey Experts..!!
i try my best on tree while i am deleting a node from Tree..
it place remains same but value becomes 0..
Plz help me out from this situation
here code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#include<conio.h>
#include<iostream.h>
struct node
{
int info;
node *left,*right,*parent;
};
node *root,*ntd;
void create()
{
node*par,*x;
par=NULL;
x=root;
ntd=new node;
cout<< " Enter a value" ;
cin>>ntd->info;
while (x!=NULL)
{
par=x;
if (ntd->info < x->info)
x=x->left;
else
x=x->right;
}
ntd->parent=par;
if (par==NULL)
root=ntd;
else if (ntd->info<par->info)
par->left=ntd;
else
par->right=ntd;
}
void preorder(node *r)
{ if (r==NULL)
return ;
cout<<" " <<r->info<<"\t" ;
preorder(r->left);
preorder(r->right);
}
node * min(node *m)
{
while (m->left!=NULL)
m=m->left;
return m;
}
node * treesuc(node * s)
{
return min(s->right);
}
void del(node *r,int num)
{ node *z,*y,*x,*ptr;
while (r!=NULL)
{
if (num==r->info)
{ ptr=r; break ;}
else if (num < r->info)
r=r->left;
else
r=r->right;
}
z=ptr;
if (z->left==NULL || z->right==NULL)
y=z;
else y=treesuc(z);
if (y->left!=NULL)
x=y->left;
else
x=y->right;
if (x!=NULL)
x->parent=y->parent;
if (y->parent=NULL)
root=x;
else if (y=y->parent->left)
y->parent->left=x;
else
y->parent->right=x;
if (y!=z)
z->info=y->info;
}
void main()
{
clrscr();
root=NULL;
int nu;
create();
// preorder(root);
for (int i=1;i<=5;i++)
create();
preorder(root);
cout<<"\nEnter a number to del from tree" ;
cin>>nu;
del(root,nu);
preorder(root);
create();
preorder(root);
getch();
}
Jan 1, 2013 at 7:30pm UTC
Probably the first thing to do is to start paying attention to the warnings generated by your compiler.
To compare two variables we use == . main returns int. Use #include <iostream>
instead of <iostream.h>
.
Jan 10, 2013 at 4:19pm UTC
i am using Turbo c...
here we write
#include <iostream>
== where to add line number
Jan 10, 2013 at 4:31pm UTC
Line 88:
else if (y=y ->parent->left)
All the best,
NwN
Jan 10, 2013 at 4:40pm UTC
thanks
Topic archived. No new replies allowed.