Click here to Skip to main content
15,884,892 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

struct node
{
   int data;
   node *left;
   node *right;
};
node * par(int z,node *root);
node *binary_tree(node *p, int data)
{
  node *temp;
  if(p==NULL)
  {
    p=new node;
    p->data=data;
    p->left=p->right=NULL;
  }
  else if(data>(p->data))
  {
    p->right=binary_tree(p->right,data);

  }
  else
  {
    p->left=binary_tree(p->left,data);
  }
  }


  int search_tree(node *p,int k)
  {
  	  if(p==NULL) return 0;
  	  else if(k==p->data) return 1;
  	  else if(k>=p->data)  search_tree(p->right,k);
  	  else search_tree(p->left,k);



  }

node * find_min(node *p)
  {

      node *t,*temp;
      while(p!=NULL)
      {
          t=p;
          p=p->left;
      }
      return(t);
  }

void del_node(int k,node *root)
{
    node *z,*temp;
    node *parent=par(k,root);
    if(parent->left->data==k) z=parent->left;
    else z=parent->right;
    if(z->left==NULL && z->right==NULL)
    {
        if(parent->right==z)
        {
            parent->right=NULL;
            free(z);
        }
        else
        {
            parent->left=NULL;
            free(z);
        }
    }
    else if(z->left==NULL || z->right==NULL)
    {
        if(z->right!=NULL)
        {
            if(parent->right==z)
            {
                parent->right=z->right;
                free(z);
            }
            else
            {
                parent->left=z->right;
                free(z);
            }
        }
        else if(z->left!=NULL)
        {
            if(parent->right==z)
            {
               parent->right=z->left;
               free(z);
            }
            else
            {
               parent->left=z->right;
               free(z);
            }
        }
    }
    else
    {
       node *m=find_min(z->right);
       cout<<endl<<"min value right of node to be deleted "<<m->data;
       int j=m->data;
       del_node(j,root);
       z->data=j;

    }
}


node * par(int z,node *root)
{
    node *temp=root;
    while(1)
    {
        if(temp->left==NULL && temp->right!=NULL && temp->right->data!=z) temp=temp->right;
        else if(temp->left==NULL && temp->right!=NULL && temp->right->data==z) {return temp;break;}
        else if(temp->left!=NULL && temp->right==NULL && temp->right->data!=z) temp=temp->left;
        else if(temp->left!=NULL && temp->right==NULL && temp->right->data==z) {return temp;break;}
        else
        {
            if(temp->right->data==z || temp->left->data==z) {return temp;break;}
            else if(z>temp->data) temp=temp->right;
            else if(z<temp->data) temp=temp->left;
        }
    }
    return temp;
}


int main()
{
  cout<<endl<<"enter the number of elements you want to insert ";
  int n,val;
  node *root=NULL;
  cin>>n;
  for(int i=0;i<n;i++)>
  {
    cout<<endl<<"enter the value of data ";
    cin>>val;
    root= binary_tree(root,val);
  }
 // cout<<endl<<((root->right)->right)->data;
  cout<<endl<<"enter a number to search in the tree ";
  int k;
  cin>>k;
  int j=search_tree(root,k);
  if(j==1) cout<<endl<<"the number is present in the tree ";
  else cout<<endl<<"the number is not present in the tree";
  cout<<endl<<" enter the number you want to delete ";
  cin>>k;
  del_node(k,root);
  cout<<endl<<"the node was successfully deleted ";


}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 18-Jan-14 20:45pm
v2
Comments
OriginalGriff 19-Jan-14 2:52am    
Before we try to answer this, you need to do a couple of things:
1) Move the question itself from the subject line into the body of the question. The subject should be a brief summary - "Binary tree node delete crashes" or similar - so we can tell if we know the general area you are having problems with. When you try to put the whole of your question in there, it gets truncated, and we don't know what you are trying to say.
2) Sort out your indentation: it's all over the place and makes it hard to read your code, particularly if you aren't familiar with it - as we aren't.
3) Use a code block to preserve any formatting and engage the syntax highlighter - I added it to yours - it make it easier to read again.
4) Tell us what data you provided, and what you did - you may have tried to do that already, but since your question was truncated, we can't tell.

Use the "Improve question" widget to edit your question and provide better information.

I just reformatted your code, and one thing springs to mind: it's not (necessarily) your delete code: your build code is very, very faulty as well:
C++
node *binary_tree(node *p, int data)
    {
    node *temp;
    if(p==NULL)
        {
        p=new node;
        p->data=data;
        p->left=p->right=NULL;
        }
    else if(data>(p->data))
        {
        p->right=binary_tree(p->right,data);

        }
    else
        {
        p->left=binary_tree(p->left,data);
        }
    }
Look at this code: you use it to build a new tree. What doesn't happen here that should?

Hint:
C++
node *binary_tree(node *p, int data)
Declares a function returning a pointer to a node. What value does this function actually return? Where does it return it?

How did you get this to compile, never mind run?
 
Share this answer
 
Comments
Member 10533327 19-Jan-14 13:32pm    
Man, I bet there is no problem with the tree formation code it works just fine and by the way the
function will return the root address of the tree to the root pointer variable. Plz focus on delete function.
OriginalGriff 19-Jan-14 13:44pm    
You sure?
What keyword do you use to set a return value in a c/c++/c# function?
Tell us what data you provided and we can help you :) Also maybe you applied some data that requires the "Select The Greatest Node in left Subtree Policy" to delete a node, But in your code only "Select The least Node in Right Subtree Policy" is provided and that's the point!!!!!
 
Share this answer
 
v4
Comments
Stefan_Lang 11-Sep-14 8:32am    
You probably meant "Policy", not "politics".

Then again maybe you did mean politics, after all the question was posted 8 months ago, and some the functions still don't return any value... ;-)
MrSirwan 12-Sep-14 4:38am    
you got me ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900