findNode in binary search tree

Posted by Weadadada Awda on Stack Overflow See other posts from Stack Overflow or by Weadadada Awda
Published on 2012-12-11T10:55:56Z Indexed on 2012/12/11 11:03 UTC
Read the original article Hit count: 194

Filed under:
|
|

Does this look right? I mean I am trying to implement the delete function.

Node* BST::findNode(int tofind) { Node* node = new Node;

node = root;
while (node != NULL) {
    if (node->val == tofind) {
        return node;
    } else if (tofind < node->val) {
        node = node->left;
    } else {
        node = node->right;
    }
}

}

Here is the delete, it's not even close to done but, void BST::Delete(int todelete) {

// bool found = false;

Node* toDelete = new Node();
toDelete=findNode(todelete);
if(toDelete->val!=NULL) {
        cout << toDelete->val << endl;

}

}

This causes a segmentation fault just running that, any ideas?

© Stack Overflow or respective owner

Related posts about search

Related posts about binary