Once you understand a few core patterns, most tree and graph questions start to look familiar. This article goes deeper into concepts, includes working code with proper problem statements, and shows how AI can actually speed up your preparation.
What is a Tree?
A tree is a hierarchical data structure made up of nodes connected by edges, where one node is designated as the root and every other node is reachable from it.Key Components
- Root Node: The topmost node from where the tree starts and has no parent.
- Parent Node: A node that has one or more child nodes connected below it.
- Child Node: A node that descends from another node.
- Leaf Node: A node that has no children and represents the end of a branch.
- A tree with N nodes always has N−1 edges.
- There are no cycles in a tree.
- Every node is connected, meaning the structure is always fully connected.
Types of Trees
- Binary Tree: Each node has at most two children, usually referred to as left and right.
- Binary Search Tree (BST): A special type of binary tree where the left subtree contains smaller values and the right subtree contains larger values.
- Balanced Tree: A tree where the height difference between subtrees is minimized to ensure efficient operations.
- Complete Binary Tree: All levels are completely filled except possibly the last, which is filled from left to right.
- Full Binary Tree: Every node has either 0 or 2 children.
What is a Graph?
A graph is a non-linear data structure consisting of vertices (nodes) and edges that connect pairs of nodes.Types of Graphs
- Directed Graph: Edges have direction, meaning one-way connections.
- Undirected Graph: Edges are bidirectional.
- Weighted Graph: Each edge has an associated weight or cost.
Tree vs Graph
| Feature | Tree | Graph |
|---|---|---|
| Structure | A tree follows a strict hierarchical structure with one root node. | A graph is a flexible structure without a fixed hierarchy. |
| Cycles | A tree never contains cycles. | A graph may contain cycles depending on its type. |
| Connectivity | A tree is always connected. | A graph may be disconnected. |
| Edge Count | A tree with N nodes always has exactly N−1 edges. | A graph can have any number of edges. |
Binary Tree Traversals
Traversal means visiting all nodes in a structured order. This is one of the most frequently asked topics in interviews.Types of Traversals
- Inorder Traversal: Visit left subtree, then root, then right subtree.
- Preorder Traversal: Visit root first, then left and right subtrees.
- Postorder Traversal: Visit left subtree, then right subtree, then root.
Problem Statement 1: Inorder Traversal of Binary Tree
Write a program to traverse a binary tree using inorder traversal and print all node values.
# Python program for inorder traversal
# of a binary tree
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def inorder(root):
if root is None:
return
inorder(root.left)
print(root.val, end=" ")
inorder(root.right)
# Creating a sample tree
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
inorder(root)
Output:
Explanation:4 2 1 3
The recursive function first processes the left subtree completely, then prints the current node, and finally processes the right subtree. This ensures sorted output in case of a BST.
Problem Statement 2: Preorder Traversal
Write a program to perform preorder traversal.
# Python program for preorder traversal
# of a binary tree
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def preorder(root):
if root is None:
return
print(root.val, end=" ")
preorder(root.left)
preorder(root.right)
# Example Tree
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
preorder(root)
Output:1 2 4 3
Explanation:
Preorder follows Root → Left → Right. It is useful for copying trees and serialization.
Problem Statement 3: Postorder Traversal
Write a program to perform postorder traversal.
# Python program for postorder traversal
# of a binary tree
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def postorder(root):
if root is None:
return
postorder(root.left)
postorder(root.right)
print(root.val, end=" ")
# Example Tree
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
postorder(root)
Output:4 2 3 1
Explanation:
Postorder follows Left → Right → Root. It is useful for deletion of trees.
Binary Search Tree (BST)
For every node:Left subtree Root Right subtree
0 Comments