A binary tree is a hierarchical data structure in which every node has at most two children, commonly called:
It is a special form of a tree data structure (a non-linear structure used to represent hierarchical relationships).
Learn JavaScript from the comfort of your home
(Root)
/ \
Left Right The basic storage unit. It contains:
The topmost node in the tree.
A node with no children.
A node with at least one child.
A node connected below another.
A tree within the main tree.
Length of the longest downward path to a leaf.
Distance from the root to that node.
Understanding these variations is important.
Apply to learn software engineering at Codeflare’s Academy
Every node has 0 or 2 children.
A
/ \
B C All levels are filled except possibly the last, and nodes are as far left as possible.
All interior nodes have 2 children and all leaves are at the same level.
For every node, the height of left and right subtrees differ by at most 1.
(Used for fast searching — e.g., AVL, Red-Black trees)
Every parent has only one child, making it behave like a linked list.
A Binary Tree is a general structure.
A Binary Search Tree (BST) is a special binary tree with the rule:
Left subtree values < Node value < Right subtree values This property enables fast search, insertion, deletion (O(log n) average).
To access all nodes, you traverse the tree. There are two main categories:
Example Tree:
1
/ \
2 3 Traversals:
Visit nodes level by level.
Level order → 1, 2, 3, 4, 5... Uses a queue internally.
Depends on tree type
(e.g., BST inserts smaller values left, larger right).
More complex — especially if the node has 2 children.
DFS or BFS.
Used in balancing algorithms.
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor(rootValue) {
this.root = new Node(rootValue);
}
// Pre-order traversal (Root → Left → Right)
preorder(node) {
if (node !== null) {
console.log(node.value);
this.preorder(node.left);
this.preorder(node.right);
}
}
}
const tree = new BinaryTree(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.preorder(tree.root);
// Output: 1, 2, 3 Fast searching, inserting, deleting.
Used for priority queues, implemented as complete binary trees.
Used in compilers and interpreters.
For file compression algorithms.
They provide:
Binary trees are one of the most fundamental data structures in computer science — understanding them makes advanced algorithms much easier.
Latest tech news and coding tips.
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…
Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…
Debugging your own code is hard enough — debugging someone else’s code is a whole…
Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…
Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…