Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the parent node of a node in binary search tree

So I want to find the parent node of a Node in a binary tree. Suppose that I input 30,15,17,45,69,80,7 in the tree through a text file.

The tree should be

                 30
             15       45
          7      17       69
                               80

And here is my code :

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

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

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

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

}

In this case i'm not consider if the user input the value of the Root node.

Thing is, when I input 15,17,7, all the value in the left branch of Root node, it came out ok. But when i want to find parent Node of the values from the right branch (69,80) EXCEPT for 45, the program stop running.

Any idea about what caused this error guys? Thanks for reading.

like image 895
Hòa Ngô Avatar asked Jun 09 '15 15:06

Hòa Ngô


People also ask

What is parent node in binary tree?

A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root. Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent.

How do you find the parent of a node in an array in binary tree?

For any given node index N , the children of that node will always be in locations 2N+1 and 2(N+1) in the same array. Therefore, The parent of any node N > 0 in such an array will always be at index (N-1)/2 .

What is the parent of a node?

Any subnode of a given node is called a child node, and the given node, in turn, is the child's parent.

How many parents can a node have in a binary tree?

Each node except the root node has exactly one parent node. The root node has no parent. Two nodes are siblings if they have the same parent.


1 Answers

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);
}
like image 112
AndyG Avatar answered Nov 15 '22 03:11

AndyG