CampusFlow
AlgorithmsTree Algorithms

đŸŒŗ Tree Algorithms

Master tree traversals, binary search trees, and advanced tree operations. Trees are fundamental in databases, compilers, and AI.

Overview

â„šī¸

What are Tree Algorithms?

Trees are hierarchical data structures with a root node and child subtrees. Tree algorithms handle traversal (visiting all nodes), search (finding specific nodes), and modification (insert/delete). Binary Search Trees enable O(log n) operations.

Key Tree Algorithms

Inorder Traversal

Left → Root → Right. Produces sorted order for BSTs.

Time: O(n)Space: O(h)

Preorder Traversal

Root → Left → Right. Used for copying trees.

Time: O(n)Space: O(h)

Postorder Traversal

Left → Right → Root. Used for deleting trees.

Time: O(n)Space: O(h)

Level Order Traversal

BFS on tree, visit nodes level by level.

Time: O(n)Space: O(n)

BST Search

Efficient search in a binary search tree.

Time: O(h)Space: O(1)

Lowest Common Ancestor

Find LCA of two nodes in a binary tree.

Time: O(n)Space: O(h)

Interactive Visualizer

Tree Visualization

đŸŒŗ

Inorder Traversal Tree Visualizer

Watch nodes being traversed with highlights showing the current node and direction.

Deep Dive: Inorder Traversal

Time Complexity

Time: O(n)

Space Complexity

Space: O(h)

Method

Recursive/Iterative
💡

Algorithm Approach

Visit left subtree, then root, then right subtree. For BSTs, this visits nodes in ascending order. Iterative version uses explicit stack.

python

def inorder(node):
    if not node: return
    inorder(node.left)
    print(node.val)
    inorder(node.right)
1

Choose Traversal

Select order based on what you need: inorder for sorted, preorder for copy, postorder for deletion.

2

Handle Base Case

If node is null, return (do nothing). This is the foundation of recursive traversal.

3

Recursive Calls

Make recursive calls in the correct order with left and right children.

4

Process Node

Print, collect, or transform node value at the appropriate point in the recursion.

Real-World Applications

File Systems

Hierarchical directory/folder structures are trees

HTML DOM

Browser represents HTML as a tree (Document Object Model)

Databases

B-Trees and B+ Trees index database records efficiently

Compilers

Abstract Syntax Trees (ASTs) represent program structure

Network Routing

Spanning trees prevent loops in network broadcasts

AI Decision Trees

Decision trees and random forests for classification

Interview Questions

What's the difference between a binary tree and BST?

Binary tree: each node has at most 2 children, no ordering. BST: for each node, left subtree has smaller values, right subtree has larger values. BST enables O(h) search.

How do you check if a binary tree is balanced?

Check that height difference between left and right subtrees ≤ 1 for every node. Can be done with a recursive function that returns height and checks balance simultaneously (postorder).

What is tree serialization?

Serialization converts a tree to a string (e.g., for network transmission). Preorder traversal with null markers works well. Deserialization reconstructs the tree from the string.

Knowledge Check

1. Which traversal produces sorted order in BST?

2. Height of a balanced BST with n nodes is:

3. Which traversal is used to delete a tree?

4. What traversal mimics BFS?

5. What is the space complexity of level order traversal?

What's Next?