A Beginner's Guide to Trees

EJ JungEJ Jung
3 min read

1. Introduction to Trees

A Tree is a widely used data structure in computer science that simulates a hierarchical tree structure with a set of connected nodes. Unlike linear data structures such as arrays or linked lists, trees allow efficient representation of hierarchical relationships like file systems, organizational charts, or HTML document structures.


2. Basic Terminology

  • Root: The topmost node in a tree.

  • Parent and Child: A node that has descendants is a parent, and the connected nodes below it are children.

  • Leaf: A node with no children.

  • Height of Tree: The longest path from the root to a leaf.

  • Depth of Node: The distance from the root to that node.


3. Types of Trees

3.1 Binary Tree

A tree where each node has at most two children (left and right).

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

# Example binary tree creation
root = Node(1)
root.left = Node(2)
root.right = Node(3)

3.2 Binary Search Tree (BST)

A binary tree where the left child contains values smaller than the parent, and the right child contains larger values.

class BST:
    def __init__(self):
        self.root = None

    def insert(self, root, value):
        if root is None:
            return Node(value)
        if value < root.value:
            root.left = self.insert(root.left, value)
        else:
            root.right = self.insert(root.right, value)
        return root

3.3 Balanced Trees

Examples include AVL Trees and Red-Black Trees, which maintain balance to guarantee logarithmic search time.

3.4 N-ary Tree

A tree where nodes can have up to N children. Useful in representing hierarchical data like file directories.


4. Tree Traversals

Traversals are methods to visit all nodes of a tree.

4.1 Depth-First Search (DFS)

  • Pre-order (Root โ†’ Left โ†’ Right)
def preorder(node):
    if node:
        print(node.value)
        preorder(node.left)
        preorder(node.right)
  • In-order (Left โ†’ Root โ†’ Right)
def inorder(node):
    if node:
        inorder(node.left)
        print(node.value)
        inorder(node.right)
  • Post-order (Left โ†’ Right โ†’ Root)
def postorder(node):
    if node:
        postorder(node.left)
        postorder(node.right)
        print(node.value)

4.2 Breadth-First Search (BFS)

Also known as Level Order Traversal.

from collections import deque

def bfs(root):
    if not root:
        return
    q = deque([root])
    while q:
        node = q.popleft()
        print(node.value)
        if node.left:
            q.append(node.left)
        if node.right:
            q.append(node.right)

5. Applications of Trees

  • Binary Search Trees: Fast searching and sorting.

  • Heaps: Priority queues.

  • Tries: Efficient word searching in dictionaries or autocomplete systems.

  • Parse Trees: Used in compilers for syntax analysis.

  • File Systems: Directory structures are tree-based.


6. Common Pitfalls

  • Forgetting to check for null references when traversing.

  • Poor balancing in BSTs can lead to O(n) performance.

  • Misunderstanding recursive traversal order.


โœจ Conclusion

Trees are one of the most important data structures in computer science. They are versatile, efficient for hierarchical data, and form the backbone of many algorithms and systems like search engines, compilers, and databases. Mastery of tree concepts, traversals, and balanced tree structures is crucial for coding interviews and real-world software development.


๐Ÿ”— References

0
Subscribe to my newsletter

Read articles from EJ Jung directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

EJ Jung
EJ Jung