Javatpoint Logo
Javatpoint Logo

Find Immediate Parents of Two Nodes in a Binary Tree

Suppose we are provided with a tree node, and the main task is that we have to find out the parent of the given binary tree node. To do the same, we have to traverse the whole tree and locate the parent nodes of the given tree node.

Here is an algorithm that will explain the following tasks: -

  1. We have to begin from the root of the binary tree.
  2. We have to traverse the tree using any tree traversal method and keep on traversing the tree until we reach the end of the tree or reach a desired node.
  3. When moving and exploring across the tree, we have to keep a reference to the parent node of the current node.
  4. If we find any given node, we have to find its parent node reference, and then we continue to find the next or another node.
  5. We have to keep repeating this process until we find out both nodes or reach the tree's end.
  6. We have to return the references of the parent node to the current node.

Implementation

Output:

Find Immediate Parents of Two Nodes in a Binary Tree

A step-by-step explanation of the code

  1. The code begins by including all the important headers, which are significant for the input and output operations performed by the code.
  2. In the next step of the code, we begin by defining a structure called 'Node,' which represents a node in the binary tree with the following members: data and a pointer to the left and right node. The structure also defines a constructor that evaluates the node's data values as a parameter and initializes the left and right pointers.
  3. After we are done defining the node's structure, in the next step, we declare a function named 'findParent.' This function also comprises three parameters: node, val, and parent.
  4. Now, when we come across this far, we know that there must be several different cases of this: -
    • Suppose that if the current node turns out to be NILL, it signifies that we have approached the end of the branch, and the function will return without doing anything.
    • If the data value of the current node matches the value we are looking for, it signifies that we have already found the target node. In this case, the function will print its parent's value and return the same.
    • And if the current node is not the target node, then the function makes recursive calls to find out the left and right children of the current node.
  5. In the primary function, a binary tree is created with the variable value set to 3, which determines the node for which we want to find the parents.
  6. Next, the 'findParent' function is called, and it comes up with the root of the binary tree and the value of the target node, and then we have to print its value.
  7. Finally, the program's primary function returns 0, which signifies the successful execution of the program.

Example 2)

Output:

Find Immediate Parents of Two Nodes in a Binary Tree

A step-by-step explanation of the code

  1. The code begins by defining a class for the program named TPT.
  2. Inside the TPT class, we create a nested static class named Node, which represents a node in the binary tree. Each data node present has an integer value and a pointer to the left and right nodes.
  3. After we are done defining the node's structure, in the next step, we declare a function named 'findParent.' This function also comprises three parameters: node, val, and parent.
  4. Now, when we come across this far, we know that there must be several different cases of this: -
    • Suppose that if the current node turns out to be NILL, it signifies that we have approached the end of the branch, and the function will return without doing anything.
    • If the data value of the current node matches the value we are looking for, it signifies that we have already found the target node. In this case, the function will print its parent's value and return the same.
    • And if, by any chance, the current node is not the target node, then the function makes recursive calls to find out the left and right children of the current node.
  5. Now, in the primary function of the binary tree, the root signifies the root node of the binary tree, and the corresponding nodes create the lines.
  6. The variable node represents the value 3, for which we had to find the parent node.
  7. Next, the 'findParent' function is called, and it comes up with the root of the binary tree and the value of the target node, and then we have to print its value.
  8. Finally, the output of the code will also be the node's parent.

Example 3)

Output:

Find Immediate Parents of Two Nodes in a Binary Tree

A step-by-step explanation of the code

  1. The code will begin by defining a class named 'Node,' present within the init method. The node method declared consists of an integer value and a pointer to the left and right nodes.
  2. After we are done defining the node's structure, in the next step, we declare a function named 'findParent.' This function also comprises three parameters: node, val, and parent.
  3. Now, when we come across this far, we know that there must be several different cases of this: -
    • Suppose that if the current node turns out to be NILL, it signifies that we have approached the end of the branch, and the function will return without doing anything.
    • If the data value of the current node matches the value we are looking for, it signifies that we have already found the target node. In this case, the function will print its parent's value and return the same.
    • And if, by any chance, the current node is not the target node, then the function makes recursive calls to find out the left and right children of the current node.
  4. Now, we commute to the program's primary function, which is responsible for the main execution of the program. The conditions stated in the primary function verify whether the main module is running the code.
  5. The binary tree is created in the primary function with the following structure.
  6. The variable node of the tree is set to be 3, representing the node whose parents we have to find out.
  7. The 'findParent' function is called in the program, and it will contain the root of the binary tree, the value of the node, and -1 as the initial parent value.
  8. The function recursively searches for the node's parent and then prints the parent node's value when it is found.

Example 4)

Output:

Find Immediate Parents of Two Nodes in a Binary Tree

A step-by-step explanation of the code

  1. The code begins by importing the primary namespace system, which provides the program's functionality's basic input/ output operations.
  2. The code begins by defining a class called TPT which holds the implementation of the binary tree and the main program.
  3. Inside the TPT is a nested class called Node, which basically represents the binary tree node. Each data node present has an integer value and a pointer to the left and right nodes.
  4. After defining the node's structure, we declare a function named 'findParent' in the next step. This function also comprises three parameters: node, val, and parent.
  5. Now, when we come across this far, we know that there must be several different cases of this: -
    • Suppose that if the current node turns out to be NILL, it signifies that we have approached the end of the branch, and the function will return without doing anything.
    • If the data value of the current node matches the value we are looking for, it signifies that we have already found the target node. In this case, the function will print its parent's value and return the same.
    • And if, by any chance, the current node is not the target node, then the function makes recursive calls to find out the left and right children of the current node.
  6. Now, in the primary function of the binary tree, the root signifies the root node of the binary tree, and the corresponding nodes create the lines.
  7. The variable node represents the value 3, for which we had to find the parent node.
  8. Next, the 'findParent' function is called, and it comes up with the root of the binary tree and the value of the target node, and then we have to print its value.
  9. Finally, the output of the code will also be the node's parent.

Example 5)

Output:

Find Immediate Parents of Two Nodes in a Binary Tree

A step-by-step explanation of the code

  1. The code begins by defining a class named 'Node' that will represent a binary tree node that represents a node in the binary tree. Each data node present has an integer value and a pointer to the left and right nodes.
  2. After we are done defining the node's structure, in the next step, we declare a function named 'findParent.' This function also comprises three parameters: node, val, and parent.
  3. Now, when we come across this far, we know that there must be several different cases of this: -
    • Suppose that if the current node turns out to be NILL, it signifies that we have approached the end of the branch, and the function will return without doing anything.
    • If the data value of the current node matches the value we are looking for, it signifies that we have already found the target node. In this case, the function will print its parent's value and return the same.
    • And if, by any chance, the current node is not the target node, then the function makes recursive calls to find out the left and right children of the current node.
  4. Now, in the primary function of the binary tree, the root signifies the root node of the binary tree, and the corresponding nodes create the lines.
  5. The variable node represents the value 3, for which we had to find the parent node.
  6. Next, the 'findParent' function is called, and it comes up with the root of the binary tree and the value of the target node, and then we have to print its value.
  7. Finally, the output of the code will also be the node's parent.

Conclusion

In conclusion, finding the immediate parents of two nodes in a binary tree is a task that requires traversing the tree and examining the relationship between the nodes. By using an appropriate algorithm, such as depth-first search or breadth-first search, we can very quickly locate the immediate parents of the provided nodes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA