Javatpoint Logo
Javatpoint Logo

Get a Binary Parent Tree

In a binary tree, every single tree has a parent node. We have given a binary tree and a node, and the main task is to find the parent of the given binary tree node. When we talk about a binary tree, we know that every tree has a parent node which is the node that exists just above the current node. They can be very helpful when it comes to organizing certain sets of nodes.

There are many advantages of a parent binary tree, some of which are stated below: -

  1. The first advantage of a parent binary tree is easy navigation which means that we can easily traverse the whole tree both upwards as well as downwards and carry out several operations.
  2. When we are performing operations, we can easily modify or update the links that are present between the nodes, which makes the modifications in the tree very useful.
  3. In many tree traversal algorithms, such as in-order or post-order traversal, parent nodes make it easier to move between the nodes and acquire data.

Implementation

Output

Get a Parent Binary Tree

A step-by-step explanation of the code

  1. The code begins by including all the necessary 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.
  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. Inside the 'findParent', there are three main sections:
    • If the base condition has a value of NULL, then the base condition terminates itself and returns the value NULL.
    • Next, we have to check if the value we are looking for matches the current value, and if it does, it means we have to return that value and the parent node.
    • If it does not match the required value, the function makes recursive calls for the left and right subtree, and the parent value of the current node becomes the new parent to these recursive calls.
  5. In, 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.
  6. Finally, the program's primary function returns 0, which signifies the successful execution of the program.
  7. In the primary function, a binary tree is created with the variable value set to 3, determining the node for which we want to find the parents.

Example 2)

Output

Get a Parent Binary Tree

A step-by-step explanation of the code

  1. The code begins by defining a class that contains the central part of the program called 'TPT', and inside that node, 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.
  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. Inside the 'findParent', there are three main sections:
    • If the base condition has a value of NULL, then the base condition terminates itself and returns the value NULL.
    • Next, we have to check if the value we are looking for matches the current value, and if it does, it means we have to return that value and the parent node.
    • If it does not match the required value, the function makes recursive calls for the left and right subtree, and the parent value of the current node becomes the new parent to these recursive calls.
  4. 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.
  5. Finally, the program's primary function returns 0, which signifies the successful execution of the program.
  6. In the primary function, a binary tree is created with the variable value set to 3, determining the node for which we want to find the parents.






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