Member-only story
Binary tree fundamentals #1 — Concept, height calculation and Java implementation
Table of Contents
2 min readNov 19, 2023
· What is a Binary Tree?
· Binary Tree implementation
· Height of a Binary Tree
∘ Calculating the height of a binary tree
What is a Binary Tree?
A binary tree is a tree data structure, in which each parent node has at most 2 children, referred to as the left child and the right child. Since each node has at most 2 children, we can also say that the degree of each node is at most 2.
Binary Tree implementation
class Node {
int data;
Node left;
Node right;
Node (int x) {
data = x;
left = null;
right = null;
}
}
class BinaryTree {
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(40);
root.right.left = new Node(50);
root.right.right = new Node(80);
// For an empty tree, root = null.
}
}