deleting a Binary search tree

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

deleting a Binary search tree

 
0
  #1
May 19th, 2006
i dont even know if im on the right track with this one but here it goes.

im trying to write a delete function for a BST and this is what i have

  1. bool BST::delNode(Key key)
  2. {
  3. BST_Node* node;
  4. BST_Node* p;
  5.  
  6. if(findKey(key,root))
  7. {
  8. if(root == NULL)
  9. return false;
  10. if(key == root->getKey())
  11. {
  12. delete key.data;
  13. return true;
  14. }
  15.  
  16. else if(root->getLeft() == NULL)
  17. {
  18. p = root->getRight();
  19. free(root);
  20. return false;
  21. }
  22. else if(root->getRight() == NULL)
  23. {
  24. p = root->getLeft();
  25. free(root);
  26. return false;
  27. }
  28. else
  29. {
  30. node = root->getRight();
  31. p = root->getRight();
  32. while (p->getLeft())
  33. {
  34. p = p->getLeft();
  35. }
  36. p->getLeft() = root->getLeft();
  37. free(root);
  38. return true;
  39. }
  40. if(root->getKey() < key)
  41. {
  42. root->getLeft() = delNode(key);
  43. return true;
  44. }
  45. else
  46. {
  47. root->getRight() = delNode(key);
  48. return true;
  49. }
  50. delete key.data;
  51. }
  52. else
  53. return false;
  54. }

now i dunno if its even close to what it should be but i am getting 3 errors in it

error C2106: '=' : left operand must be l-value
p->getLeft() = root->getLeft();

error C2440: '=' : cannot convert from 'bool' to 'class BST_Node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
root->getLeft() = delNode(key);

error C2440: '=' : cannot convert from 'bool' to 'class BST_Node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
root->getRight() = delNode(key);

so if you could help me out with them that would be awsome

here is my BST class

  1. class BST_Node {
  2. private:
  3. Key key; // key holds the data
  4. BST_Node* left; // ptr to left subtree
  5. BST_Node* right; // ptr to right subtree
  6.  
  7. public:
  8. // Managers
  9. BST_Node();
  10. BST_Node(Key key); // Construct given key-data
  11. BST_Node(BST_Node& node); // Copy Constructor
  12. ~BST_Node(); // Destruct node
  13.  
  14. // Operators
  15. BST_Node& operator= (BST_Node& node); // Assignment
  16.  
  17. // Accessors
  18. Key getKey() {return key;}; // get Key Data
  19. BST_Node* getLeft() {return left;}; // get root of left subtree
  20. BST_Node* getRight() {return right;}; // get root of right subtree
  21. void setLeft(BST_Node* node);
  22. void setRight(BST_Node* node);
  23. };
  24.  
  25. BST_Node::BST_Node()
  26. {
  27. key.data = NULL;
  28. left = NULL;
  29. right = NULL;
  30. }
  31.  
  32. BST_Node::BST_Node(Key key)
  33. {
  34.  
  35. cout << "enter key" << endl;
  36. cin >> key.data;
  37.  
  38. }
  39.  
  40. BST_Node::BST_Node(BST_Node& node)
  41. {
  42. right = node.right;
  43. left = node.left;
  44. //key = node.key;
  45. }
  46.  
  47. BST_Node::~BST_Node()
  48. {
  49. delete left;
  50. delete right;
  51. }
  52.  
  53. BST_Node& BST_Node::operator= (BST_Node& node)
  54. {
  55. right = node.right;
  56. left = node.left;
  57. //key = node.key;
  58. return *this;
  59. }
  60.  
  61. void BST_Node::setLeft(BST_Node* node)
  62. {
  63. this->left = node;
  64. }
  65.  
  66. void BST_Node::setRight(BST_Node* node)
  67. {
  68. this->right = node;
  69. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: deleting a Binary search tree

 
0
  #2
May 19th, 2006
Try
  1. p->setLeft( root->getLeft() );
and likewise.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: deleting a Binary search tree

 
0
  #3
May 19th, 2006
that fixed the first error but the others i cant do that to
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: deleting a Binary search tree

 
0
  #4
May 19th, 2006
Originally Posted by tyczj
error C2440: '=' : cannot convert from 'bool' to 'class BST_Node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
root->getLeft() = delNode(key);

error C2440: '=' : cannot convert from 'bool' to 'class BST_Node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
root->getRight() = delNode(key);
BST_Node::getLeft() is a function. You can't assign values to functions. You have to pass values to them. I dont know what you are trying to do. I dont know if you are trying to delete the left node or what. In anycase getLeft does not take a bool value as parameters, which is the datatype returned by delNode. You should be able to work out what the problem is after the first solution was given. In anycase just try
  1. delNode(key);
only and see. I didnt do any testing nor did I read the whole program, so no guarantees.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: deleting a Binary search tree

 
0
  #5
May 19th, 2006
what i am trying to do with the root->getRight() and root->getLeft() is basically just delete the left or right node of the tree depending on what the key is.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: deleting a Binary search tree

 
1
  #6
May 19th, 2006
Originally Posted by tyczj
what i am trying to do with the root->getRight() and root->getLeft() is basically just delete the left or right node of the tree depending on what the key is.
You do realise that to delete a node in a binary search tree, whilst still retaining the inherent structure is a non-trivial exercise.

But if you choose to do this there are three cases
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC