GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Aug-2009, 11:05
skiing64 skiing64 is offline
New Member
 
Join Date: Aug 2009
Posts: 1
skiing64 is on a distinguished road

Delete node in binary tree help


I have my program working except for deleting the node at the end and I am not sure my code is correct for the very bottom recursive call would someone help please. I am stuck.

CPP / C++ / C Code:
bool Delete_Node(node_ptr &p, keytype Target)
{
if(p==null)
	return false;
else if(p->datum.key<Target)
	return Delete_Node(p->right,Target);
else if(p->datum.key>Target)
	return Delete_Node(p->left,Target);
else
{
Node_ptr Temp=p;
if(p->left==null)
{
p=p->right;
delete temp;
return true;
}
else if(p->right==null)
{
p=p->left;
delete temp;
return true;
}
else
{
Temp = p->left;
while(Temp->right!=null){
Temp=Temp->right;
}
p->datum=Temp->datum;
delete Temp;
delete(p->left,Target);
Last edited by LuciWiz : 09-Aug-2009 at 12:34. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 09-Aug-2009, 22:48
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Delete node in binary tree help


What you have posted is a bit sketchy.
You should try to post a small example that works as good as you can get it to show what you are trying to do.
You say "deleting the node at the end " but your function looks like it is made to delete a specified node.
Well I need the exercise so I got something together based on what you provided and it seems that YES your second recursive call works , but after that the function was hard to understand and incomplete so I rewrote it the way I would go about it. Note that I also changed that functions header. So see if this is close to what you are trying to do and maybe it will be helpful to you:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

/* 
what is type: node_ptr ??? 
what is type: keytype  ???
I'll guess:
*/

typedef struct Keytype
{
  int key, value;
} KEYTYPE;   /* re: keytype; You should make defined datatype names STAND OUT */

typedef struct Node
{
  KEYTYPE  datum;
  struct Node *left, *right ;
} NODE_PTR;        /* node_ptr SAME  */

bool Delete_Node( NODE_PTR *p, int Target );

/********************************************************************/
int main(void)
{
  NODE_PTR *beg, *curr, *np;
  int i;
  /* create a quick linked list using the structures above */
  for(i = 0; i < 10; i++)
  {
    np = new NODE_PTR;
    if(i == 0)
    {
      np->left = np->right = NULL;
      beg = curr = np;
    }
    else
    {
      np->right = curr->right;
      np->left = curr;
      curr->right = np;
      curr = np;
    }
    curr->datum.key = i;
    cout << "np= " << np << "  np->datum.key= " <<  np->datum.key  << endl;
  }

  /* delete a selected node */
  if( Delete_Node( curr, 4))
    cout << "node 4 deleted?? \n";

  cout << "Don't forget to free all the allocated nodes. \n(or is that not needed in C++) \n";
  while(curr->left != NULL) /* rewind (you could check values along the way) */
    curr= curr->left;

  while(curr != NULL)
  {
    cout << "delete:  curr= " << curr
         << " curr->datum.key= " << curr->datum.key << endl;
    delete curr;
    curr = curr->right;
  }
  return 0;
}

/*******************************************************************
 bool Delete_Node(node_ptr &p, keytype Target)
 */
bool Delete_Node( NODE_PTR *p, int Target )
{
  cout << "In Delete_Node() at " << p->datum.key << endl;
  if(p == NULL)    // there is no 'null' defined but there is NULL pointer value
    return false;  // if you are returning here you do not need else if() 

  if( p->datum.key < Target)
    return Delete_Node( p->right, Target);

  if( p->datum.key > Target)
    return Delete_Node(p->left, Target);

  /* supposedly we are at the correct node so link up neighbors accordingly */
  if(p->left != NULL)
  {
    p->left->right = p->right;
  }
  else
  {
    p->right->left = NULL;
  }

  if(p->right != NULL)
  {
    p->right->left = p->left;
  }
  else    p->left->right = NULL;
  }

  return true;
}
Output:
Code:
forums> g++ -Wall -W -pedantic 090809_delete_node.cpp -o 090809_delete_node forums> ./090809_delete_node np= 0x8959008 np->datum.key= 0 np= 0x8959020 np->datum.key= 1 np= 0x8959038 np->datum.key= 2 np= 0x8959050 np->datum.key= 3 np= 0x8959068 np->datum.key= 4 np= 0x8959080 np->datum.key= 5 np= 0x8959098 np->datum.key= 6 np= 0x89590b0 np->datum.key= 7 np= 0x89590c8 np->datum.key= 8 np= 0x89590e0 np->datum.key= 9 In Delete_Node() at 9 In Delete_Node() at 8 In Delete_Node() at 7 In Delete_Node() at 6 In Delete_Node() at 5 In Delete_Node() at 4 node 4 deleted?? Don't forget to free all the allocated nodes. (or is that not needed in C++) delete: curr= 0x8959008 curr->datum.key= 0 delete: curr= 0x8959020 curr->datum.key= 1 delete: curr= 0x8959038 curr->datum.key= 2 delete: curr= 0x8959050 curr->datum.key= 3 delete: curr= 0x8959080 curr->datum.key= 5 delete: curr= 0x8959098 curr->datum.key= 6 delete: curr= 0x89590b0 curr->datum.key= 7 delete: curr= 0x89590c8 curr->datum.key= 8 delete: curr= 0x89590e0 curr->datum.key= 9
So again it does look like your recursive methods work ok , but are you required to use recursive?
That could get to be one heck of a stack!
It would seem to me that it would be quicker and less machine intensive to use
a while loop to find the node... like the one I use to get to the beginning of th list:
CPP / C++ / C Code:
 /********************************************************************/
bool Delete_Node( NODE_PTR *p, int Target )
{
  if(p == NULL)
    return false;

  while( p->datum.key < Target)
  {    
    cout << "In Delete_Node() at " << p->datum.key << endl;
    p = p->right;
  }
  while( p->datum.key > Target)
  {    
    cout << "In Delete_Node() at " << p->datum.key << endl;
    p = p->left;
  }
  cout << "In Delete_Node() at " << p->datum.key << endl;

  /* supposedly we are at the correct node so link up neighbors accordingly */
.... 
Anyhow, if that doesn't help , try again and provide more code to show what you are working with.
Howard++;
Last edited by Howard_L : 09-Aug-2009 at 23:50.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 20:50
help with header files and linked lists raikenyuu C Programming Language 3 28-Jan-2008 10:06
can anyone help me with my tree :) bioeng_mtm C++ Forum 5 22-Apr-2006 12:50
Strings tripping me up once more Elsydeon C++ Forum 5 04-Dec-2005 17:41
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 20:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:19.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.