Binary Tree in Data Structure — Types and Implementation
A Binary Tree is a hierarchical data structure in which each node has at most two children, commonly referred to as the left child and the right child.
Types of Binary Trees
| Type | Description |
|---|---|
| Full Binary Tree | Every node has either 0 or 2 children. |
| Complete Binary Tree | All levels are completely filled except possibly the last, which is filled left to right. |
| Perfect Binary Tree | All internal nodes have two children and all leaves are at the same level. |
| Balanced Binary Tree | The height of the left and right subtrees of any node differ by at most one. |
| Degenerate (Skewed) Tree | Each parent node has only one child, resembling a linked list. |
Linked Node Implementation
class Node:
def __init__(self, value):
self.val = value
self.left = None
self.right = None
class BinaryTree:
def __init__(self):
self.root = None
def insert_level_order(self, values):
# builds a complete binary tree from a list, left to right
if not values:
return
self.root = Node(values[0])
queue = [self.root]
i = 1
while queue and i < len(values):
node = queue.pop(0)
if i < len(values):
node.left = Node(values[i]); queue.append(node.left); i += 1
if i < len(values):
node.right = Node(values[i]); queue.append(node.right); i += 1
Complexity
| Operation | Time |
|---|---|
| Search/Insert/Delete (unbalanced) | O(n) |
| Search/Insert/Delete (balanced) | O(log n) |
Applications
- Expression trees for evaluating mathematical expressions.
- Binary Search Trees for fast lookup.
- Huffman coding trees for data compression.
Traversals Every Binary Tree Needs
A tree is only useful if you can visit its nodes in a meaningful order. The four standard traversals are:
- Inorder (left, root, right) — on a binary search tree this visits values in sorted order.
- Preorder (root, left, right) — handy for copying a tree or saving its structure.
- Postorder (left, right, root) — used when children must be processed before the parent, such as deleting a tree or evaluating an expression tree.
- Level order (breadth-first) — visits the tree row by row using a queue, the same idea the sample builder uses.
def inorder(node):
if node:
inorder(node.left)
print(node.val)
inorder(node.right)
Array vs Linked Representation
The linked version with Node objects is flexible and natural for sparse or changing trees. For a complete binary tree, though, an array is leaner: store the root at index 0, and for a node at index i its children sit at 2i+1 and 2i+2, with its parent at (i-1)//2. This is exactly how a binary heap is stored. The trade-off: arrays waste space for sparse trees because missing nodes still occupy slots.
Reading Tree Height and Why It Matters
Height is the number of edges on the longest path from the root to a leaf, and it drives performance. A balanced tree of n nodes has height around log n, so search, insert and delete cost O(log n). A skewed tree degenerates into a chain of height n, dragging those operations to O(n) — no better than a linked list. This is the whole reason self-balancing trees like AVL and red-black trees exist.
def height(node):
if not node:
return -1
return 1 + max(height(node.left), height(node.right))
A frequent mistake is confusing a "complete" tree with a "full" one: full means every node has 0 or 2 children, while complete means every level is filled except possibly the last, left to right. The distinction matters because the array representation and heaps rely specifically on the complete shape, where no index gaps appear.
Two more terms are worth fixing in your mind. The depth of a node is the number of edges from the root down to it, while height measures upward from the deepest leaf — the root's height equals the tree's height. And a leaf is any node with no children, where traversals bottom out. Keeping these words precise avoids confusion when reading algorithm descriptions that assume them.
Takeaway: choose the representation that fits your tree's shape, pick the traversal that matches your task, and keep the tree balanced — its height, not its node count, decides how fast it runs.