🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we will walk you through the process of writing a Java program to find the height of a binary tree.
1. Defining the Binary Tree Node:
The foundation of a binary tree is its nodes. Here's a simple class definition for a binary tree node:class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}2. Implementing the Binary Tree Class:
The binary tree class contains our methods for adding nodes and calculating the height.
class BinaryTree {
Node root;
// Compute the height of the binary tree
int height(Node node) {
if (node == null) {
return 0;
} else {
// Compute the height of each subtree
int leftHeight = height(node.left);
int rightHeight = height(node.right);
// Return the larger one
return Math.max(leftHeight, rightHeight) + 1;
}
}
int getHeight() {
return height(root);
}
}3. Testing the Program:
Now that our binary tree and its methods are set up, let's populate it with some nodes and compute its height.
public class Main {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.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);
System.out.println("Height of the binary tree is: " + tree.getHeight());
}
}Output:
Height of the binary tree is: 3Step-by-Step Explanation:
Node Definition: We begin by defining a simple Node class that has integer data and two child nodes (left and right).
Calculating Height: The height() method is a recursive function. If the node is null, it returns 0. Otherwise, it computes the height of the left and right subtrees and returns the greater of the two, incremented by 1.
Building the Tree: In our test, we constructed a binary tree of height 3, where the root node has data 1, its left child has data 2, and its right child has data 3. Furthermore, the left child of the root node (with data 2) has two children: 4 (left) and 5 (right).
Output: The output shows that our program correctly computed the height of the tree as 3.
Conclusion
Computing the height of a binary tree is a foundational concept in data structures. In this post, we broke down the process of defining a binary tree in Java and computing its height. Such an understanding is crucial when delving deeper into tree algorithms and operations, and it serves as a building block for more advanced topics like balanced trees and graph traversals.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment