Tree Fundamentals & Terminology Resource - aloalgo

Tree Fundamentals & Terminology

A tree is a hierarchical data structure consisting of nodes connected by edges. Unlike arrays or linked lists, trees represent hierarchical relationships and enable efficient searching, insertion, and organization of data.

What Makes a Tree?

A tree has these properties:
  • One node is designated as the root
  • Every other node has exactly one parent
  • There are no cycles (no path leads back to itself)
  • All nodes are connected (there's a path from root to every node)

Essential Terminology

TermDefinition
RootThe topmost node with no parent
ParentThe node directly above a given node
ChildA node directly below a given node
SiblingNodes that share the same parent
LeafA node with no children
Internal NodeA node with at least one child (not a leaf)
AncestorAny node on the path from root to a given node
DescendantAny node reachable by going down from a given node
SubtreeA node and all its descendants

Height vs Depth vs Level

These terms are often confused. Here's the distinction:
TermDefinition
DepthDistance from root to node (root has depth 0)
HeightDistance from node to deepest leaf (leaves have height 0)
LevelSame as depth (sometimes 1-indexed)
Tree HeightHeight of the root node

Types of Trees

TypeDefinition
Binary TreeEach node has at most 2 children (left and right)
N-ary TreeEach node can have any number of children
Full Binary TreeEvery node has 0 or 2 children (never just 1)
Complete Binary TreeAll levels filled except last, which fills left to right
Perfect Binary TreeAll internal nodes have 2 children, all leaves at same level
Balanced Binary TreeHeight difference between subtrees is at most 1 for every node
Binary Search TreeLeft subtree values < node < right subtree values

Tree Properties & Formulas

PropertyFormula
Edges in any treen - 1 (for n nodes)
Min height (balanced)logâ‚‚(n)
Max height (skewed)n - 1
Nodes in perfect tree2^(h+1) - 1
Leaves in perfect tree2^h
Max nodes at level k2^k (for binary tree)

When to Use Trees

  • Hierarchical data: File systems, org charts, HTML DOM
  • Fast search: BST provides O(log n) lookup when balanced
  • Sorted data: BST maintains sorted order efficiently
  • Priority queues: Heaps (complete binary trees)
  • Prefix matching: Tries for string operations
  • Expression parsing: Abstract syntax trees

Common Interview Tips

  1. Clarify the tree type: Is it binary? BST? Balanced? This affects your approach.
  2. Ask about constraints: Can values be negative? Duplicates allowed? How large can the tree be?
  3. Consider edge cases: Empty tree (null root), single node, skewed tree (like a linked list).
  4. Know your terminology: Being precise about height vs depth shows mastery.
  5. Think recursively: Trees are recursive structures - most solutions are naturally recursive.
Was this helpful?
© 2026 aloalgo. All rights reserved.