Javatpoint Logo
Javatpoint Logo

Get All Descendants of the Binary Tree Element

The descendant in a binary tree typically refers to that particular node from which we can travel down and traverse the whole tree from that particular node. This node which we call the descendants is present below a specific node and can be its children, grandchildren, or great-grandchildren.

Now we will discuss some advantages of them in detail: -

  1. Descendants in the binary tree help us in the easy tree traversal because when we follow the path of the same, we can easily traverse down all the nodes of the tree efficiently.
  2. By using descendants in the tree, many operations, such as searching and traversing, become very easy and prominent with descendants.
  3. The presence of the descendants also contributes to the structural analysis of the tree.
  4. They play a significant role when it comes to implementing recursive operations, and many algorithms, such as the depth-first search algorithm, require the concept of descendants to efficiently carry out the operations.

We are allotted a binary tree and a key; with that, we have to find out all the ancestors by writing down a function that will print all the ancestors of the binary tree.

Implementation

Output

Get All Descendants of the Binary Tree Element

A step-by-step explanation of the code

  1. The code begins by declaring the necessary header files for the program's input-output operations.
  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. Next, we define a 'printAncestors' function which basically holds two parameters, a pointer to the root of the binary tree node and an integer value that will showcase the target or intended value.
  4. Inside this function, we have to check various cases, like first in the base, whether the root is NULL or not, which would indicate that the tree is empty.
  5. We declare a 'newNode' function that helps us create a new node and assign memory allocation to the values in the tree.
  6. The main function in the program is the entry point of any program, and a binary tree is constructed.
  7. The 'printAncestors' function will be called along with the root of the binary tree.
  8. The program returns 0.

Example 2)

Output

Get All Descendants of the Binary Tree Element

A step-by-step explanation of the code

  1. The code begins by declaring two significant classes, 'node' and 'binary tree'. The 'binary tree' contains the root of the binary tree.
  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. Next, we define a 'printAncestors' function which basically holds two parameters, a pointer to the root of the binary tree node and an integer value that will showcase the target or intended value.
  4. Inside this function, we have to check various cases, like first in the base, whether the root is NULL or not, which would indicate that the tree is empty.
  5. We declare a 'newNode' function that helps us create a new node and assign memory allocation to the values in the tree.
  6. The main function in the program is the entry point of any program, and a binary tree is constructed.
  7. The 'printAncestors' function will be called along with the root of the binary tree.
  8. The program returns 0.

Example 3)

Output

Get All Descendants of the Binary Tree Element

A step-by-step explanation of the code

  1. The code begins by declaring the necessary header files for the program's input-output operations.
  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. Next, we define a 'printAncestors' function with two parameters, a pointer to the root of the binary tree node and an integer value that will showcase the target or intended value.
  4. Inside this function, we have to check various cases, like first in the base, whether the root is NULL or not, which would indicate that the tree is empty.
  5. We declare a 'newNode' function that helps us create a new node and assign memory allocation to the values in the tree.
  6. The primary function in the program is the entry point of any program, and a binary tree is constructed.
  7. The 'printAncestors' function will be called along with the root of the binary tree.
  8. The program returns 0.

Example 4)

Output

Get All Descendants of the Binary Tree Element

A step-by-step explanation of the code

  1. 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 nodes.
  2. Next, we define a 'printAncestors' function with two parameters, a pointer to the root of the binary tree node and an integer value that will showcase the target or intended value.
  3. Inside this function, we have to check various cases, like first in the base, whether the root is NULL or not, which would indicate that the tree is empty.
  4. We declare a 'newNode' function that helps us create a new node and assign memory allocation to the values in the tree.
  5. The main function in the program is the entry point of any program, and a binary tree is constructed.
  6. The 'printAncestors' function will be called along with the root of the binary tree.
  7. The program returns 0.

Example 5)

Output

Get All Descendants of the Binary Tree Element

A step-by-step explanation of the code

  1. The code begins by declaring the necessary header files for the program's input-output operations.
  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. Next, we define a 'printAncestors' function with two parameters, a pointer to the root of the binary tree node and an integer value that will showcase the target or intended value.
  4. Inside this function, we have to check various cases, like first in the base, whether the root is NULL or not, which would indicate that the tree is empty.
  5. We declare a 'newNode' function that helps us create a new node and assign memory allocation to the values in the tree.
  6. The primary function in the program is the entry point of any program, and a binary tree is constructed.
  7. The 'printAncestors' function will be called along with the root of the binary tree.
  8. The program returns 0.






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