Find the parent node of a node in binary search tree

牧云@^-^@ 提交于 2019-12-03 17:05:34

It appears that 45 does not have a left node, it is NULL, so checking pRoot->pleft->value == value is undefined behavior.

             30
            /  \
         15     45
        /   \     \
      7      17    69
                     \
                      80

So you need to do a check, something like:

Node*  BST::searchforparentnode(Node* pRoot, int value)
{
    if(pRoot->pleft == NULL && pRoot->pright == NULL)
       return NULL;

    if( (pRoot->pleft != NULL && pRoot->pleft->value == value)
        || (pRoot->pright != NULL && pRoot->pright->value == value))
       return pRoot;

    if(pRoot->value > value)
       return searchforparentnode(pRoot->pleft,value);

    if(pRoot->value < value)
       return searchforparentnode(pRoot->pright,value);
}

However, another thing to check for is if pRoot itself is NULL:

Node*  BST::searchforparentnode(Node* pRoot, int value)
{
    if (pRoot == NULL)
       return NULL;

    if(pRoot->pleft == NULL && pRoot->pright == NULL)
       return NULL;

    if( (pRoot->pleft != NULL && pRoot->pleft->value == value)
        || (pRoot->pright != NULL && pRoot->pright->value == value))
       return pRoot;

    if(pRoot->value > value)
       return searchforparentnode(pRoot->pleft,value);

    if(pRoot->value < value)
       return searchforparentnode(pRoot->pright,value);
}

Your Problem is:

if(pRoot->pleft->value == value || pRoot->pright->value == value)

because before you checked if left AND right is null. In the section mentioned above, one could be null

You maybe wanna Change your code to something like this:

Node*  BST::searchforparentnode(Node* pRoot, int value)
{
   if(pRoot->pleft == NULL && pRoot->pright == NULL)
    return NULL;

   //Added check 
   if(pRoot->pleft && pRoot->pleft->value == value)
    return pRoot;

   //Added check
   if(pRoot->pright && pRoot->pright->value == value)
    return pRoot;

   //Check also needed here
   if(pRoot->pleft && pRoot->value > value)
    return searchforparentnode(pRoot->pleft,value);

   //Check also needed here
   if(pRoot->pright && pRoot->value < value)
    return searchforparentnode(pRoot->pright,value);

}

IMHO: You could also create a pointer to the parent node in each node. This could Speed up certain things a lot!

public Node nodeParent(Node root, Node n){
        if(root==null){
            return null;
        }
        Node p=root;
        if(p.left==null && p.right==null ){
            return null;
        }
        if(p.left!=null && p.key>n.key){
            p=p.left;
            if(p.left.key==n.key){
            return p;
        }
        }
        if(p.right!=null && p.key<n.key){
            p=p.right;
            if(p.right.key==n.key){
            return p;
        }
        }
        if(p.left!=null && p.right!=null ){
            if(p.key<n.key){
                p=p.right;
            }else{
                p=p.left;
            }
        }
        return p;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!