How to Measure the Height of a Tree - Practical Primate

Blog

C++

#include <iostream>#include <queue>using namespace std;// This approach counts the number of nodes from root to the// leaf to calculate the height of the tree.// Defining the structure of a Node.class Node {public:int data;Node* left;Node* right;};// Helper function to create a newnode.// Input: Data for the newnode.// Return: Address of the newly created node.Node* createNode(int data){Node* newnode = new Node();newnode->data = data;newnode->left = NULL;newnode->right = NULL;return newnode;}// Function to calculate the height of given Binary Tree.// Input: Address of the root node of Binary Tree.// Return: Height of Binary Tree as a integer. This includes// the number of nodes from root to the leaf.int calculateHeight(Node* root){queue<Node*> nodesInLevel;int height = 0;int nodeCount = 0; // Calculate  number of nodes in a level.Node* currentNode; // Pointer to store the address of a// node in the current level.if (root == NULL) {return 0;}nodesInLevel.push(root);while (!nodesInLevel.empty()) {// This while loop runs for every level and// increases the height by 1 in each iteration. If// the queue is empty then it implies that the last// level of tree has been parsed.height++;// Create another while loop which will insert all// the child nodes of the current level in the// queue.nodeCount = nodesInLevel.size();while (nodeCount–) {currentNode = nodesInLevel.front();// Check if the current nodes has left child and// insert it in the queue.if (currentNode->left != NULL) {nodesInLevel.push(currentNode->left);}// Check if the current nodes has right child// and insert it in the queue.if (currentNode->right != NULL) {nodesInLevel.push(currentNode->right);}// Once the children of the current node are// inserted. Delete the current node.nodesInLevel.pop();}}return height;}// Driver Function.int main(){// Creating a binary tree.Node* root = NULL;root = createNode(1);root->left = createNode(2);root->left->left = createNode(4);root->left->right = createNode(5);root->right = createNode(3);cout << “The height of the binary tree using iterative “”method is: ” << calculateHeight(root) << “.”;return 0;}

Video

What is the tallest tree in the world 2020?

Found in northern California, the tallest trees in the world are redwoods, also known as Sequoias. These trees can easily reach heights of 300 feet (91 meters). The tallest of them all, Hyperion, stands at 380 feet tall and is estimated to be between 600 and 800 years old.

Finding the Height Without Recursion (Breadth-First search approach)

Let us consider an example to understand the approach in a easier way. Consider the following Binary Tree, with 12 as the root node and 19, 16, 7 and 8 as the leaf node: 

2.) Node:

  • Node class representing the nodes in a binary tree.

What is edge in binary tree?

An edge is another fundamental part of a tree. An edge connects two nodes to show that there is a relationship between them. Every node (except the root) is connected by exactly one incoming edge from another node. Each node may have several outgoing edges.

Algorithm for Calculating Height of a Binary Tree

There are two methods to approach this problem statement. First Approach is based on Depth first seach using recursion, and the other method is based on Breadth first search using Level order traversal without using recursion. 

Algorithm to find the height of a binary tree

Now that we have found a way to find the height of the binary tree, we will formulate the algorithm for finding the height as follows.

  1. If we find an empty root node, we will say that the height of the tree is 0.
  2. Otherwise, we will find the height of the left subtree and right subtree recursively.
  3. After finding the height of the left subtree and right subtree, we will calculate their maximum height.
  4. We will add 1 to the maximum height. That will be the height of the binary tree.

Example 2: find height of right sub-tree, rooted at node A

  1. Go to node C
    • Find the height of left subtree
      • Go to Node F and apply Example 1 algorithm.
      • At Node F, Height of binary Tree = 2
    • Find the height of right subtree
      • Height of right subtree = 1
  2. Height of a binary binary tree will be
    • Height = max(height of left subtree, height of right subtree) + 1 ( Node C).
    • Height = max(2,1) + 1 = 3
Fig 3: Height of binary tree is 3

Fig 3: Height of binary tree is 3

Smartphone Method Very Easy

Technology to the rescue! Apps have changed the way we do so many things and it looks like estimating tree height is no exception. There are a few apps that will do the job and the ones I have tried seem to be fairly accurate. Until I have more experience with these apps I won’t recommend any particular one. I also won’t suggest they are an adequate replacement for more time tested methods, but perhaps they soon will be? Height measuring apps are certainly worth looking into though.

C#

// An iterative C# program to// find height of binary treeusing System;using System.Collections.Generic;// A binary tree nodeclass Node{public int data;public Node left, right;public Node(int item){data = item;left = right;}}public class BinaryTree{Node root;// Iterative method to find// height of Binary Treeint treeHeight(Node node){// Base Caseif (node == null)return 0;// Create an empty queue// for level order traversalQueue<Node> q = new Queue<Node>();// Enqueue Root and initialize heightq.Enqueue(node);int height = 0;while (1 == 1){// nodeCount (queue size) indicates// number of nodes at current level.int nodeCount = q.Count;if (nodeCount == 0)return height;height++;// Dequeue all nodes of current// level and Enqueue all// nodes of next levelwhile (nodeCount > 0){Node newnode = q.Peek();queue();if (newnode.left != null)q.Enqueue(newnode.left);if (newnode.right != null)q.Enqueue(newnode.right);nodeCount–;}}}// Driver codepublic static void Main(String []args){BinaryTree tree = new BinaryTree(); // Let us create a binary// tree shown in above diagramtree.root = new Node(1);tree.root.left = new Node(2);tree.root.right = new Node(3);tree.root.left.left = new Node(4);tree.root.left.right = new Node(5);Console.WriteLine(“Height of tree is ” +tree.treeHeight(tree.root));}}// This code has been contributed by 29AjayKumar

Trigonometry Method Medium

This is the ‘full version’ of the 45° triangle method above. With an angle of 45°, the math is easy and height equals distance. When you use an angle other than 45° (when you are forced to stand closer or further away) the math is a little more involved. For this method you will need clinometer and a way to measure your distance from the base of the tree.

Measuring the height of a tree using trigonometry.
Measuring the height of a tree using trigonometry.
  1. Measure the angle between the top of the tree and the ground from your eye. Ideally do this using a clinometer. Stand up straight on level ground. You need to be far enough back that you can easily see the top of tree. Put the clinometer to your eye and line it up with the top of the tree while holding the lever (lets the indicator swing freely). Release the lever to trap the indicator and then read off the indicated angle. If you don’t have a clinometer you can just mark the angle on a peice of card and measure using a protractor.
  2. Now that you have the angle, measure the distance between the tree and where you were standing in step one. Do this with a measuring tape or wheel for a more accurate result.
  3. Use the Tangent rule to calculate height of the tree (above eye level). tan(angle) = opposite/adjacent Where opposite is the height of the tree and adjacent is the distance between you and the tree. This is rearranged to:opposite = tan(angle) x adjacentor more simplyheight = tan(angle) x distance
  4. Add the height of your eyes to the calculated height of the tree. This gives you the total height of the tree. Remember you measured from your eye height and so you have to include how high your eyes are. See the diagram above for clarification.

For example, if I stood 20 m from a tree and the angle between the tree’s top and horizontal from my eye was 30 degrees, I would calculate the height as follows:

height = tan(angle) x distanceheight = tan(30) x 20height = 11.55 m

I am 1.75 m tall at my eyes and so

total height = 11.55 m + 1.75 mtotal height = 13.3 m

Warnings

  • These methods do not work well if the tree is on sloping ground. A professional surveyor uses electronic transits in these situations, but these are typically too expensive for home use.

    Thanks! Helpful 2 Not Helpful 1

  • While the angle of elevation methods, if used correctly, can calculate the correct height within 2-3 feet, there is abundant opportunity for human error, especially if the tree is angled or on a slope. If precision is absolutely necessary, consult your local extension service or other such agency for assistance.

    Thanks! Helpful Not Helpful 1

Advertisement

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Reader Success Stories

  • M. Hardin

Nov 14, 2017

    M. Hardin Nov 14, 2017

    “We are learning about the emergent layer of the rain forest in homeschool. My daughter wondered how people measure it to know the trees are 200 ft tall. We used each of these methods to measure a tree in our back yard to show her some ways this is known.” …” more

More reader stories Hide reader stories Share your story

Tags