Javatpoint Logo
Javatpoint Logo

Get the Level of a Given Key in a Binary Tree

The level of a key in a specific binary tree generally refers to the distance that is present between the root of the binary tree node and the node that is containing the desired key. It is very important and noteworthy how many steps are required to reach the desired key. Before we move further to the implementation details of the tree, let us carefully review the major concepts which are related to the binary tree.

This pattern will make sure that we have a solid foundation and understanding of the techniques that will help us in getting the level of a given key in the binary tree. Once we are familiar with all the interesting concepts and algorithms we can move forward and learn the examples that will help us understand the various algorithms, and code more effectively. They are very valuable when it comes to storing and managing data and processing the data.

We are given a binary tree and a key; with that, we have to write the level of the binary tree as well. Extracting the level of the binary tree can sometimes be cumbersome as it involves creating many different functions which carry their type of mechanism and, at last, provide the result.

Implementation

Output

Get the Level of a Given Key in a Binary Tree

A step-by-step explanation of the code

  1. The code begins by declaring the necessary header files to help us carry out the input/output operations.
  2. Next, in the program, we define a structure called 'Node' that contains three members: a data value and a pointer to the left and right child.
  3. Next, we create a function called 'getlevelUtil', which is a helper function that helps find out the node level in the binary tree.
  4. Within this function, the node first checks out the basic condition, whether the node is NULL or not; if not, it returns 0.
  5. In case the current node's data matches up with the data value that we have been assigned, then it signifies that the node has been found, and the current level of that particular node is returned.
  6. If, in case, the current node's data doesn't appear to be the same or match with it, the function calls itself, and that too with an incremented level, and the value is returned.
  7. If the 'downlevel' value is non-zero, it signifies that the node was found in the left subtree, and if the value is still zero, then it signifies that the desired node was not found in the left subtree.
  8. Finally, in this step function returns the value, which will either be the level of the node or 0 if it doesn't match.
  9. The function 'newNode' is a new function that creates a new node that can store various new values and help in memory allocation.
  10. In the primary function of the program, a binary tree is constructed.
  11. Then a loop is executed from count 1 to 5.
  12. The 'get level' function is called to print the level of the binary tree.

Example 2)

Output

Get the Level of a Given Key in a Binary Tree

A step-by-step explanation of the code

  1. The code begins by declaring the necessary header files to help us carry out the input/output operations.
  2. Next, in the program, we define a structure called 'typedef' that contains the following three members: a data value and a pointer to the left and right child.
  3. Next, we create a function called 'getlevelUtil', which is a helper function that helps find out the node level in the binary tree.
  4. Within this function, the node first checks out the basic condition, whether the node is NULL or not; if not, it returns 0.
  5. In case the current node's data matches up with the data value that we have been assigned, then it signifies that the node has been found, and the current level of that particular node is returned.
  6. If, in case, the current node's data doesn't appear to be the same or match with it, the function simply calls itself, and that too with an incremented level, and the value is returned.
  7. If the 'downlevel' value is non-zero, it signifies that the node was found in the left subtree, and if the value is still zero, then it signifies that the desired node was not found in the left subtree.
  8. Finally, in this step function returns the value, which will either be the level of the node or 0 if it doesn't match.
  9. The function 'newNode' is a new function that creates a new node that can store various new values and help in memory allocation.
  10. In the primary function of the program, a binary tree is constructed.
  11. Then a loop is executed from count 1 to 5.
  12. The 'get level' function is called to print the level of the binary tree.

Example 3)

Output

Get the Level of a Given Key in a Binary Tree

A step-by-step explanation of the code

  1. The code begins with a class named 'Node' and a class named 'Binary tree', which contains the program's root and main logic.
  2. Next, in the program, we define a structure called 'Node' that contains three members: a data value and a pointer to the left and right child.
  3. Next, we create a function called 'getlevelUtil', which is a helper function that helps find out the node level in the binary tree.
  4. Within this function, the node first checks out the basic condition, whether the node is NULL or not; if not, it returns 0.
  5. If the current node's data matches up with the data value that we have been assigned, it signifies that the node has been found and that the current level of that particular node is returned.
  6. If, in case, the current node's data doesn't appear to be the same or match with it, the function simply calls itself, and that too with an incremented level, and the value is returned.
  7. If the 'downlevel' value is non-zero, it signifies that the node was found in the left subtree, and if the value is still zero, then it signifies that the desired node was not found in the left subtree.
  8. Finally, in this step function returns the value, which will either be the level of the node or 0 if it doesn't match.
  9. The function 'newNode' is a new function that creates a new node that can store various new values and help in memory allocation.
  10. In the primary function of the program, a binary tree is constructed.
  11. Then a loop is executed from count 1 to 5.
  12. The 'get level' function is called to print the level of the binary tree.






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