| | |
deleting a Binary search tree
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
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
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
im trying to write a delete function for a BST and this is what i have
C++ Syntax (Toggle Plain Text)
bool BST::delNode(Key key) { BST_Node* node; BST_Node* p; if(findKey(key,root)) { if(root == NULL) return false; if(key == root->getKey()) { delete key.data; return true; } else if(root->getLeft() == NULL) { p = root->getRight(); free(root); return false; } else if(root->getRight() == NULL) { p = root->getLeft(); free(root); return false; } else { node = root->getRight(); p = root->getRight(); while (p->getLeft()) { p = p->getLeft(); } p->getLeft() = root->getLeft(); free(root); return true; } if(root->getKey() < key) { root->getLeft() = delNode(key); return true; } else { root->getRight() = delNode(key); return true; } delete key.data; } else return false; }
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)
class BST_Node { private: Key key; // key holds the data BST_Node* left; // ptr to left subtree BST_Node* right; // ptr to right subtree public: // Managers BST_Node(); BST_Node(Key key); // Construct given key-data BST_Node(BST_Node& node); // Copy Constructor ~BST_Node(); // Destruct node // Operators BST_Node& operator= (BST_Node& node); // Assignment // Accessors Key getKey() {return key;}; // get Key Data BST_Node* getLeft() {return left;}; // get root of left subtree BST_Node* getRight() {return right;}; // get root of right subtree void setLeft(BST_Node* node); void setRight(BST_Node* node); }; BST_Node::BST_Node() { key.data = NULL; left = NULL; right = NULL; } BST_Node::BST_Node(Key key) { cout << "enter key" << endl; cin >> key.data; } BST_Node::BST_Node(BST_Node& node) { right = node.right; left = node.left; //key = node.key; } BST_Node::~BST_Node() { delete left; delete right; } BST_Node& BST_Node::operator= (BST_Node& node) { right = node.right; left = node.left; //key = node.key; return *this; } void BST_Node::setLeft(BST_Node* node) { this->left = node; } void BST_Node::setRight(BST_Node* node) { this->right = node; }
•
•
•
•
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);
C++ Syntax (Toggle Plain Text)
delNode(key);
•
•
•
•
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.
But if you choose to do this there are three cases
*Voted best profile in the world*
![]() |
Similar Threads
- Reading a file into a binary search tree (C++)
- Binary Search Tree (C++)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Binary search tree removal (C++)
- Insertion in a binary search tree (C++)
Other Threads in the C++ Forum
- Previous Thread: invalid initialisation of reference of type 'double& '
- Next Thread: my #include <string> wont make "string" bold
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






