Data Structures - Trees & Graphs
Data Structures - Trees & Graphs
2
Sri Ramakrishna College of Arts and Science
Definitions - Continued
The definition of the tree emphasizes on the aspect of
(i) connectedness and
(ii) absence of closed loops or what are termed cycles.
Beginning from the root node, the structure of the tree
permits connectivity of the root to every other node in
the tree.
•In general, any node is reachable from any where in the
tree. Also, with branches providing the links between the
nodes, the structure ensures that no set of nodes link
together to form a closed loop or a cycle.
5
Sri Ramakrishna College of Arts and Science
BASIC TERMNOLOGIES - Contd
Root- The first node from where the tree originates is called as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
Edge- The connecting link between any two nodes is called as an edge.
In a tree with n number of nodes, there are exactly (n-1) number of edges.
Here,
Degree of node A = 2
Degree of node B = 3
Degree of node C = 2
Degree of node D = 0
Degree of node E = 2
Degree of node F = 0
Degree of node G = 1
Degree of node H = 0
Degree of node I = 0
Degree of node J = 0
Degree of node K = 0
Sri Ramakrishna College of Arts and Science
7
BASIC TERMNOLOGIES – Contd
The DATA field of the node stores the information content of the
tree node. A fixed set of LINK fields accommodate the pointers to
the child nodes of the given node. In fact the maximum number
of links the node would require is equal to the degree of the tree.
10
Sri Ramakrishna College of Arts and Science
Representation of Trees - Contd
The linked representation of the tree shown in Fig. 8.1 is illustrated in Fig. 8.2 (b).
Observe the colossal wastage of space by way of null pointers!
12
Sri Ramakrishna College of Arts and Science
Representation of Trees - Contd
14
Sri Ramakrishna College of Arts and Science
Representation of Trees - Contd
It is essential that the distinction between trees and binary trees are brought out clearly.
While a binary tree can be empty with zero nodes, a tree can never be empty.
Again while the ordering of the subtrees in a tree is immaterial, in a binary tree the distinction
of left and right subtrees are very clearly maintained.
All other terminologies applicable to trees such as levels, degree, height, leaf nodes, parent,
child, siblings etc. are also applicable to binary trees.
16
Sri Ramakrishna College of Arts and Science
Binary Trees: Basic Terminologies & Types
However, there are some important observations regarding binary trees.
(i) The maximum number of nodes on level i of a binary tree is 2i –1, i ≥ 1.
(ii) The maximum number of nodes in a binary tree of height h is 2h– 1, h ≥ 1.
(iii) For any non empty binary tree, if to is the number of terminal nodes and t2 is
the number of nodes of degree 2, then to = t2 + 1
From the figure 8.5 the maximum number of nodes on level 3 is 23-1 = 22 = 4. Also with the
height of the binary tree being 3, the maximum number of nodes = 23-1 = 7. [Again to = 4
and t2 = 3 which yields to = t2 + 1.]
Figure 8.6(b) illustrates an example complete binary tree. A binary tree which is dominated
solely by left child nodes or right child nodes is called a skewed binary tree or more
specifically left skewed binary tree or right skewed binary tree respectively.
Figure 8.6(c) illustrates examples of skewed binary trees.
19
Sri Ramakrishna College of Arts and Science
Representation of Binary Trees
A binary tree could be represented by using a
1. Sequential data structure (arrays) and 2. Linked data structure
Array representation of binary trees (Sequential data structure )
To represent the binary tree as an array, the sequential numbering system emphasized by a
complete binary tree comes in handy. Consider the binary tree shown in Fig. 8.7(a). The array
representation is as shown in Fig. 8.7(b). The association of numbers pertaining to parent and
left/right child nodes makes it convenient to access the appropriate cells of the array. However,
the missing nodes in the binary tree and hence the corresponding array locations, are left
empty in the array. This obviously leads to a lot of wastage of space. However, the array
representation ideally suits a full binary tree due to its non wastage of space.
20
Sri Ramakrishna College of Arts and Science
Linked Representation of Binary Trees
Linked representation of binary trees : The linked representation of a binary tree has the node
structure shown in Fig. 8.8(a). Here, the node, besides the DATA field, needs two pointers
LCHILD and RCHILD to point to the left and right child nodes respectively. The tree is accessed by
remembering the pointer to the root node of the tree.
In the binary tree T shown in Fig. 8.8(b), LCHILD (T) refers to the node storing b and
RCHILD(LCHILD (T )) refers to the node storing d and so on. The following are some of the important
observations regarding the linked representation of a binary tree:
(i) If a binary tree has n nodes then the number of pointers used in its linked representation is 2*n.
(ii) The number of null pointers used in the linked representation of a binary tree with n nodes is n+1.
However, in a linked representation it is difficult to determine a parent given a child node. In any
case if an application so requires, a fourth field PARENT may also be included in the structure. 21
Binary Tree Traversals
Inorder Traversal The traversal keeps moving left in the binary tree until one can move no
further, processes the node and moves to the right to continue its traversal again. In the
absence of any node to the right, it retracts backwards by a node and continues the traversal.
Algorithm 8.1 illustrates a recursive procedure to perform inorder traversal of a binary tree.
For clarity of application, the action Process Node (P) is interpreted as Print node. Observe the
recursive procedure reflect the maxim LPR repetitively.
Execute the traversal of the binary tree as traverse left subtree, process root node and
traverse right subtree. Repeat the same for each of the left and right subtrees encountered.
The output of the call to the procedure INORDER_TRAVERSAL with the root of the appropriate
subtree as its input. The final output of the inorder traversal (from Fig.8.9) is S T Q P W U R V.
23
Sri Ramakrishna College of Arts and Science
Inorder Traversal
Example 8.2 An easy method to obtain the traversal would be to run ones fingers on
the binary tree with the maxim: move left until no more nodes, process node, then
move right and continue the traversal.
24
Sri Ramakrishna College of Arts and Science
Inorder Traversal
25
Sri Ramakrishna College of Arts and Science
Inorder Traversal
26
Sri Ramakrishna College of Arts and Science
Inorder Traversal
27
Sri Ramakrishna College of Arts and Science
Post Order Traversal
Postorder Traversal
The traversal proceeds by keeping to the left until it is no further possible, turns right to
begin again or if there is no node to the right, processes the node and retraces its direction by
one node to continue its traversal. Algorithm 8.2 illustrates a recursive procedure to perform
post order traversal of a binary tree. The recursive procedure reflects the maxim LRP invoked
repetitively.
Example : The postorder traversal of the binary tree shown in Fig. 8.9.
Output : The output of Postorder traversal is TSQWUVRP.
Preorder Traversal
The traversal processes every node as it moves left until it can move no further. Now it turns
right to begin again or if there is no node in the right, retracts until it can move right to
continue its traversal. The recursive procedure for the preorder traversal is illustrated in
Algorithm 8.3. The recursive procedure reflects the maxim PLR invoked repetitively.
Example : The preorder traversal of the binary tree shown in Fig. 8.9.
Out put of the Preorder traversal : PQSTRUWV.
33
Sri Ramakrishna College of Arts and Science
Preorder Traversal
Example 8.4 An easy method as discussed before would be to trace the traversal on the binary
tree using the maxim: Process nodes while moving left until no more nodes, turn right, and
otherwise retract to continue the traversal.
An alternative method is to trace the recursive steps of the algorithm using the following
scheme: Execute the traversal of the binary tree as, process root node, traverse left subtree
and traverse right subtree, repeating the same for each of the left and right subtrees
encountered.
34
Sri Ramakrishna College of Arts and Science
Preorder Traversal
35
Sri Ramakrishna College of Arts and Science
Preorder Traversal
36
Sri Ramakrishna College of Arts and Science
Preorder Traversal
37
Sri Ramakrishna College of Arts and Science
Preorder Traversal
38
Sri Ramakrishna College of Arts and Science
Threaded Binary Tree
The linked representation of the binary tree showed that for a binary tree with n nodes, 2n
pointers are required of which (n+1) are null pointers.
A.J. Perlis and C.Thornton devised a prudent method to utilize these (n+1) empty pointers,
introducing what are called threads. Threads are also links or pointers but replace null
pointers by pointing to some useful information in the binary tree. Thus, for a node NODE if
RCHILD(NODE) is NIL then the null pointer is replaced by a thread which points to the node
which would occur after NODE when the binary tree is traversed in inorder. Again if LCHILD
(NODE) is NIL then the null pointer is replaced by a thread to the node which would
immediately precede NODE when the binary tree is traversed in inorder.
Figure 8.10. illustrates a threaded binary tree. The threads are indicated using broken lines to
distinguish them from the normal links indicated with solid lines. The inorder traversal of the
binary tree is also shown in the figure.
Note that the left child of G and the right child of E have threads which are left dangling due
to the absence of an inorder predecessor and successor respectively.
39
Sri Ramakrishna College of Arts and Science
Representation of Threaded Binary Tree
40
Sri Ramakrishna College of Arts and Science
Threaded Binary Tree
•In order to avoid confusion while processing the threaded binary tree when the LCHILD and
RCHILD fields are utilized to represent both links and threads . In the node structure includes
two more fields which act as flags to indicate if the LCHILD and RCHILD fields represent a
thread or a link.
•If the LEFT THREAD TAG or RIGHT THREAD TAG is marked true then LCHILD and RCHILD fields
represent threads otherwise they represent links Also, to tuck in the dangling threads which
are bound to arise, the linked representation of a threaded binary tree includes a head node.
The dangling threads point to the head node.
•The head node by convention has its LCHILD pointing to the root node of the threaded binary
tree and therefore has its LEFT THREAD TAG set to false. The RIGHT THREAD TAG field is also
set to false but the RCHILD link points to the head node itself. Figure 8.12(a) shows the linked
representation of an empty threaded binary tree and Fig. 8.12(b) that of a non-empty
threaded binary tree.
41
Sri Ramakrishna College of Arts and Science
Linked Representations of Threaded Binary Tree
42
Sri Ramakrishna College of Arts and Science
Graphs : Definitions & Basic Terminologies
Graph : A graph G = (V, E ) consists of a finite non empty set of vertices V also called points or
nodes and a finite set E of unordered pairs of distinct vertices called edges or arcs or links.
Example :Figure 9.3 illustrates a graph. Here V = {a, b, c, d} and E= {(a, b), (a, c), (b, c), (c,
d)}. However it is convenient to represent edges using labels as shown in the figure.
V : Vertices : {a, b, c, d}
E : Edges : {e1, e2, e3, e4}
A graph G = (V, E) where E = Ø, is called as a null or empty graph. A graph with one vertex and
no edges is called a trivial graph.
Multigraph : A multigraph G = (V, E) also consists of a set of vertices and edges except that E
may contain multiple edges (i.e.) edges connecting the same pair of vertices, or may contain
loops or self edges (i.e.) an edge whose end points are the same vertex.
Example : Figure 9.4 illustrates a multigraph Observe the multiple edges e 1, e2 connecting
vertices a, b and e5, e6 , e7 connecting vertices c, d respectively. Also note the self edge e 4 .
Note : Graphs do not contain multiple edges or loops but multigraphs contain multiple edges
or loops
43
Graphs : Definitions & Basic Terminologies
Directed Graphs or digraphs : Each edge can be traversed only in a specified direction
(ordered pairs of vertices). The edge eij is referred to as < vi, vj > which is distinct from < vj ,vi >
where vi, vj are distinct vertices. In < vi, vj >, vi is known as tail of the edge and vj as the head.
Undirected Graphs: Each edge can be traversed in either direction (Unordered pairs of
vertices). The edge eij of such an undirected graph is represented as (v i, vj) where vi, vj are
distinct vertices. Thus an undirected edge (vi, vj ) is equivalent to (vj ,vi). Figure 9.5(a-b)
illustrates a digraph and an undirected graph.
In Fig. 9.5(a), e1 is a directed edge between v1 and v2 , (i.e.) e1 = < v1, v2 >, whereas in Fig.9 .5(b)
e1 is an undirected edge between v1 and v2, (i.e.) e1 = (v1, v2). The list of vertices and edges of
graphs G1 and G2 are:
Vertices (G1) : {v1, v2, v3, v4 }
Vertices (G2) : {v1, v2, v3, v4}
Edges (G1) : {< v1, v2 > < v2, v1 > < v1, v3 > < v3, v4 > < v4, v3 >} or {e1, e2, e3, e4 , e5}
44
Graphs : Definitions & Basic Terminologies
In the case of an undirected edge (vi, vj) in a graph, the vertices vi, vj are said to be adjacent
or the edge (vi, vj) is said to be incident on vertices vi, vj. Thus in Fig. 9.5(b) vertices v1, v3 are
adjacent to vertex v2 and edges e1 : (v1, v2 ), e3 : (v2, v3) are incident on vertex v2.
On the other hand, if < vi, vj> is a directed edge, then vi is said to be adjacent to vi and vj is
said to be adjacent from vi. The edge < vi, vj> is incident to both vi and vj . Thus in Fig. 9.5(a)
vertices v2 and v3 are adjacent from v1, and v1 is adjacent to vertices v2 and v3. The edges
incident to vertex v3 are < v1, v3 >, < v3, v4 > and < v4, v3 >.
45
Sri Ramakrishna College of Arts and Science
Graphs : Definitions & Basic Terminologies
Complete graphs :
The number of distinct unordered pairs (vi, vj), vi ≠ vj in a graph with n vertices is nC2 =
An n vertex undirected graph with exactly n(n-1)/2 edges is said to be complete.
Figure 9.6 illustrates a complete graph. The undirected graph with 4 vertices has all its 4C2 =
6 edges intact.
In the case of a digraph with n vertices, the maximum number of edges is given by nP2 = n. (n
-1). Such a graph with exactly n.(n-1) edges is said to be a complete digraph.
Figure 9.7(a) illustrates a digraph which is complete and Fig. 9.7(b) a graph which is not
complete.
46
Sri Ramakrishna College of Arts and Science
Graphs : Definitions & Basic Terminologies
Subgraph : A subgraph G = (V’, E’) of a graph G = (V, E) is such that V’ ⊆ V and E’ ⊆ E.
Example : Figure 9.8 illustrates some subgraphs of the directed and undirected graphs
shown in Fig. 9.5 (Graphs G1 and G2)
47
Graphs : Definitions & Basic Terminologies
Figure 9.9(a) illustrates a path P1 from vertex v1 to v4 in graph G1 of Fig. 9.5(a) and Fig.
9.9(b) illustrates a path P2 from vertex v1 to v4 of graph G2 of Fig. 9.5(b).
The length of a path is the number of edges on it. In Fig. 9.9 the length of path P1 is 4 and the
length of path P2 is 3. A simple path is a path in which all the vertices except possibly the first
and last vertices are distinct.
48
Graphs : Definitions & Basic Terminologies
•In graph G2 (Fig. 9.5(b)), the path from v1 to v4 given by {(V1 , V2), (V2 , V3), (V3 , V4)} and
written as {V1 , V2 , V3, V4} is a simple path where as the path from V3 to V4 given by {(V3, V1),
(V1 , V2), (V2 , V3), (V3 , V4)} and written as {V3, V1 , V2 , V3, V4} is not a simple path but a path
due to the repetition of vertices.
•Also in graph G1 (Fig. 9.5(a)) the path from v1 to v3 given by {<V1, V2 >, <V2 , V1 >, <V1, V3 > }
written as {V1 , V2 , V1, V3} is not a simple path but a mere path due to the repetition of
vertices. However, the path from v2 to v4 given by { <V2 , V1 >, <V1, V3 > , <V3, V4 > } written
•A cycle is a simple path in which the first and last vertices are the same. A cycle is also
known as a circuit, elementary cycle, circular path or polygon.
•In graph G2 (Fig. 9.5(b)) the path {v1, v2, v3, v1} is a cycle. Also, in graph G1 (Fig. 9.5(a)) the
path {v1, v2, v1} is a cycle or more specifically a directed cycle.
49
Graphs : Definitions & Basic Terminologies
•Connected graphs : Two vertices vi , vj in a graph G are said to be connected only if there is a
•An undirected graph is said to be a connected graph if every pair of distinct vertices vi , vj
are Connected. Example figure 9.10.
•Graph G2 (Fig. 9.5(b)) is connected where as graph G3 shown in Fig. 9.10 is not connected.
•In the case of an undirected graph which is not connected, the maximal connected
subgraph is called as a connected component or simply a component.
•Example Graph G3 (Fig. 9.10) has two connected components viz., graph G31 and G32.
50
Graphs : Definitions & Basic Terminologies
A directed graph is said to be strongly connected if every pair of distinct vertices vi , vj are
connected (by means of a directed path). Thus if there exists a directed path from vi to vj
then there also exists a directed path from vj to vi .
Graph G4 shown in Fig. 9.11 is strongly connected.
However, the digraph shown in Fig. 9.12 is not strongly connected but is said to possess two
strongly connected components. A strongly connected component is a maximal subgraph
that is strongly connected.
51
Graphs : Definitions & Basic Terminologies
Trees : A tree is defined to be a connected acyclic graph. The following properties are
satisfied by a tree:
(i) There exists a path between any two vertices of the tree, and
(ii) No cycles must be present in the tree. In other words, trees are acyclic.
Example Figure 9.13(a) illustrates a tree. Figure 9.13(b) illustrates graphs which are not
trees due to the violation of the property of acyclicity and connectedness respectively.
Figure 9.5.b
•Degree : The degree of a vertex in an undirected graph is the number of edges incident to
that vertex. A vertex with degree one is called as a pendant vertex or end vertex. A vertex
with degree zero and hence has no incident edges is called an isolated vertex.
•Example In graph G2 (Fig. 9.5(b)) the degree of vertex v3 is 3 and that of vertex v2 is 2.
In the case of digraphs, we define the indegree of a vertex v to be the number of edges
with v as the head and the outdegree of a vertex to be number of edges with v as the tail.
In graph G1 (Fig. 9.5(a)) the indegree of vertex v3 is 2 and the out degree of vertex v4 is 1.
Sri Ramakrishna College of Arts and Science
Graphs : Definitions & Basic Terminologies
Isomorphic graphs : Two graphs are said to be isomorphic if,
(i) they have the same number of vertices
(ii) they have the same number of edges
(iii) they have an equal number of vertices with a given degree.
The property of isomorphism can be verified on the lists of vertices and edges of the two
graphs G8 and G9 when superimposed as shown below:
53
Graphs : Definitions & Basic Terminologies
Cut set : A cut set in a connected graph G is the set of edges whose removal from G leaves G
disconnected, provided the removal of no proper subset of these edges disconnects the
graph G. Cut sets are also known as proper cut set or cocycle or minimal cut set.
Figure 9.15 illustrates the cut set of the graph G10.
The cut set {e1, e4} disconnects the graph into two components as shown in the figure.
{e5} is also another cut set of the graph.
54
Graphs : Definitions & Basic Terminologies
Labeled graphs : A graph G is called a labeled graph if its edges and / or vertices are assigned
some data. In particular if the edge e is assigned a non negative number l(e) then it is called
the weight or length of the edge e.
Example Figure 9.16 illustrates a labeled graph. A graph with weighted edges is also known
as a network.
Eulerian graph : A walk starting at any vertex going through each edge exactly once and
terminating at the start vertex is called an Eulerian walk or Euler line.
•The Koenigsberg bridge problem was in fact a problem of obtaining an Eulerian walk for
the graph concerned.
•Given a connected graph G, G is an Euler graph if all the vertices are of even degree.
Example Figure 9.17 illustrates an Euler graph {e1, e2, e3, e4} shows a Eulerian walk. The
even degree of the vertices may be noted.
55
Graphs : Definitions & Basic Terminologies
Hamiltonian circuit : A Hamiltonian circuit in a connected graph is defined as a closed walk
that traverses every vertex of G exactly once, except of course the starting vertex at which
the walk terminates.
A circuit in a connected graph G is said to be Hamiltonian if it includes every vertex of G. If
any edge is removed from a Hamiltonian circuit then what remains is referred to as a
Hamiltonian path. Hamiltonian path traverses every vertex of G.