Skip to main content
Binary Trees & Graphs
CHAPTER 01 Beginner

Introduction to Trees and Graphs

Updated: May 17, 2026
5 min read

# CHAPTER 1

Introduction to Trees and Graphs

1. Introduction

In computer science, data structures are ways to store and organize data so that it can be used efficiently. While arrays, linked lists, stacks, and queues are excellent for storing linear data (where elements follow a single sequence), they fall short when representing complex, real-world relationships. Enter Trees and Graphs. These are non-linear data structures. Trees represent hierarchical relationships (like a corporate organizational chart or a computer file system), while Graphs represent network relationships (like a social network, road maps, or the internet itself).

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define what Trees and Graphs are in computer science.
  • Differentiate between linear and non-linear data structures.
  • Understand the real-world applications of Trees and Graphs.
  • Conceptualize hierarchical vs. network structures.

3. What are Trees?

A Tree is a non-linear data structure that simulates a hierarchical tree structure, with a root value and subtrees of children, represented as a set of linked nodes.
  • It starts with a single top-level node called the Root.
  • It branches down to child nodes.
  • A tree strictly prohibits cycles (loops). You cannot traverse from a child back to its parent using a downward path.

Visual Example:

text
12345
        CEO (Root)
       /   \
   VP(A)   VP(B)
   /  \      \
Mgr1 Mgr2   Mgr3

4. What are Graphs?

A Graph is a non-linear data structure consisting of Vertices (or nodes) and Edges (lines connecting the nodes). Unlike trees, graphs do not have a strict "root" or hierarchical rule, and they actively permit cycles (loops) and disconnected components.
  • A Tree is actually just a special, restricted type of Graph!

Visual Example:

text
123
A --- B
|     |
C --- D

*(In this graph, A is connected to B and C. You can travel from A to B, B to D, D to C, and C back to A, creating a cycle).*

5. Hierarchical vs. Network Structures

  • Hierarchical (Trees): Represents a "One-to-Many" relationship. Data flows from top to bottom.
  • *Analogy:* A family tree. You have parents, who have children.
  • Network (Graphs): Represents a "Many-to-Many" relationship. Data flows in any direction.
  • *Analogy:* A city's subway system. Multiple stations (Vertices) connect to multiple other stations via train tracks (Edges).

6. Why Trees and Graphs Matter

Linear structures like Arrays take $O(N)$ time to search in the worst case. If you have 1 Billion users, searching an array takes 1 Billion operations. By structuring data into a balanced Binary Search Tree, that same search drops to $O(\log N)$ time—taking a maximum of just 30 operations! Graphs allow us to build GPS navigation systems (finding the shortest path) and recommendation engines (finding mutual friends).

7. Real-World Applications

Applications of Trees:
  • File Systems: Your computer's folder structure (Root: C:\ -> Users -> Documents).
  • Databases: B-Trees are used to index database records for ultra-fast retrieval.
  • Compilers: Abstract Syntax Trees (AST) parse the code you write.
  • HTML DOM: The Document Object Model of every webpage is a Tree.

Applications of Graphs:

  • Social Networks: Facebook and LinkedIn use graphs (Users are Nodes, Friendships are Edges).
  • Mapping/GPS: Google Maps uses graphs to find the shortest route between cities.
  • Network Routing: The internet uses graphs to route packets from your router to a server.

8. Mini Project: Organizational Chart

Let's define a simple Node structure in code for an organizational chart tree.

C++:

cpp
12345678910111213141516171819202122232425262728
#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Node {
    string name;
    string title;
    vector<Node*> reports;
    
    Node(string n, string t) {
        name = n;
        title = t;
    }
};

int main() {
    Node* ceo = new Node("Alice", "CEO");
    Node* vp1 = new Node("Bob", "VP Engineering");
    Node* vp2 = new Node("Charlie", "VP Sales");
    
    ceo->reports.push_back(vp1);
    ceo->reports.push_back(vp2);
    
    cout << ceo->title << " has " << ceo->reports.size() << " direct reports." << endl;
    return 0;
}

Java:

java
123456789101112131415161718192021222324
import java.util.ArrayList;
import java.util.List;

class Node {
    String name;
    String title;
    List<Node> reports;

    public Node(String name, String title) {
        this.name = name;
        this.title = title;
        this.reports = new ArrayList<>();
    }
}

public class Main {
    public static void main(String[] args) {
        Node ceo = new Node("Alice", "CEO");
        Node vp1 = new Node("Bob", "VP Engineering");
        
        ceo.reports.add(vp1);
        System.out.println(ceo.title + " has " + ceo.reports.size() + " direct report.");
    }
}

Python:

python
1234567891011
class Node:
    def __init__(self, name, title):
        self.name = name
        self.title = title
        self.reports = []

ceo = Node("Alice", "CEO")
vp1 = Node("Bob", "VP Engineering")
ceo.reports.append(vp1)

print(f"{ceo.title} has {len(ceo.reports)} direct report(s).")

9. Complexity Analysis

  • Time Complexity (Creation): $O(1)$ to create a single node.
  • Space Complexity: $O(N)$ where $N$ is the number of nodes in the structure, as each node requires memory allocation.

10. Common Mistakes

  • Confusing Trees with Graphs: Remember, all Trees are Graphs, but not all Graphs are Trees. If a structure has a cycle (a loop), it is mathematically a Graph, NOT a Tree.

11. Best Practices

  • When dealing with hierarchical data, always default to a Tree structure rather than trying to force it into a 2D Array or Linked List.

12. Exercises

  1. 1. Draw a visual tree representing the folder structure of your computer's Desktop.
  1. 2. Draw a visual graph representing your 5 closest friends and who is friends with whom.

13. MCQs with Answers

Question 1

What type of data structure is a Tree?

Question 2

Which real-world system is best represented by a Graph?

Q3. True or False: A Tree is legally allowed to contain cycles (loops). a) True b) False Answer: b) False. The defining characteristic of a Tree is that it is an acyclic graph. If it has a loop, it ceases to be a Tree.
Question 4

What is the top-most node of a Tree called?

Question 5

In an array of 1 Billion items, linear search takes $O(N)$ time. What time complexity can a balanced tree achieve?

Question 6

What does a "Node" in a Graph generally represent?

Question 7

What does an "Edge" in a Graph represent?

Question 8

Which structure represents a "Many-to-Many" relationship?

Question 9

Which structure represents a "One-to-Many" hierarchical relationship?

Question 10

Why are non-linear data structures preferred over arrays for massive datasets?

14. Interview Questions

  • Q: Explain the fundamental difference between a Tree and a Graph.
  • Q: If you were designing a recommendation engine like Netflix, would you use a Tree or a Graph, and why?

15. Summary

Trees and Graphs are the backbone of advanced computer science. While Arrays handle flat, sequential data, Trees organize data into efficient hierarchies, and Graphs map the chaotic, interconnected networks of the real world. Mastering these structures is the key to passing technical interviews and building highly scalable software.

16. Next Chapter Recommendation

Now that we understand the big picture, we must dive deep into the specific vocabulary of Trees. In Chapter 2: Tree Terminology and Fundamentals, we will define Roots, Leaves, Edges, Height, and Depth.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·