CS3353 Unit4
CS3353 Unit4
Syllabus:
Trees – Binary Trees – Binary tree representation and traversals –Binary Search Trees – Applications of
trees. Set representations – Union-Find operations. Graph and its representations – Graph Traversals.
TREES STRUCTURE
Definition:
Tree is a non-linear data structure. It organized the data in hierarchical manner. A tree is a finite set of one
or more nodes such that there is a specially designated node called the root node and root node can have zero
or more sub trees T1,T2,T3, .............. ,Tn. Each of whose roots are connected by a directed edge from root R.
Tree is collection of nodes in which the first node is called root and root has many number of sub tree T1,
T2, T3… Tn.
Terms:
1. Root
A node which does not have a parent is called as root node.
2. Node
Each data element in the tree is called as node.
3. Leaf node
A node which does not have any children is called leaf node.
4. Siblings
A child of same parent is called sibling.
5. Path
A path from node n1 to nk is defined has sequence of nodes n1, n2, n3 ......... nk. Such that ni is a parent
of ni +1.Example: A->B->E->J
6. Length for a path
Number of edges in the path.
Example: Consider path from A to J is 3
7. Degree
Number of sub trees of the node is called degree.
8. Level
Root is at level 1 then i+s children are at level 2+1
Example: level
1
9. Depth
For any node n, the depth n is length of unique path from root to n.
10. Height
For any node n, the height of node n is the length of longest path from n to left.
11. Forest
Collection of tree node is known as forest.
Definition: -
Binary Tree is a special type of tree in which no node can have most two children. Typically, child nodes
of a binary tree on the left is called left child and node on right is called right child. Maximum number of nodes
A simple binary tree of size 9 and height 3, with a root node whose value is 2. The above tree is neither
a sorted nor a balanced binary tree
Representation of tree.
1. Sequential representation or array representation.
2. Linked representation.
1. Sequential representation or array representation.
The elements are represented using arrays. For any element in position i, the left child is in position 2i,
the right child is in position (2i + 1), and the parent is in position (i/2).
A B C D E F G
0 1 2 3 4 5 6
2
2. Linked representation
The elements are represented using pointers. Each node in linked representation has three fields, namely,
* Pointer to the left subtree
* Data field
* Pointer to the right subtree
Inorder Traversal
The inorder traversal of a binary tree is performed as
* Traverse the left subtree in inorder
* Visit the root
* Traverse the right subtree in inorder.
Recursive Routine for Inorder Traversal
void Inorder (Tree T)
{
if (T!=NULL)
{
Inorder (T->left);
printf(“%d”,T->data);
Inorder (T->right);
}
}
3
Preorder Traversal
The preorder traversal of a binary tree is performed as follows,
* Visit the root
* Traverse the left subtree in preorder
* Traverse the right subtree in preorder.
EXPRESSION TREE
An expression tree is tree in which left nodes have the operands and interior node have the operators.
Like binary tree, expression tree can also be travesed by inorder, preorder and postorder traversal.
Example:
(A+B)*(C-D)
+ -
A B C D
4
Constructing an Expression Tree
Let us consider postfix expression given as an input for constructing an expression tree by performing the
following steps:
1. Read one symbol at a time from the postfix expression and then scan the expression from left to right
manner.
2. Check whether the symbol is an operand or operator.
a. If the symbol is an operand, create a one - node tree and push a pointer on to the stack.
b. If the symbol is an operator pop two pointers from the stack namely T1 and T2 and form a new tree
with root as the operator and T2 as a left child and T1 as a right child. A pointer to this new tree is
then pushed onto the stack.
3. Repeated the above steps until reach the end of the expression.
4. The final pointer in the stack is complete expression tree.
Example: ABC*+DE*F+G*+
Operations of BST.
1. Insertion
2. Deletion
3. Find
4. Find min
5. Find max
6. Retrieve
Notes: when you’re constructing the binary tree the given elements are read from first.
Example:
5
struct treenode
{
int data;
struct
treenode
*left; struct
treenode
*right;
};
Routine for perform find.
else
}
return T;
6
Routine for perform insertion.
struct treenode* insert(struct treenode *t,int x)
{
if(t==NULL)
{
7
Routine for perform deletion
struct treenode * findmin(struct treenode *t)
{
if(t==NULL)
return NULL; /* There is no element in the tree */
else if(t->left==NULL) /* Go to the left sub tree to find the min element */
return t;
else
return findmin(t->left);
/* Here we will replace with minimum element in the right sub tree */
temp = findmin(t->right);
t->data = temp->data ;
/* As we replaced it with some other node, we have to delete that node */
t->right = deletion (t->right,t->data);
else
{
/* If there is only one or zero children then we can directly remove it from the tree and connect its
parent to its child */
temp = T;
if(t->left==NULL)
t = t->right;
else if(T->right == NULL)
t = t->left;
free(temp); /* temp is longer required */
}
return T;
void inorder(struct treenode *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d \t",t->data);
inorder(t->right);
}
}
Applications of Tree
1. Manipulate hierarchical data.
2. Make information easy to search 3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
DIFFERENCE BETWEEN BINARY AND BINARY SEARCH TREES:
BINARY TREE BINARY SEARCH TREE
It is a tree with only two
It is also a tree with only two children.
children
It has no restrictions regardingits In this the left child is lesser than the parent and the rightchild is greater
children than the parent
THE DISJOINT SET ADT
A disjoint set data structure keeps note of a set of non-overlapping subsets. It is a data structure that
helps us solve the dynamic equivalence problem.
Various Representations of Disjoint Set Adt
Array Representation
Linked List Representation
3. Connected graph
A graph is called connected if there is a path from any vertex to any other vertex.
Strongly Connected Graph If there is a path from every vertex to every other vertex in a directed graph
then it is said to be strongly connected graph. Otherwise, it is said to be weakly connected graph.
4. Multiple edges
5. Distinct edges e and e` are called multiple edges if they connected the same and point.
6. Loop
Ex: e=(U,V) then e`=(U,V)
An edges e is called loop if it has identical and points.
E=(U,U)
7. Path
A path is a sequence of distinct vertices, each adjacent to the next. The length of such a path is number of
edges on that path.
8. Cycle
A path from a node to itself is called cycle. Thus, a cycle is a path in which the initial and final vertices are
same.
Acyclic Graph A directed graph which has no cycles is referred to as acyclic graph. It is abbreviated as DAG
[Directed Acyclic Graph]
Note: a graph need not be a tree but a tree must be graph
9. Degree, incidence and adjacent
A vertex V is incident to an edges e if V is one of the two vertices in the order pair of vertices that
constitute e.
The degree of a vertex is the number of edges incident to it.
The indegree of vertex V is the number of edges that have V as head and the outdegree of vertex V is
number of edges that have v as the tail.
A vertex V is adjacent to vertex U if there is an edge from U to V. if V is adjacent to U,V is called a
successor of U, and U a predecessor of V.
10. Weighted graph
A weighted graph is a graph in which edges are assigned weighted. Weights of an edge are called as cost.
11. complete graph
If there is an edges from each vertices to all other vertices in graph is called as completed graph.
Graph and its representations
Graph is a data structure that consists of following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not same as (v, u) in
case of directed graph(di-graph). The pair of form (u, v) indicates that there is an edge from vertex u to vertex v. The
edges may contain weight/value/cost.
Graphs are used to represent many real life applications: Graphs are used to represent networks. The networks
may include paths in a city or telephone network or circuit network. Graphs are also used in social networks like
LinkedIn, Facebook. For example, in Facebook, each person is represented with a vertex (or node). Each node is a
structure and contains information like person id, name, gender and locale. See this for more applications of graph.
Following is an example undirected graph with 5 vertices.
Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether there is
an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).
Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same
space. Adding a vertex is O(V^2) time.
Adjacency List:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i]
represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a
weighted graph. The weights of edges can be stored in nodes of linked lists. Following is adjacency list representation of
the above graph.
STUDENTSFOCUS
Adjacency List Representation of the above Graph
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we keep on dequeuing
in order to get all unvisited nodes. When the queue gets emptied, the program is over.
As in the example given above, DFS algorithm traverses from A to B to C to D first then to E, then to F and
lastly to G. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices
from the stack, which do not have adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
As C does not have any unvisited adjacent node so we keep popping the stack until we find a node that has an
unvisited adjacent node. In this case, there's none and we keep popping until the stack is empty.