Tree & Graph Data Structures
Expert Answer & Key Takeaways
Comprehensive guide to Non-Linear data structures: Trees (Binary Tree, BST, Traversals) and Graphs (Representations, BFS, DFS, Shortest Path).
Non-Linear Data Structures: Tree and Graph
While arrays, linked lists, stacks, and queues are linear data structures where data is arranged sequentially, Trees and Graphs are non-linear data structures. They organize data hierarchically or interconnectedly, making them ideal for representing complex relationships like family trees, organizational charts, or computer networks.
1. Tree Data Structure
A Tree is a hierarchical data structure consisting of nodes connected by edges. It is a specialized graph that is acyclic (contains no cycles) and connected.
1.1 Basic Terminology
- Node: An entity that contains a key or value and pointers to its child nodes.
- Root: The topmost node in the tree hierarchy. It has no parent.
- Edge: The link connecting two nodes.
- Parent: Any node (except root) has one edge upward to a node called parent.
- Child: The node below a given node connected by its edge downward.
- Leaf (External Node): A node that has no children.
- Internal Node: A node with at least one child.
- Depth of a Node: The number of edges from the root to the node.
- Height of a Node: The number of edges on the longest path from the node to a leaf.
- Height of a Tree: The height of its root node.
1.2 Binary Tree
A Binary Tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child.
Types of Binary Trees:
- Full Binary Tree (Strict Binary Tree): Every node has either 0 or 2 children.
- Complete Binary Tree: All levels are completely filled except possibly the last level, and the last level has all keys as left as possible (like a Binary Heap).
- Perfect Binary Tree: All internal nodes have two children, and all leaf nodes are at the same level.
- Balanced Binary Tree: The left and right subtrees of every node differ in height by no more than 1 (e.g., AVL tree, Red-Black tree).
- Degenerate (Pathological) Tree: Every internal node has only one child. It essentially behaves like a linked list.
1.3 Binary Search Tree (BST)
A Binary Search Tree is a node-based binary tree with the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- The left and right subtrees each must also be a binary search tree. Operations on BST:
- Search: O(log N) average, O(N) worst case.
- Insertion: O(log N) average, O(N) worst case.
- Deletion: O(log N) average, O(N) worst case.
1.4 Tree Traversals
Traversal is the process of visiting every node in the tree exactly once.
A. Depth-First Traversals (DFS):
- Inorder Traversal (Left, Root, Right):
- Gives nodes in non-decreasing (sorted) order for a BST.
- Algorithm: Traverse Left Subtree -> Visit Root -> Traverse Right Subtree.
- Preorder Traversal (Root, Left, Right):
- Used to create a copy of the tree or to get prefix expressions.
- Algorithm: Visit Root -> Traverse Left Subtree -> Traverse Right Subtree.
- Postorder Traversal (Left, Right, Root):
- Used to delete the tree or to get postfix expressions.
- Algorithm: Traverse Left Subtree -> Traverse Right Subtree -> Visit Root. B. Breadth-First Traversal (BFS):
- Level Order Traversal:
- Visits nodes level by level from left to right. Implemented using a Queue.
2. Graph Data Structure
A Graph is a non-linear data structure consisting of a finite set of vertices (or nodes) and a set of edges connecting these vertices. Mathematically, G = (V, E).
2.1 Basic Terminology
- Vertex: A node in the graph.
- Edge: A connection between two vertices.
- Directed Graph (Digraph): Edges have a direction (indicated by arrows).
- Undirected Graph: Edges do not have a direction. The connection is two-way.
- Weighted Graph: Edges have weights or costs associated with them.
- Degree: The number of edges connected to a vertex.
- In-Degree (Digraph): Number of edges coming into a vertex.
- Out-Degree (Digraph): Number of edges going out of a vertex.
- Path: A sequence of vertices connected by edges.
- Cycle: A path that starts and ends at the same vertex.
2.2 Graph Representation
Graphs are commonly represented in two ways in memory:
1. Adjacency Matrix:
- A 2D array of size V x V where V is the number of vertices.
adj[i][j] = 1indicates an edge from vertex i to vertex j. (In a weighted graph, it holds the weight).- Pros: Easy to implement, O(1) to check if an edge exists.
- Cons: Consumes O(V^2) space, which is inefficient for sparse graphs. 2. Adjacency List:
- An array of Linked Lists (or dynamic arrays). The size of the array is equal to the number of vertices.
adj[i]contains a list of all vertices connected to vertex i.- Pros: Saves space O(V + E). Efficient for sparse graphs.
- Cons: O(V) time to check if an edge exists between two specific vertices.
2.3 Graph Traversals
- Breadth-First Search (BFS):
- Traverses level by level.
- Uses a Queue data structure.
- Useful for finding the shortest path on unweighted graphs.
- Time Complexity: O(V + E).
- Depth-First Search (DFS):
- Traverses as deep as possible before backtracking.
- Uses a Stack data structure (or recursion).
- Useful for topological sorting, cycle detection, and solving puzzles like mazes.
- Time Complexity: O(V + E).
2.4 Important Graph Algorithms (Overview)
- Minimum Spanning Tree (MST): A subset of edges that connects all vertices with the minimum possible total edge weight, without any cycles.
- Prim's Algorithm: Builds MST by picking the smallest edge connected to the growing MST.
- Kruskal's Algorithm: Builds MST by sorting all edges and adding them one by one if they don't form a cycle (using Disjoint Set).
- Shortest Path Algorithms:
- Dijkstra's Algorithm: Finds the shortest path from a source vertex to all other vertices in a weighted graph (with non-negative weights).
- Bellman-Ford Algorithm: Finds shortest path from source and can handle negative weight edges.
- Floyd-Warshall Algorithm: Finds shortest paths between all pairs of vertices.
Course4All Editorial Board
Verified ExpertSubject Matter Experts
Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.