943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6117
  • C++ RSS
May 19th, 2006
0

deleting a Binary search tree

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
tyczj is offline Offline
91 posts
since Mar 2005
May 19th, 2006
0

Re: deleting a Binary search tree

Try
C++ Syntax (Toggle Plain Text)
  1. p->setLeft( root->getLeft() );
and likewise.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
May 19th, 2006
0

Re: deleting a Binary search tree

that fixed the first error but the others i cant do that to
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
tyczj is offline Offline
91 posts
since Mar 2005
May 19th, 2006
0

Re: deleting a Binary search tree

Quote 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
C++ Syntax (Toggle Plain Text)
  1. delNode(key);
only and see. I didnt do any testing nor did I read the whole program, so no guarantees.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
May 19th, 2006
0

Re: deleting a Binary search tree

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
tyczj is offline Offline
91 posts
since Mar 2005
May 19th, 2006
1

Re: deleting a Binary search tree

Quote 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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: invalid initialisation of reference of type 'double& '
Next Thread in C++ Forum Timeline: my #include <string> wont make "string" bold





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC