Tree Graph
Tree Graph
Discrete Mathematics I
Introduction to trees and graphs
07/14/2024 CS201 1
Trees
07/14/2024 CS201 2
What is a tree?
• Trees are structures used to represent
hierarchical relationship
• Each tree consists of nodes and edges
• Each node represents an object
• Each edge represents the relationship between
two nodes.
nod
e edg
e
07/14/2024 CS201 3
Some applications of Trees
Organization Expression
Chart Tree
Preside
nt +
VP VP
Personn Marketin * 5
el g
3 2
Director Direct
Custom or
er Sales
Relation
07/14/2024 CS201 4
Terminology I
• For any two nodes u and v, if there is an edge
pointing from u to v, u is called the parent of v
while v is called the child of u. Such edge is
denoted as (u, v).
• In a tree, there is exactly one node without
parent, which is called the root. The nodes
without children are called leaves.
roo
t
u
u: parent of
v
v: child of u v
07/14/2024 CS201 leave
5
Terminology II
• In a tree, the nodes without children are
called leaves. Otherwise, they are called
internal nodes.
internal
nodes
leave
s
07/14/2024 CS201 6
Terminology III
• If two nodes have the same parent, they are
siblings.
• A node u is an ancestor of v if u is parent of v or
parent of parent of v or …
• A node v is a descendent of u if v is child of v or
child of child of v or …
u
T
A subtree of
T
v v
07/14/2024 CS201 8
Depth: The depth of a node is the number of
edges from the root to that node.
•Tree Depth: The maximum depth among all
nodes.
Level 2: 21
nodes
Level 3: 22
nodes
07/14/2024 CS201 13
Property of binary tree (II)
• Consider a binary tree T of height h. The
number of nodes of T ≤ 2h-1
07/14/2024 CS201 14
Property of binary tree (III)
• The minimum height of a binary tree with n
nodes is log(n+1)
07/14/2024 CS201 15
Types of Binary Tree
07/14/2024 CS201 21
An array-based representation
Array Representation is another way to represent binary
trees, especially useful when the tree is complete (all
levels are fully filled except possibly the last, which is
filled from left to right).
A C E D B F A C E
0 1 2 3 4 5
22
Reference Based
Representation
NULL: empty tree left element righ
t
You can code this with a
class of three fields:
Object element;
BinaryNode left;
BinaryNode right;
d d
b f
b f
a c
a c
07/14/2024 CS201 23
Balanced Binary Tree
It is a type of binary tree in which the
difference between the height of the left and
the right subtree for each node is either 0 or
1. In the following above, the root node
having a value 0 is unbalanced with a depth
of 2 units.
Tree Traversal
• Given a binary tree, we may like to do
some operations on all nodes in a binary
tree. For example, we may want to double
the value in every node in a binary tree.
• To do this, we need a traversal algorithm
which visits every node in the binary tree.
07/14/2024 CS201 27
Ways to traverse a tree
• There are three main ways to traverse a tree:
– Pre-order:
• (1) visit node, (2) recursively visit left subtree, (3) recursively
visit right subtree
– In-order:
• (1) recursively visit left subtree, (2) visit node, (3) recursively
right subtree
– Post-order:
• (1) recursively visit left subtree, (2) recursively visit right
subtree, (3) visit node
– Level-order:
• Traverse the nodes level by level
• In different situations, we use different traversal
algorithm.
07/14/2024 CS201 28
Examples for expression tree
• By pre-order, (prefix)
+*23/84
• By in-order, (infix)
+
2*3+8/4
• By post-order, (postfix) * /
23*84/+
2 3 8 4
• By level-order,
+*/2384
• Note 1: Infix is what we read!
• Note 2: Postfix expression can be computed
efficiently using stack
07/14/2024 CS201 29
Pre-order
Algorithm pre-order(BTree x)
If (x is not empty) {
print x.getItem(); // you can do other things!
pre-order(x.getLeftChild());
pre-order(x.getRightChild());
}
07/14/2024 CS201 30
Pre-order example
a b d c b c
d
07/14/2024 CS201 31
Time complexity of Pre-order
Traversal
• For every node x, we will call
pre-order(x) one time, which performs
O(1) operations.
• Thus, the total time = O(n).
07/14/2024 CS201 32
In-order and post-order
Algorithm in-order(BTree x)
If (x is not empty) {
in-order(x.getLeftChild());
print x.getItem(); // you can do other things!
in-order(x.getRightChild());
}
Algorithm post-order(BTree x)
If (x is not empty) {
post-order(x.getLeftChild());
post-order(x.getRightChild());
print x.getItem(); // you can do other things!
}
07/14/2024 CS201 33
In-order example
d b a c b c
07/14/2024 CS201 34
Post-order example
d b c a b c
d
07/14/2024 CS201 35
Time complexity for in-order and
post-order
• Similar to pre-order traversal, the time
complexity is O(n).
07/14/2024 CS201 36
General tree implementation
struct TreeNode A
{
Object element
TreeNode *firstChild B C D E
TreeNode *nextsibling
}
F G
because we do not know how many children a
node has in advance.
07/14/2024 CS201 37
Binary search tree
A Binary Search Tree (or BST) is a data structure
used in computer science for organizing and storing
data in a sorted manner.
Each node in a Binary Search Tree has at most
two children, a left child and a right child, with
the left child containing values less than the parent
node and the right child containing values greater
than the parent node.
This hierarchical structure allows for efficient
searching, insertion and deletion operations on
the data stored in the tree.
BST
Traversing
Inorder Traversal: 10 20 30 100
150 200 300 (sorted)
Preorder Traversal: 100 20 10
30 200 150 300
Postorder Traversal: 10 30 20
150 300 200 100
Search Operation
The algorithm depends on the property of BST that
if each left subtree has values below root and each
right subtree has values above the root.
If the value is below the root, we can say for sure
that the value is not in the right subtree;
we need to only search in the left subtree and if the
value is above the root, we can say for sure that the
value is not in the left subtree; we need to only
search in the right subtree.
Search
• Find value:4
construct a Binary Tree
Step 1:Insert 40
•Tree is empty. So 40 becomes the root.
•40
Step 2. Insert 20
•20 < 40 → insert to the left of 40.
40
• /
•20
Step 3. Insert 60
•60 > 40 → insert to the right of 40.
• 40
• / \
• 20 60
Creation
Step 4. Insert 10
•10 < 40 → go left
•10 < 20 → insert to the left of 20
• 40
• / \
• 20 60
• /
• 10
Creation
Insert 30
•30 < 40 → go left
•30 > 20 → insert to the right of 20.
• 40
• / \
• 20 60
• / \
• 10 30
Insertion
Given a BST, the task is to insert a new node
in this BST.
How to Insert
A new key is always inserted at the leaf by
maintaining the property of the binary search tree.
We start searching for a key from the root until we
hit a leaf node. Once a leaf node is found, the new
node is added as a child of the leaf node.
• Initilize the current node (say, currNode or node) with
root node
• Compare the key with the current node.
• Move left if the key is less than or equal to the current
node value.
• Move right if the key is greater than current node value.
• Repeat steps 2 and 3 until you reach a leaf node.
• Attach the new key as a left or right child based on the
comparison with the leaf node’s value.
insertion
insertion
insertion
Deletion
Process of Deletion:
Since deleting an element at any intermediary
position in the heap can be costly, so we can
simply replace the element to be deleted by the
last element and delete the last element of the
Heap.
• Replace the root or element to be deleted by
the last element.
• Delete the last element from the Heap.
• Since, the last element is now placed at the
position of the root node. So, it may not follow
the heap property. Therefore, heapify the last
node placed at the position of root.
Illustration:
Suppose the Heap is a Max-Heap as:
10
/ \
5 3
/ \
2 4
The element to be deleted is root, i.e. 10.
Process:
The last element is 4.
Step 1: Replace the last element with root, and
delete it.
4
/ \
5 3
/
2
• Step 2: Heapify root.
Final Heap:
5
/ \
4 3
/
2
Huffman Coding
07/14/2024 CS201 73
What is a graph?
• Graphs represent the relationships among data
items
• A graph G consists of
– a set V of nodes (vertices)
– a set E of edges: each edge connects two nodes
• Each node represents an item
• Each edge represents the relationship between
two items
nod
e
edg
e
07/14/2024 CS201 74
Examples of graphs
Molecular Computer
Structure Network
H Server Terminal
1 1
H C H
Terminal
H Server 2
2
Other examples: electrical and communication
networks, airline routes, flow chart, graphs for
planning projects
07/14/2024 CS201 75
Formal Definition of graph
• The set of nodes is denoted as V
• For any nodes u and v, if u and v are
connected by an edge, such edge is denoted
as (u, v) v
(u,
v)
u
• The set of edges is denoted as E
• A graph G is defined as a pair (V, E)
07/14/2024 CS201 76
Adjacent
• Two nodes u and v are said to be adjacent
if (u, v) ∈ E
v
(u,
v)
u
w
u and v are adjacent
v and w are not
adjacent
07/14/2024 CS201 77
Path and simple path
• A path from v1 to vk is a sequence of nodes
v1, v2, …, vk that are connected by edges
(v1, v2), (v2, v3), …, (vk-1, vk)
• A path is called a simple path if every node
appears at most once. v v
v
2 3
1
- v2, v3, v4, v2, v1 is a path
v v
- v2, v3, v4, v5 is a path,
also it is a simple path 4
5
07/14/2024 CS201 78
Cycle and simple cycle
• A cycle is a path that begins and ends at
the same node
• A simple cycle is a cycle if every node
appears at most once, except for the first
and the last nodes
v
v v
2
3
- v2, v3, v4, v5 , v3, v2 is a 1
cycle v v
- v2, v3, v4, v2 is a cycle, it is 5
4
also a simple cycle
07/14/2024 CS201 79
Connected graph
• A graph G is connected if there exists path
between every pair of distinct nodes;
otherwise, it is disconnected
v
v v
2
1 3
v v
5
This is a connected
4 graph because there exists
path between every pair of nodes
07/14/2024 CS201 80
Example of disconnected graph
v v v v
1 v 3 7 8
v 2 v
v v
5
4
6
9
This is a disconnected graph because there does
not exist path between some pair of nodes, says,
v1 and v7
07/14/2024 CS201 81
Connected component
• If a graph is disconnect, it can be partitioned into
a number of graphs such that each of them is
connected. Each such graph is called a
connected component.
v v v
v v
2 7 8
1 3
v v
v v
5
4 6
9
07/14/2024 CS201 82
Complete graph
• A graph is complete if each pair of distinct
nodes has an edge
Complete Complete
graph graph
with 3 nodes with 4 nodes
07/14/2024 CS201 83
Subgraph
• A subgraph of a graph G =(V, E) is a graph
H = (U, F) such that U ⊆ V and
F ⊆ E.
v v
v v v
2 2
1 3 3
v v v v
5 5
4 G 4 H
07/14/2024 CS201 84
Weighted graph
• If each edge in G is assigned a weight, it is
called a weighted graph
Housto
n
07/14/2024 CS201 85
Directed graph (digraph)
• All previous graphs are undirected graph
• If each edge in E has a direction, it is called a directed
edge
• A directed graph is a graph where every edges is a
directed edge
Chicago 100 New York
0
Directed
200 edge
350
0
0
Housto
07/14/2024 n CS201 86
More on directed graph
x y
07/14/2024 CS201 87
Multigraph
• A graph cannot have duplicate edges.
• Multigraph allows multiple edges and self
edge (or loop).
Self Multiple
edge edge
07/14/2024 CS201 88
Property of graph
• A undirected graph that is connected and
has no cycle is a tree.
• A tree with n nodes have exactly n-1
edges.
• A connected undirected graph with n
nodes must have at least n-1 edges.
07/14/2024 CS201 89
Implementing Graph
• Adjacency matrix
– Represent a graph using a two-dimensional
array
• Adjacency list
– Represent a graph using n linked lists where
n is the number of vertices
07/14/2024 CS201 90
Adjacency matrix for directed graph
Matrix[i][j] = 1 if 1 2 3 4 5
(vi, vj)∈E
0 if (vi, v1 v2 v3 v4 v5
vj)∉E 1 v1 0 1 0 0 0
v
v v 2 v 0 0 0 1 0
2 2
3
1 3 v3 0 1 0 1 0
v v 4 v4 0 0 0 0 0
5 5 v5 0 0 1 1 0
4 G
07/14/2024 CS201 91
Adjacency matrix for weighted
undirected graph
Matrix[i][j] = w(vi, vj) if (vi, vj)∈E or
(vj, vi)∈E
∞ otherwise 1 2 3 4 5
v v1 v2 v3 v4 v5
v 2 v
5 2 1 v1 ∞ 5 ∞ ∞ ∞
1 4 3 3
7 2 v2 5 ∞ 2 4 ∞
v
8 v
3 v3 0 2 ∞ 3 7
5
4 G 4 v4 ∞ 4 3 ∞ 8
5 v5 ∞ ∞ 7 8 ∞
07/14/2024 CS201 92
Adjacency list for directed graph
1 v1 → v2
v 2 v2 → v4
v v
2 3 v3 → v2 → v4
1 3
4 v4
v v
5 v5 → v3 → v4
5
4 G
07/14/2024 CS201 93
Adjacency list for weighted
undirected graph
v
v 2 v 1 v1 → v2(5)
5 2
4 3 2 v2 → v1(5) → v3(2) → v4(4)
1 3 7
3 v3 → v2(2) → v4(3) → v5(7)
v
8 v 4 v4 → v2(4) → v3(3) → v5(8)
5 5 v5 → v3(7) → v4(8)
4 G
07/14/2024 CS201 94
Adjacency Matrix
•Definition: A 2D array matrix[V][V] where
matrix[i][j] = 1 (or the weight, in weighted
graphs) if there is an edge from vertex i to vertex j.
Pros:
•Fast edge lookup: O(1) time to check if an edge
exists between two nodes.
•Simple implementation.
•Good for dense graphs (i.e., many edges).
07/14/2024 CS201 95
Cons:
Cons:
•Slower edge lookup: O(degree) time to check if an edge exists
between two nodes.
•Slightly more complex to implement.
Use When:
•The graph is sparse (few edges).
•You need to traverse neighbors frequently (like in DFS/BFS).
•Space efficiency is important.
Problems related to Graph
• Graph Traversal
• Topological Sort
• Spanning Tree
• Minimum Spanning Tree
• Shortest Path
07/14/2024 CS201 98
Graph Traversal Algorithm
• To traverse a tree, we use tree traversal
algorithms like pre-order, in-order, and post-
order to visit all the nodes in a tree
• Similarly, graph traversal algorithm tries to visit
all the nodes it can reach.
• If a graph is disconnected, a graph traversal that
begins at a node v will visit only a subset of
nodes, that is, the connected component
containing v.
07/14/2024 CS201 99
Two basic traversal algorithms
• Two basic graph traversal algorithms:
– Depth-first-search (DFS)
• After visit node v, DFS strategy proceeds along a
path from v as deeply into the graph as possible
before backing up
– Breadth-first-search (BFS)
• After visit node v, BFS strategy visits every node
adjacent to v before visiting any other nodes
Algorithm dfs(v)
print v; // you can do other things!
mark v as visited;
for (each unvisited node u adjacent to v)
dfs(u);
v v v
v 2 v 2 v
5 2
5 2
1
4 3
1
4 3 73
73 8
v 8 v v v
4 5 4 5
Find the minimum edge
attach to v2, v3 , v4 and
v5
07/14/2024 CS201 115
Prim’s algorithm (II)
Algorithm PrimAlgorithm(v)
• Mark node v as visited and include it in the
minimum spanning tree;
• while (there are unvisited nodes) {
– find the minimum edge (v, u) between a visited node v
and an unvisited node u;
– mark u as visited;
– add both v and (v, u) to the minimum spanning tree;
}
1 [v1] 0 5 ∞ ∞ ∞
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17
4 v3 [v1, v2, v4, v3] 0 5 12 9 16
5 v5 [v1, v2, v4, v3, v5] 0 5 12 9 16