0% found this document useful (0 votes)
22 views19 pages

DS 4

Uploaded by

bijo6074
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views19 pages

DS 4

Uploaded by

bijo6074
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Module 4

Tree Traversal
It refers to the process of visiting or accessing each node of the tree exactly once in a
certain order. Tree traversal algorithms help us to visit and process all the nodes of the tree.

Types of Traversal of Binary Tree


1. Inorder tree traversal
2. Preorder tree traversal
3. Postorder tree traversal

Inorder Tree Traversal

The left subtree is visited first, followed by the root, and finally the right subtree. Always
keep in mind that any node might be a subtree in and of itself. The output of a binary tree
traversal in order produces sorted key values in ascending order.

Inorder traversal visits the node in the order: Left -> Root -> Right

Algorithm for Inorder Traversal:


Inorder(tree)
 Traverse the left subtree, i.e., call Inorder(left->subtree)
 Visit the root.
 Traverse the right subtree, i.e., call Inorder(right->subtree)

Uses of Inorder Traversal:


 In the case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order.
 Inorder traversal can be used to evaluate arithmetic expressions stored in
expression trees.

Preorder Tree Traversal

In this traversal method, the root node is visited first, then the left subtree, and finally the
right subtree.
Preorder traversal visits the node in the order: Root -> Left -> Right
Uses of Preorder Traversal:
 Preorder traversal is used to create a copy of the tree.
 Preorder traversal is also used to get prefix expressions on an expression tree.

Algorithm for Preorder Traversal:


Preorder(tree)
 Visit the root.
 Traverse the left subtree, i.e., call Preorder(left->subtree)
 Traverse the right subtree, i.e., call Preorder(right->subtree)

Postorder Tree Traversal

The root node is visited last in this traversal method, hence the name. First, we traverse the
left subtree, then the right subtree, and finally the root node.

Postorder traversal visits the node in the order: Left -> Right -> Root

Uses of Postorder Traversal:


 Postorder traversal is used to delete the tree.
 It is also useful to get the postfix expression of an expression tree.
 Postorder traversal can help in garbage collection algorithms, particularly in
systems where manual memory management is used.

Algorithm for Postorder Traversal:


Algorithm Postorder(tree)
 Traverse the left subtree, i.e., call Postorder(left->subtree)
 Traverse the right subtree, i.e., call Postorder(right->subtree)
 Visit the root
Pre-order Traversal Without Recursion

The following operations are performed to traverse a binary tree in pre-order using a stack:
1. Start with root node and push onto stack.
2. Repeat while the stack is not empty
1. POP the top element (PTR) from the stack and process the node.
2. PUSH the right child of PTR onto to stack.
3. PUSH the left child of PTR onto to stack.
Consider the following tree.

Now POP 13 from the stack and process it.


We don’t have to PUSH anything to the
Start with node 4 and push it onto the stack because it also doesn’t have any
stack. subtrees.

Since the stack is not empty, POP 4 from


the stack, process it and PUSH its left(7) POP 18 from the stack, process it and
and right(18) child onto the stack. PUSH its left(5) and right(2) child to the

stack.

Similarly POP 5 and 2 from the stack one


Repeat the same process since the stack is after another. Since both these nodes don’t
not empty. POP 7 from the stack, process have any child, we don’t have to PUSH
it and PUSH its left(8) and right (13) child anything onto the stack.
onto the stack.

Again, POP 8 from the stack and process


it. Since it has no right or left child we
don’t have to PUSH anything to the stack.
The nodes are processed in the order
[ 4, 7, 8, 3, 18, 5, 2 ].This is the required
preorder traversal of the given tree

.
The formal algorithm is given below:
1. Set TOP = 1. STACK[1] = NULL and PTR = ROOT.
2. Repeat Steps 3 to 5 while PTR ≠ NULL:
3. Apply PROCESS to PTR->DATA.
4. If PTR->RIGHT ≠ NULL, then:
Set TOP = TOP + 1, and STACK[TOP] = PTR->RIGHT.
[End of If]
5. If PTR->LEFT ≠ NULL, then:
Set PTR = PTR -> LEFT.
Else:
Set PTR = STACK[TOP] and TOP = TOP - 1.
[End of If]
[End of Step 2 loop.]
6. Exit.
In-order Traversal Without Recursion

The following operations are performed to traverse a binary tree in in-order using a stack:
1. Start from the root, call it PTR.
2. Push PTR onto stack if PTR is not NULL.
3. Move to left of PTR and repeat step 2.
4. If PTR is NULL and stack is not empty, then Pop element from stack and set as PTR.
5. Process PTR and move to right of PTR , go to step 2.
Consider the following tree.

onto the stack.

Start with node 4 and call it PTR. Since


PTR is not NULL, PUSH it onto the stack.
Again, move to the left of node 7. Now
PTR is node 8, which is not NULL. So
PUSH it onto the stack.

Move to the left of node 4. Now PTR is


node 7, which is not NULL. So PUSH it

When we move again to the left of node 8,


PTR becomes NULL. So POP 8 from the
stack. Process PTR (8).
Move to the right child of PTR(8), which Move to the left of node 5, which is
is NULL. So POP 7 from the stack and NULL. So POP 5 from the stack and
process it. Now PTR points to 7. process it.

Move to the right child(13) of PTR and


PUSH it onto the stack. Now, move to the right of node 5, which is
NULL. So POP 18 from the stack and
process it.

Move to the left of node 13, which is


NULL. So POP 13 from the stack and
process it. Move to the right(2) of node 18 and PUSH
it on to the stack.

Since node 13 don’t have any right child,


POP 4 from the stack and process it. Now
PTR points to node 4. Since node 2 has left child, POP 2 from
the stack and process it. Now the stack is
empty and node 2 has no right child. So
stop traversing.

Move to the right of node 4 and put it on to


the stack. Now PTR points to node 18.

The nodes are processed in the order

[ 8, 7, 13, 4, 5, 18, 2 ]
. This is the required in order traversal of
Move to the left(5) child of 18 and put it the given tree.
onto the stack. Now PTR points to node 5.

The formal algorithm is given below:


1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat while PTR ≠ NULL:
(a) Set TOP = TOP + 1 and STACK[TOP] = PTR.
(b) Set PTR = PTR->LEFT.
[End of loop.]
3. Set PTR = STACK[TOP] and TOP = TOP - 1.
4. Repeat Steps 5 to 7 while PTR ≠ NULL:
5. Apply PROCESS to PTR->INFO.
6. If PTR->RIGHT ≠ NULL, then:
(a) Set PTR = PTR->RIGHT.
(b) Go to Step 3.
[End of If]
7. Set PTR = STACK[TOP] and TOP = TOP -1.
[End of Step 4 loop.]
8. Exit.
Post-order Traversal Without Recursion
The post order traversal algorithm is more difficult to implement than the previous two
algorithms. The following operations are performed to traverse a binary tree in post-order
using a stack:
1. Start from the root, call it PTR.
2. Push PTR onto stack if PTR is not NULL.
3. Move to left of PTR and repeat step 2.
4. If PTR has a right child R, then PUSH -R onto the stack.
5. Pop and process positive element from stack and set as PTR.
6. If a negative node is popped, (PTR = -N), then set PTR = N and go to step 2.

The formal algorithm is given below:


1. Set TOP = 1. STACK[1] = NULL and PTR = ROOT.
2. Repeat Steps 3 to 5 while PTR ≠ NULL:
3. Set TOP = TOP + 1 and STACK[TOP] = PTR.
4. If PTR->RIGHT ≠ NULL. then: [Push on STACK. ]
Set TOP = TOP + 1 and STACK[TOP] = PTR->-RIGHT.
[End of If structure.]
5. Set PTR = PTR->LEFT. [Updates pointer PTR.]
[End of Step 2 loop.]
6. Set PTR = STACK[TOP] and TOP = TOP - 1.
7. Repeat while PTR > 0:
(a) Apply PROCESS to PTR->INFO.
(b) Set PTR = STACK[TOP] and TOP = TOP - 1.
[End of loop.]
8. If PTR < 0, then:
(a) Set PTR = -PTR.
(b) Go to Step 2.
[End of If structure]
9. Exit.

Binary Search Tree


Binary Search Tree (BST) is a special type of binary tree in which the left child of a node
has a value less than the node’s value and the right child has a value greater than the node’s
value. This hierarchical structure allows for efficient Searching, Insertion,
and Deletion operations on the data stored in the tree.

Properties of Binary Search Tree:


 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.
 This means everything to the left of the root is less than the value of the root and
everything to the right of the root is greater than the value of the root. Due to this
performing, a binary search is very easy.
 The left and right subtree each must also be a binary search tree.
 There must be no duplicate nodes

Operations on a Binary Search Tree


A binary search tree can perform three basic operations: searching, insertion, and deletion.
Searching in a Binary Search Tree
The search operation finds whether or not a particular value exists in a tree. Since the binary
search tree is ordered, the search can be easily made.
Suppose we want to find X in a binary tree having root R.
 Compare item, X, with the root, R, of the tree.
 If X < R, then recursively search on the left subtree of the tree.
 If X > R, then recursively search on the right subtree of the tree.
Repeat these steps until we reach an empty subtree or locate a node R such that
R = X. That is, we continue the search from the root down the tree until we reach X or a
terminal node.
Algorithm: Search
IF ROOT = NULL
Return NULL
ELSE
IF ROOT -> DATA = X
Return ROOT
ELSE
IF X < ROOT -> DATA
Return Search(ROOT -> LEFT, X)
ELSE
Return Search(ROOT -> RIGHT, X)
[END OF IF]
[END OF IF]
[END OF IF]
Now compare 43 and 41.
Since 43>41move to the right subtree.
Consider the following BST and suppose
we want to find 43. Start from root node
50.

Now 43<47, So move to the right subtree,


Move to the left subtree of root where 43 is found.
since 43<50

Inserting in a Binary Search Tree


The insert operation is similar to the search operation. This is because first, you have to find
the correct position where the new element is to be inserted.
 If the tree is NULL, insert new element as the root.
 Otherwise, depending on the value, we continue to the right or left subtree, and when
we reach a position where the left or right subtree is null, we insert the new node
there.

Algorithm: Insert
IF TREE = NULL
Allocate memory for TREE
SET TREE -> DATA = VAL
SET TREE -> LEFT = TREE -> RIGHT = NULL
ELSE
IF VAL < TREE -> DATA
Insert(TREE LEFT, VAL)
ELSE
Insert(TREE RIGHT, VAL)
[END OF IF]
[END OF IF]
Consider the following BST and suppose
we want to insert a new node 42.

Since 43>42 but 43 has no left subtree. So


we insert 43 as the left child of node 43.

Start searching for element 42 from the


root.The search will stop when we reach
node 43.

Deleting from a Binary Search Tree


The deletion operation is to be done very carefully that the properties of the binary
search tree are not violated and no nodes are lost. Deletion is done in the following three
cases:
 Deleting a node that has no children.
 Deleting a node with one child.
 Deleting a node with two children.

Case 1: Deleting a node that has no children


Here the node we have to delete is a leaf node. So, we can simply delete the node from
the tree.
Suppose we want to delete node 58
from the following tree.

Simply delete node 58.


Case 2: Deleting a node with one child.
The following steps are to be done to delete a node that has only a single child.
 Replace the node with its child.
 Remove the child node.
Suppose we want to delete node 47
from the following tree.

Now delete the child node 43 from its


original position

Replace node 47 with its child node 43.

Case 3: Deleting a node with two children


The case is when the node we want to delete has 2 children. This case can be handled
by the following steps:
 Replace the node’s value with it’s in-order successor.
 Remove the in-order successor from its original position.

Suppose we want to delete node 41


from the following tree.
Replace the value of node 41 with its in- Now delete node 43 from its original
order successor 43. position.

Algorithm: Deletion
IF TREE = NULL
Write "VAL not found in the tree"
ELSE IF VAL < TREE->DATA
Delete(TREE->LEFT, VAL)
ELSE IF VAL > TREE->DATA
Delete(TREE->RIGHT, VAL)
ELSE IF TREE->LEFT AND TREE->RIGHT
SET TEMP = findLargestNode(TREE->LEFT)
SET TREE->DATA = TEMP->DATA
Delete(TREE->LEFT, TEMP->DATA)
ELSE
SET TEMP = TREE
IF TREE->LEFT = NULL AND TREE->RIGHT = NULL
SET TREE = NULL
ELSE IF TREE LEFT != NULL
SET TREE = TREE LEFT
ELSE
SET TREE = TREE RIGHT
[END OF IF]
FREE TEMP
[END OF IF]
Applications of Binary Search Tree

 Used for indexing and multilevel indexing in the database.


 Used to implement various searching algorithms.
 For dynamic sorting.
 Used for managing virtual memory areas in unix kernal.

Expression Tree
An expression tree is a special type of binary tree that is used to store algebraic expressions.
In an expression tree, each internal node corresponds to the operator and each leaf
node corresponds to the operand.
Consider the algebraic expression given as:
X = (a + b) - (c * d). This can be represented using a binary tree as follows:

The in-order traversal of the tree returns the infix expression. Similarly, the pre-order and
post-order traversal of the expression tree will return prefix and postfix expression
respectively.

Constructing an expression tree


An expression tree can be constructed with the help of a stack. Repeat the following steps for
every character in the postfix expression:
 If the character is an operand, PUSH it on to the stack.
 If the character is an operator, POP two values from the stack and insert them as the
child of operator. A pointer to the new root is pushed on to the stack.

Consider the postfix expression a b - c * . The following steps are done to create an
expression tree. Since the first two characters are operands, PUSH it onto the stack.

When the - symbol is read, a and b are popped from the stack and is added as the child of
node -. A pointer to the new node is now pushed onto the stack.

Now c is read and is pushed onto the stack.

Finally, the operator * is read, so we pop c and - from the stack and add it as the child of
node * .
When the operation is completed, the pointer to the root of three remains on the stack.

Graphs
Graph is a non-linear data structure consisting of vertices and edges. The vertices are
sometimes referred to as nodes and the edges are lines or arcs that connect any two nodes in
the graph. More formally a Graph is composed of a set of vertices( V ) and a set of
edges( E ). The graph is denoted by G(V, E).

The following figure shows an example of a graph where V(G) = {A, B, C, D, E}and E(G) =
{ (A,B), (B,C), (C,D), (D,E), (E,A), (A,D) }

Graph Terminology

Path:
A path can be defined as the sequence of nodes that are followed in order to reach some
terminal node V from the initial node U.
Closed Path:A path will be called as closed path if the initial node is same as terminal node.
A path will be closed path if V0=VN.
Simple Path:If all the nodes of the graph are distinct with an exception V0=VN, then such path
P is called as closed simple path.

Cycle:
A cycle can be defined as the path which has no repeated edges or vertices except the first
and last vertices.
Connected Graph:
A connected graph is the one in which some path exists between every two vertices (u, v) in
V. There are no isolated nodes in connected graph.

Complete Graph
A complete graph is the one in which every node is connected with all other nodes. A
complete graph contain n(n-1)/2 edges where n is the number of nodes in the graph.

Loop
An edge that is associated with the similar end points can be called as Loop.

Parallel Edges
When multiple edges in a graph connect the same pair of nodes, the edges are referred to
as parallel edges.

Multigraph
A multigraph is a graph that contains a self-loop, parallel edges, or both.

Digraph
A digraph is a directed graph in which each edge of the graph is associated with some
direction and the traversing can be done only in the specified direction.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called as
neighbours or adjacent nodes.
Degree of the Node
A degree of a node is the number of edges that are connected with that node. A node with
degree 0 is called as isolated node.

Types of Graphs

Undirected Graph
A graph with edges that don’t have direction is called an undirected graph. The edges in an
undirected graph indicate a two-way relationship.

Directed Graph
A Directed graph(digraph) is a graph that has edges with direction. These edges represent a
one-way relationship. That is edges can be only traversed in one direction.

Undirected Graph Directed Graph

If a digraph has a directed edge e = [u, v], then


 e begins at u, and ends at v.
 u is the origin or initial point of e, and v is the destination or terminal point of e.

The number of edges beginning at a node u is its outdegree. The number of edges that finish
at a node u is its indegree. If a node u has a positive outdegree but a negative indegree, it is
referred to as a source.

If a node u has a zero outdegree but a positive indegree, it is referred to as a sink.


If there is a directed path from v to u, then node v is said to be reachable from u. A directed
graph G is said to be connected (strongly connected) if there is a path from u to v and also a
path from v to u for each pair u, v of nodes in the graph.

Weighted Graph

A weighted graph is a graph that has a number associated with each of its edges. The number
is called the weight of the edge. A weighted graph can be undirected or directed. The weight
of a path P is the sum of the weights of the edges along the path.
Representation of Graphs

1.Sequential Representation : Adjacency Matrix

A graph with N nodes can be represented by an NxN square matrix with row and column
numbers representing the vertices. A cell at the cross-section of the vertices can only have the
values 0 or 1. An entry in the adjacency matrix [V i, Vj] = 1 only if there exists an edge from
Vi to Vj. A graph and its corresponding adjacency matrix are illustrated below:

In the case of a weighted graph, the weight of the edge is saved instead of the value 1. Since
the adjacency matrix will be sparse, a lot of space will be wasted. Adding or removing new
nodes may be difficult.

2.Linked Representation

The linked representation of a graph contains two lists, a NODE list and
an EDGE(Adjacency) list. Each element in the NODE list will correspond to a node in the
graph. It contains:
 Name of the Node.
 Pointer to adjacent nodes.
 Pointer to next node.
Each element in the edge list will represent an edge of the graph. It contains:
 Destination node of the edge.
 Link that link together the edges with the same initial node.
A graph and its corresponding linked representation are illustrated below.

Because we only need to keep the values for the edges, an adjacency list is efficient in
terms of storage.
Applications of Graph
 In circuit networks: connection points as vertices and wires as edges.
 In transport network: stations as vertices and routes as edges.
 For finding shortest paths.

Resource Allocation Graph


Google Maps as a Graph
 In state diagrams: states as vertices and transition between states as edges.
 For scientific computations.
 In recommendation algorithms.

Graph Traversal

The two most common ways a Graph can be traversed are:


 Depth First Search (DFS)
 Breadth First Search (BFS)
DFS is usually implemented using a Stack or by the use of recursion (which utilizes the call
stack), while BFS is usually implemented using a Queue.

Breadth First Search (BFS)

In the BFS algorithm, the traversal starts from a node and then carries onto its adjacent nodes.
It traverses all the sibling nodes within a level and then moves to the next level. The search
continues until all the vertices are visited.
BFS implementation puts each vertex in the graph into the following lists:
 Visited.(array)
 Not Visited (Queue).
This marking is used to avoid cycles while traversing.
Algorithm
1. Put the starting node to the back of the Queue.
2. Repeat Steps 3 and 4 until Queue is empty.
3. Remove the front node N of Queue and add it to the Visited List.
4. Add all unvisited neighbours of N to the rear of the Queue.

Let us understand the algorithm using a diagram.


In the above diagram, the full way of traversing is shown using arrows.
Step 1: Create a Queue with the same size as the total number of vertices in the graph.
Step 2: Choose 12 as your beginning point for the traversal. Visit 12 and add it to the
Queue.
Step 3: Insert all the adjacent vertices of 12 that are in front of the Queue but have not
been visited into the Queue. So far, we have 5, 23, and 3.
Step 4: Delete the vertex in front of the Queue when there are no new vertices to visit
from that vertex. We now remove 12 from the list.
Step 5: Continue steps 3 and 4 until the queue is empty.
Step 6: When the queue is empty, generate the final spanning tree by eliminating
unnecessary graph edges.

Applications of Breadth First Search Algorithm


 Finding all connected components in a graph G.
 Finding the shortest path between two unweighted network nodes, u and v.
 Finding the shortest path between two weighted network nodes, u and v

Depth First Search (DFS)


In the DFS algorithm, the traversal starts from a node and then follows its outgoing nodes.
The search goes deeper and deeper until the goal node is found, or until a node that has no
children is found.
DFS implementation puts each vertex in the graph into the following lists:
 Visited.(array)
 Not Visited (Stack).
Algorithm
1. Start from any vertex. PUSH it onto the stack.
2. POP the top item from the stack and add to the list of visisted nodes.
3. Select a adjacent vertex that is not visisted and PUSH it onto the stack.
4. Repeat steps 2 and 3 until stack is empty.

Let us understand the algorithm using a diagram.


The entire path of traversal is depicted in the diagram above with arrows.
 Step 1: Create a Stack with the total number of vertices in the graph as its size.
 Step 2: Choose 12 as your beginning point for the traversal. Go to that vertex and
place it on the Stack.
 Step 3: Push any of the adjacent vertices of the vertex at the top of the stack that has
not been visited onto the stack. As a result, we push 5
 Step 4: Repeat step 3 until there are no new vertices to visit from the stack’s top
vertex.
 Step 5: Use backtracking to pop one vertex from the stack when there is no new
vertex to visit.
 Step 6: Repeat steps 3, 4, and 5.
 Step 7: When the stack is empty, generate the final spanning tree by eliminating
unnecessary graph edges.

Applications of Depth First Search Algorithm


 To Find a path between two specified nodes, u and v, in an unweighted or
weighted graph.
 To determine whether a graph is connected or not.
 To calculate the spanning tree of a connected graph.

You might also like