Find height of binary tree

Find Height of binary tree

Here given code implementation process.

/*
  C Program 
+ Find height of binary tree 
*/
#include<stdio.h>

#include<stdlib.h>
 //structure of Binary Tree node
struct Node {
  int data;
  struct Node *left, *right;
};
//Create a binary tree nodes and node fields (data,pointer) 
//And returning the reference of newly nodes

struct Node *insert(int data) {
  //create dynamic memory to new binary tree node
  struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
  if (new_node != NULL) {
    //set data and pointer values
    new_node->data = data;
    new_node->left = NULL; //Initially node left-pointer is NULL
    new_node->right = NULL; //Initially node right-pointer is NULL
  } else {
    printf("Memory Overflow\n");
    exit(0); //Terminate program execution
  }
  //return reference
  return new_node;

}

//Find height of given binary tree
int height(struct Node *root) {

  if (root != NULL) {
    int a = height(root->left);
    int b = height(root->right);

    //returning a height of largest subtree

    if (a > b) {
      return a + 1;
    } else {
      return b + 1;
    }
  } else {
    return 0;
  }
}
int main() {

  struct Node *root = NULL;
  /* Make A Binary Tree
  -----------------------
          1
         /  \
        2    3
       /    /  \
      4    5    6
       \       /
        7     8
               \
                9                    
  */
  //Add element
  root = insert(1);
  root->left = insert(2);
  root->right = insert(3);
  root->right->right = insert(6);
  root->right->left = insert(5);
  root->left->left = insert(4);
  root->left->left->right = insert(7);
  root->right->right->left = insert(8);

  root->right->right->left->right = insert(9);


  printf("%d\n", height(root));
  return 0;
}

Output

5
/*
  C++ Program
  Find height of binary tree 
*/
#include<iostream>

using namespace std;
class Node {
public:
  int data;
  Node *left;
  Node *right;
  Node(int value) {
    this->data = value;
    this->right = NULL;
    this->left = NULL;
  }
};
class BinaryTree {
  public:
  Node *root;
  BinaryTree() {
    this->root = NULL;
  }
  int height(Node *head) {
    if (head != NULL) {
      int a = this->height(head->left);
      int b = this->height(head->right);
      if (a > b) {
        return a + 1;
      } else {
        return b + 1;
      }
    } else {
      return 0;
    }
  }

};

int main() {
  BinaryTree obj;
  obj.root = new Node(1);
  obj.root->left = new Node(2);
  obj.root->right = new Node(3);
  obj.root->right->right = new Node(6);
  obj.root->right->left = new Node(5);
  obj.root->left->left = new Node(4);
  obj.root->left->left->right = new Node(7);
  obj.root->right->right->left = new Node(8);

  obj.root->right->right->left->right = new Node(9);
  cout << obj.height(obj.root) << "\n";
  return 0;
}

Output

5
/*
  Java Program
  Find height of binary tree 
*/

class Node
{

  public int data;
  public Node left;
  public Node right;
  public Node(int value)
  {
    this.data=value;
    this.right=null;
    this.left=null;
  }
}
public class BinaryTree {

  public Node root;
 
  public BinaryTree()
  {
    root = null;

  }
 
  //Find height of given binary tree
  public int height(Node head)
  {

    if(head!=null)
    {
      int a = height(head.left);
      int b = height(head.right);

      //returning a height of largest subtree

      if(a>b)
      {
        return a+1;
      }
      else
      {
        return b+1;
      }
    }else
    {
      return 0;
    }
  }
  public static void main(String[] args) {

    BinaryTree obj = new BinaryTree();
    /*Create Binary Tree
    -----------------------
            1
           /  \
          2    3
         /    /  \
        4    5    6
         \       /
          7     8
                 \
                  9                    
    */      
    //Add element
    obj.root         =new Node(1);
    obj.root.left        =new Node(2);
    obj.root.right       =new Node(3);
    obj.root.right.right =new Node(6);
    obj.root.right.left  =new Node(5);
    obj.root.left.left   =new Node(4);
    obj.root.left.left.right =new Node(7);
    obj.root.right.right.left =new Node(8);

    obj.root.right.right.left.right =new Node(9);


    System.out.print(obj.height(obj.root)+"\n");
  }
}

Output

5
/*
  C# Program
  Find height of binary tree 
*/
using System;
public class Node
{

  public int data;
  public Node left;
  public Node right;
  public Node(int value)
  {
    this.data=value;
    this.right=null;
    this.left=null;
  }
}
public class BinaryTree {

  public Node root;

  public BinaryTree()
  {
    root = null;

  }

  //Find height of given binary tree
  public int height(Node head)
  {

    if(head!=null)
    {
      int a = height(head.left);
      int b = height(head.right);

      //returning a height of largest subtree

      if(a>b)
      {
        return a+1;
      }
      else
      {
        return b+1;
      }
    }else
    {
      return 0;
    }
  }
  public static void Main(String[] args) {

    BinaryTree obj = new BinaryTree();
    /*Create Binary Tree
    -----------------------
            1
           /  \
          2    3
         /    /  \
        4    5    6
         \       /
          7     8
                 \
                  9                    
    */      
    //Add element
    obj.root         =new Node(1);
    obj.root.left        =new Node(2);
    obj.root.right       =new Node(3);
    obj.root.right.right =new Node(6);
    obj.root.right.left  =new Node(5);
    obj.root.left.left   =new Node(4);
    obj.root.left.left.right =new Node(7);
    obj.root.right.right.left =new Node(8);

    obj.root.right.right.left.right =new Node(9);


    Console.Write(obj.height(obj.root)+"\n");
  }
}

Output

5
#Python3 Program
#Find height of binary tree 
class Node :
  def __init__(self, value) :
    self.data = value;
    self.right = None;
    self.left = None;
  

class BinaryTree :
 
  def __init__(self) :
    self.root = None;
  
  def height(self, head) :
    if (head != None) :
      a = self.height(head.left);
      b = self.height(head.right);
      if (a > b) :
        return a + 1;
      else :
        return b + 1;
      
    else :
      return 0;
    
  
def main() :
  obj = BinaryTree();
  obj.root = Node(1);
  obj.root.left = Node(2);
  obj.root.right = Node(3);
  obj.root.right.right = Node(6);
  obj.root.right.left = Node(5);
  obj.root.left.left = Node(4);
  obj.root.left.left.right = Node(7);
  obj.root.right.right.left = Node(8);
  obj.root.right.right.left.right = Node(9);
  print(obj.height(obj.root));
  


if __name__ == "__main__":
  main()

Output

5
<?php
/*
  Php Program
  Find height of binary tree 
*/
class Node {
  public $data;
  public $left;
  public $right;

  function __construct($value) {
    $this->data = $value;
    $this->right = null;
    $this->left = null;
  }
}
class BinaryTree {
  public $root;

  function __construct() {
    $this->root = null;
  }
  public  function height($head) {
    if ($head != null) {
      $a = $this->height($head->left);
      $b = $this->height($head->right);
      if ($a > $b) {
        return $a + 1;
      } else {
        return $b + 1;
      }
    } else {
      return 0;
    }
  }

}
function main() {
  $obj = new BinaryTree();
  $obj->root = new Node(1);
  $obj->root->left = new Node(2);
  $obj->root->right = new Node(3);
  $obj->root->right->right = new Node(6);
  $obj->root->right->left = new Node(5);
  $obj->root->left->left = new Node(4);
  $obj->root->left->left->right = new Node(7);
  $obj->root->right->right->left = new Node(8);
  $obj->root->right->right->left->right = new Node(9);
  echo ($obj->height($obj->root) ."\n");
}
main();

Output

5
#Ruby Program
#Find height of binary tree 
class Node 
  attr_reader :data, :left, :right
  attr_accessor :data, :left, :right
  def initialize(value) 
    self.data = value
    self.right = nil
    self.left = nil
  end
end

class BinaryTree 
  attr_reader :root
  attr_accessor :root
  def initialize() 
    @root = nil
  end
  def height(head) 
    if (head != nil) 
      a = self.height(head.left)
      b = self.height(head.right)
      if (a > b) 
        return a + 1
      else 
        return b + 1
      end
    else 
      return 0
    end
  end
end

def main() 
  obj = BinaryTree.new()
  obj.root = Node.new(1)
  obj.root.left = Node.new(2)
  obj.root.right = Node.new(3)
  obj.root.right.right = Node.new(6)
  obj.root.right.left = Node.new(5)
  obj.root.left.left = Node.new(4)
  obj.root.left.left.right = Node.new(7)
  obj.root.right.right.left = Node.new(8)
  obj.root.right.right.left.right = Node.new(9)
  print(obj.height(obj.root))
end
main() 

Output

5
/*
  Node Js Program
  Find height of binary tree 
*/
class Node {
  
  constructor(value) {
    this.data = value;
    this.right = null;
    this.left = null;
  }
}
class BinaryTree {
  
  constructor() {
    this.root = null;
  }
  height(head) {
    if (head != null) {
      var a = this.height(head.left);
      var b = this.height(head.right);
      if (a > b) {
        return a + 1;
      } else {
        return b + 1;
      }
    } else {
      return 0;
    }
  }
}
function main() {
  var obj = new BinaryTree();
  obj.root = new Node(1);
  obj.root.left = new Node(2);
  obj.root.right = new Node(3);
  obj.root.right.right = new Node(6);
  obj.root.right.left = new Node(5);
  obj.root.left.left = new Node(4);
  obj.root.left.left.right = new Node(7);
  obj.root.right.right.left = new Node(8);
  obj.root.right.right.left.right = new Node(9);
  process.stdout.write(obj.height(obj.root) + "\n");
}
main();

Output

5
/*
  Swift 4 Program
  Find height of binary tree 
*/

class Node {
  var data: Int;
  var left: Node? ;
  var right: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.right = nil;
    self.left = nil;
  }
}
class BinaryTree {
  var root: Node? ;
  init() {
    self.root = nil;
  }
  func height(_ head: Node? ) -> Int {
    if (head != nil) {
      let a: Int = self.height(head!.left);
      let b: Int = self.height(head!.right);
      if (a > b) {
        return a + 1;
      } else {
        return b + 1;
      }
    } else {
      return 0;
    }
  }
}
func main() {
  let obj: BinaryTree? = BinaryTree();
  obj!.root = Node(1);
  obj!.root!.left = Node(2);
  obj!.root!.right = Node(3);
  obj!.root!.right!.right = Node(6);
  obj!.root!.right!.left = Node(5);
  obj!.root!.left!.left = Node(4);
  obj!.root!.left!.left!.right = Node(7);
  obj!.root!.right!.right!.left = Node(8);
  obj!.root!.right!.right!.left!.right = Node(9);
  print(obj!.height(obj!.root));
}
main();

Output

5


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved