Get a Binary Parent TreeIn 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: - - 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.
- 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.
- 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.
ImplementationOutput A step-by-step explanation of the code - The code begins by including all the necessary headers, which are significant for the input and output operations performed by the code.
- 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.
- 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.
- 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.
- 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.
- Finally, the program's primary function returns 0, which signifies the successful execution of the program.
- 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 A step-by-step explanation of the code - 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.
- 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.
- 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.
- 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.
- Finally, the program's primary function returns 0, which signifies the successful execution of the program.
- 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.
|