UNIT IV NON-LINEAR DATA STRUCTURES Trees – Binary Trees – Binary tree representation and traversals – Application of trees: Set representation and Union-Find operations – Graph and its representations – Graph Traversals – Representation of Graphs – Breadth-first search – Depth-first search - Connected components.

Tree ADT Definition: A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called root, and zero or more nonempty (sub) trees T1, T2, … Tk, each of whose roots are connected by a direct edge from r. A tree structure is shown in the figure below: A

B

C

F

K

D

G

H

L

E

I

J

M

Tree Terminologies:  Node Item or information or data-element.  Root The first and top node in a tree is called the root node. i.e. A node which doesn‟t have a parent is called a root.  Parent Page 1

A node that has children is called the parent node (or ancestor node)  Edge An edge is a connection between two nodes.  Child A node of a tree referred to by a parent node. Every node, except the root, is the child of some parent. Here Node B is the child of node A, if A is the parent of B.  Sibling Children of the same parent are said to be siblings. Here B, C, D, E is siblings of A. Similarly I, J, K, L are siblings.  Path A path in a tree is a list of distinct nodes in which successive nodes are connected by edges in the tree. There is exactly only one path from each node to root.  Leaf A node which doesn‟t have children is called leaf or terminal node. (Or) Nodes at the bottommost level of the tree are called leaf nodes . Here B, K, L, G, H, M, J is leaf nodes.  Ancestor Node A is an ancestor of node B, if A is the parent of B.  Length The length is defined as the number of edges on the path. The length for the path A to L is 3.  Degree The number of subtrees of a node is called its degree. Degree of A is 4 Degree of C is 2 Page 2

Degree of D is 1 Degree of H is 0.  Level of a Node The level of the node is the no. of edges from the root to that node. Level of the root of a tree is 0, and the level of any other node in the tree is one more than the level of its parent.  Depth of a Tree The depth of a tree is the maximum level of any leaf in the tree (also called the height of the tree). For any node n, the depth of n is the length of the unique path from root to n. The depth of the root is 0. Depth of node L is 3.  Descendant Node B is the descendant of node A, if A is an ancestor of node B  Subtree Any node of a tree with all of its descendants is called its subtree.  Height The height of a node is the length of the longest downward path to a leaf from that node. The height of the root is the height of the tree. For any node n, the height of the node n is the length of the longest path from n to the leaf. The height of the leaf is always 0. Here -height of node F is 1. -height of L is 0. BINARY TREES • A binary tree is a tree in which no node can have more than two children.

Page 3

A tree is a finite set of nodes having a distinct node called root. Binary Tree is a tree which is either empty or has at most two subtrees, each of the subtrees also being a binary tree. It means each node in a binary tree can have 0, 1 or 2 subtrees. A left or right subtree can be empty. It has a distinct node called root i.e. 2. And every node has 0, 1 or 2 children. So it is a binary tree as every node has a maximum of 2 children. Example:

If A is the root of a binary tree & B the root of its left or right subtree, then A is the parent or father of B and B is the left or right child of A. Those nodes having no children are leaf nodes. Any nodes say, A is the ancestor of node B and B is the descendant of A if A is either the father of B or the father of some ancestor of B. Two nodes having same father are called brothers or Siblings. Going from leaves to root is called climbing the tree & going from root to leaves is called descending the tree. The no. of children a node has is called its degree. The level of root is 0 & the level of any node is one more than its father. In the strictly binary tree shown above a is at level 0, b & c at level 1, d & e at level 2 & f & g at level 3. The depth of a binary tree is the length of the longest path from the root to any leaf. In the above tree, depth is 3. Strictly binary tree Page 4

A binary tree is called strictly binary tree if every non-leaf node in the tree has nonempty left and right subtrees i.e., every non-leaf node has two children. The tree shown below is a strictly binary tree.

Complete binary tree  A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

 A binary tree in which every level (depth), except possibly the deepest, is completely filled. At depth n, the height of the tree, all nodes must be as far left as possible. BINARY TREE REPRESENTATION Page 5

There are two ways by which we can represent a binary tree.  Array representation of a binary tree  Linked representation of a binary tree Linked List Representation of Binary tree The structure of each node of a binary tree contains one data field and two pointers, each for the right & left child. Each child being a node has also the same structure. The structure of a node is shown below.

Binary left child and the right child. If any node has its left or right child empty trees can be represented by links where each node contains the address of the then it will have in its respective link field, a NULL value. A leaf node has NULL value in both of its links. This structure can be defined as follows: struct tnode { struct tnode *left; int data; struct tnode *right; }; Linked List Implementation of Binary Tree: typedef struct TreeNode *PtrToNode; typedef struct TreeNode *Tree; struct TreeNode { Page 6

ElementType Element; Tree Left; Tree Right; }; Pictorial Represention of a Binary Tree: Address of the Data Element

Address of the

Left Child

Right Child

Representation of a Leaf: Data Element

Note: Addresses of the Left Child and Right child are NULL. Consider the binary tree given below A

B C

D

E

F

F

Linked List Implementation for the above tree is given below:



 A

Page 7







B

D

 C

E

F

G

Binary Tree: Array Representation Binary trees can also be stored in arrays and if the tree is a complete binary tree, this method wastes no space. In this compact arrangement, if a node has an index i,  Its children are found at indices 2i + 1(for the left child) and 2i + 2(for the right), while its parent

(if any) is found at index (assuming

the root has index zero). This method benefits from more compact storage and better locality of reference, particularly during a preorder traversal.

Binary Tree in an array TREE TRAVERSALS Traversing is the process of visiting every node in the tree exactly once. Therefore, a complete traversal of a binary tree implies visiting the nodes of the tree in some linear sequence.

Page 8

In a linear list nodes are visited from first to last, but a tree being a nonlinear one we need definite rules. There is no. of ways to traverse a tree. All of them differ only in the order in which they visit the nodes. The three main methods of traversing a tree are  Postorder traversal strategy  Preorder traversal strategy  Inorder traversal strategy

Preorder traversal (depth-first order) 1.

Visit the root

2.

Traverse the left subtree in preorder.

3.

Traverse the right subtree in preorder.

Postorder traversal 1.

Traverse the left subtree in postorder

2.

Traverse the right subtree in postorder

3.

Visit the root

Inorder traversal (symmetric order) 1.

Traverse the left subtree in inorder

2.

Visit the root

3.

Traverse the right subtree in inorder

Example: A

B

C

Left subtree

Page 9 D

E

F

Right subtree

Preorder Traversal: ABDECF  First, visit the Root A  Now visit the left subtree in preorder. .For left subtree, B is the root. So visit root B, then left of B and then visit right of B. It gives the traversing order BDE.  Start visiting the right subtree of A in preorder. For right subtree, C is the root. So visit the root C, then left of C (In the given example there is no left of C), and then visit right of C. Now the order is CF.  Finally, the preorder traversal of above tree is ABDECF. Inorder traversal: DBEACF  First visit the left subtree in inorder. For left subtree, B is the root. So visit left of B, then root and then visit right of B. It gives the traversing order DBE.  Now visit the Root A.  Start visiting the right subtree of A in inorder. For right subtree, C is the root. So visit left of C (In the given example there is no left of C), then root C and then visit right of C. Now the order is CF.  Finally, the inorder traversal of above tree is DBEACF Postorder Traversal: DEBFCA  First visit the left subtree in postorder. For left subtree, B is the root. So visit the left of B, then visit right of B and then visit root B. It gives the traversing order DEB.

Page 10

 Start visiting the right subtree of A in postorder. For right subtree, C is the root. So left of C (In the given example there is no left of C), then visit right of C and then visit root C. Now the order is FC.  Now visit the Root A  Finally, the postorder traversal of above tree is DEBFCA. Example:

+ -

H

/

*

+ A

* B

C

-

E D

F

G

For the example given below, the traversals are given below: Preorder: +-/+AB*CD*E-FGH Inorder : A+B/C*D-E*F-G+H Postorder: AB+CD*/EFG-*-H+ Structure Declaration struct tree_node { tree_node *left; tree_node *right; int data; }; Insert Routine Page 11

void insert(int item) { tree_node *p=new tree_node; tree_node *parent; p->data=item; p->left=NULL; p->right=NULL; parent=NULL; if(isempty()) root=p; else { tree_node *ptr; ptr=root; while(ptr!=NULL) { parent=ptr; if(item>ptr->data) ptr=ptr->right; else ptr=ptr->left; } if(itemdata) parent->left=p; else parent->right=p; } } Inorder Traversal routine void inorder(tree_node *ptr) { if(ptr!=NULL) { inorder(ptr->left); cout<<" "<data<<" "; inorder(ptr->right); } } Postorder Traversal routine void postorder(tree_node *ptr) Page 12

{ if(ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); cout<<" "<data<<" "; } } Preorder Traversal routine void preorder(tree_node *ptr) { if(ptr!=NULL) { cout<<" "<data<<" "; preorder(ptr->left); preorder(ptr->right); } } APPLICATIONS OF TREES: There are many applications for trees. One of the popular uses is the directory structure in many common operating systems, including UNIX and DOS.  The root of this directory is /usr.  /usr has 3 children namely mark, alex and bill, which are directories.  ./usr contains 3 directories and no regular files.  The filename /usr/mark/book/ch1.r is obtained by following the leftmost child 3 times.  Each / after the first name indicates an edge; the result is the full pathname. Advantages of Hierarchical File System: 1. It allows users to organize the data logically. Page 13

2. Two files in different directories can share the same name, because their path is different from the root. Routine to list a directory in a hierarchical file system: Static void listdir(directoryorfile D, int depth) { if(D is a legitimate entry) { printname(D, depth); if(D is a directory) for each child, c, of D listdir(C, depth+1); }

}

Explanation:  This is executed by a recursive function call listdir.  This procedure starts with the directory name and the depth 0. (Note: The depth of the root is 0).  D can be either a directory or a file.  It prints entry of D.  If D is a Directory, it prints the children C recursively.  This procedure terminates when the parameter for listdir is a not a valid parameter. Routine which call the listdir routine: void listdirectory(directoryorfile D) { listdir(D,0); } Explanation: Page 14

 This routine call listdir with two parameters D and 0, where 0 denotes the depth of the root. Application of Binary Tree Using binary tree, one can create expression trees. The tree is binary because the operators are binary (i.e an operator is used to evaluate only two operands at a time). Expression Tree Expression tree is also a binary tree in which the leaf or terminal nodes are operands and non-terminal or intermediate nodes are operators used for traversal. The operands are such as constants, variable names and the operators are symbols.

To construct an expression tree the following steps are to be followed  Read out the given expression one symbol at a time.  If the symbol is an operand, create a one-node tree and push a pointer to it onto a stack.  If the symbol is an operator, pop pointers to two trees T1 and T2 from the stack and form a new whose root is the operator and whose left and right children point to T2 and T1respectively.  A pointer to this new tree is then pushed onto the stack. Page 15

[Note: Expression tree is constructed for a post-fix expression. Any Infix Expression is to be converted into postfix expression for the construction of expression tree.]

Example: Input: ab+cde+** 1. The two symbols are operands, so create one-node trees and push pointers to them onto a stack.

1

2

2. Next + is read , so two pointers to trees are popped, a new tree is formed, a new tree is formed, and a pointer to it is pushed onto the stack. 3. Now, c, d and e are read, and for each a one-node tree is created and a pointer to the corresponding tree is pushed onto the stack. 4. Now „+‟ is read, so two trees are merged. 5. Continuing, „*‟ is read, so we pop two tree pointers and form a new tree with a „*‟ as root. 6. Finally, the last symbol „*‟ is read, two trees are merged, and a pointer to the final tree is left on the stack.

Page 16

3

4

5

6

5

6

SET REPRESENTATION AND UNION-FIND OPERATIONS The Disjoint Set ADT: A disjoint set data structure is a data structure that contains the partitioned sets. These partitioned sets are separate non overlapping sets. Example: Consider a set A = { S1,S2,….,Sk } in which each member Si represents the distinct set. These sets are called dynamic sets. Two operations are performed on this data structure. They are: 1. Union operation – It combines two sets into a single set.

Page 17

2. Find Operation – It finds a particular element from the disjoint set. Dynamic Equivalence Problem: The dynamic equivalence problem is to decide for any a and b if a ~ b. To solve this problem, the following strategy is adopted.  The input is initially a collection of n sets. For Example, S = ({1}, {2}, {3}, {4}, {5}).  S1  S2 = , S2  S3 = , S1  S3 =  and so on. It implies that the sets are disjoint.  There are two possible operations on set S. They are: Union & Find. The Union operation is used to add relation and find operation returns name of the set containing the element.  If we want to have an equivalence relation between two elements a and b, then find a and b.  Then using union operation, we can add relationship of a with b. This creates a new set of a ~ b by destroying old disjoint sets.  By repeatedly performing union and find operations, newer sets are created by destroying old sets. Hence this algorithm is called dynamic. Example: S = ({1}, {2}, {3}, {4}, {51}) Find {1} and {2} and Union (1, 2) S = ({1, 2}, {3}, {4}, {5)) Find {4} and {5} and Union (4, 5) S = ({1, 2}, {3}, {4, 5}) Find {3} and {5} and Union (3, 5) S = ({1, 2}, {3, 4, 5}) Find {1} and {5} and Union (1, 5) Page 18

S = ( {1, 2, 3, 4, 5}) There are two ways by which Union & Find in Disjoint Sets can be performed better. They are: 1. Smart Union Algorithm 2. Path Compression Method. 1. Smart Union Algorithm: In this method, the smaller tree is attached to the root of the larger tree while performing union operation.

To find out which tree is larger a simple heuristic called rank is applied. If two trees have the same rank then the resultant tree will have rank + 1.

Page 19

Using smart union algorithm, union operation can be performed by three ways i. Arbitrary Union ii. Union by size iii. Union by Height Arbitrary Union This can be performed by making the second tree as the subtree of the first.

Initial Array

Page 20

0 P[1]

0 p[2]

0 p[3]

0 p[4]

0 p[5]

0 p[6]

0 p[7]

Tree representation of disjoint set ADT after Union (2,3) and (6,7)

2

1

5

4

6

7

3

0

0

2

0

0

P[1]

p[2]

p[3]

p[4]

p[5]

0

6

p[6]

p[7]

Tree representation of disjoint set ADT after Union (2,6)

2

1

3

5

4

6

7

0

0

2

P[1]

p[2]

p[3]

0 p[4]

0

2

p[5]

p[6] p[7]

6

Union by SIZE This can be performed by making the smaller tree a subtree of larger Example:

Initial Array Page 21

-1 P[1]

-1 p[2]

-1 p[3]

-1 p[4]

-1 p[5]

-1 p[6]

-1 p[7]

Union (3,4) and (5,6) 1

2

For union (3,4)

5

3

7 6

4

Formula: P[T1]=P[T1]+P[T2] P[T2]=T1 Here T1=3 and T2=4

-1 P[1]

-1

-2

3

-1

-1

-1

p[2]

p[3]

p[4]

p[5]

p[6]

p[7]

-1

-2

3

-2

5

-1

p[2]

p[3]

p[4]

p[5]

p[6]

p[7]

By applying the formula P[T1]=P[T1]+P[T2] P[3]=P[3]+P[4] = (-1)+(-1) = -2 P[T2]=T1 P[4]=3 For union (5,6) Here T1=5 and T2=6

-1 P[1]

By applying the formula P[T1]=P[T1]+P[T2] P[5]=P[5]+P[6] Page 22

= (-1)+(-1) = -2 P[T2]=T1 P[6]=5

For Union (3,5) Here T1=3 and T2=5 By applying the formula P[T1]=P[T1]+P[T2]

-1

-1

P[1]

p[2]

-1

3

-4

3

3

p[3]

p[4]

p[5]

5

-1

p[6]

p[7]

P[3]=P[3]+P[5] = (-2)+(-2) = -4 P[T2]=T1 P[5]=3 For Union (2,3) Since 2 gets attached with 3 Here T1=3 and T2=2

P[1]

p[2]

-5 p[3]

3 p[4]

3 p[5]

5 p[6]

-1 p[7]

By applying the formula P[T1]=P[T1]+P[T2] P[3]=P[3]+P[2] = (-4)+(-1) = -5 P[T2]=T1 P[2]=3 Page 23

2. Path Compression: In this method, each visited node from root will be directly attached to the root node so that the path length of the disjoint set tree will be reduced.

Routine for FIND operation

GRAPHS Page 24

Definition: A Graph is a collection of two sets V & E where V is a finite non empty set of vertices and E is a finite non empty set of edges. Vertices are the nodes in the graph. The two adjacent vertices are joined by edges. Thus a graph is a set of two sets. Any graph G is denoted by G = {V, E } Example:

V = { V1,V2,V3,V4} E = {E1, E2, E3, E4 } Types of Graphs: Graphs are classified into two types. They are: i. Directed Graph. ii. Undirected Graph.

i. Directed Graph: The directions are shown on the edges. Consider the graph given below: E1

V1

E2 V2

V3

E3

V4

E4

In the above graph, the edge E1 is in between the vertices V and V. The V1 is called the head and V2 is called the tail. It is also referred as digraph. Page 25

ii. Undirected Graph: In this type of graph, the edges are not directed.

In this type of graph the edge E is

Graph Terminologies: Weighted Graph: A Graph is said to be weighted graph if every edge in the graph is assigned a weight or value. Unweighted Graph: A Graph is said to be unweighted graph if the edge in the graph is not assigned a weight or value Complete graph: If an undirected graph of n vertices consists of n(n-1)/2 number of edges then it is called as complete graph. The graph shown below is a complete graph.

Sub graph: A Sub graph G‟ of graph G is a graph such that the set of vertices and set of edges of G‟ are proper subset of the set of edges of G.

Page 26

Connected graph: An undirected graph is said to be connected if for every pair of distinct vertices Vi and Vj in V(G) there is a path from Vi to Vj in G.

A directed graph with this property is called as strongly connected. If a directed graph is not strongly connected, but the underlying graph is connected, then the graph is said to be weekly connected. Adjacency: Vertex w is adjacent to v if and only if (v, w)  E. Length of a path: A path is a sequence of vertices w1, w2, w3, ..., wN, such that (wi, wi+1)  E for 1 < i < N. The length of a path is the number of edges on the path, which is N-1. A simple path is one such that all vertices are distinct, except that the first and the last could be the same. Cycle: A cycle in a directed graph is a path of length at least 1 such that w1 = wN. Acyclic Graph: A directed graph is acyclic if it has no cycles (abbreviated as DAG). Page 27

Indegree: The indegree of a vertex v is the number of edges entering into the vertex v. Outdegree: The outdegree of a vertex v is the number of edges leaving the vertex v. Example: Consider the graph given below:

The indegree of a is 0. The outdegree of a is 2. Representation of Graphs The two commonly used representations of Graphs are: 1. Adjacency Matrix 2. Adjacency Lists 1. Adjacency Matrix Consider a graph C of n vertices and the matrix M. If there is an edge present between vertices Vi and Vj, then M[i][j] = 1 else M[i][j] = 0. For an undirected graph, M[i][j] = M[i][j = 1. Example:

Page 28

Creating a Graph using Adjacency Matrix Creation of graph using adjacency matrix is a simple task. The adjacency matrix is nothing but a two dimensional array. The algorithm for creation of graph using adjacency matrix is given below: 1) Declare an array of M [size][size] which stores the graph. 2) Enter the no. of nodes in the graph. 3) Enter the edges of the graph by two vertices each, say Vi ,Vj, which indicates the edge. 4) If the graph is directed set M [i][j]= 1. If graph is undirected set M[i][j] =M[i][j]=1. 5) When all the edges for the desired graph is entered print the graph M[i][j]. 2. Adjacency List The type in which a graph is created with the linked list is called adjacency list. So the advantages of linked list can be obtained in this type of graph. We need not have a prior knowledge of maximum number of nodes. Construction of Adjacency List: Since graph is a set of vertices and edges, we will maintain the two structures, for vertices and edges respectively. Consider the graph given below. It has vertices a,b,c,d & e.

Page 29

The linked list representation of the head nodes and the adjacent nodes are given below. struct head { char data; struct head *down; struct head *next; } Graph Traversal: There are two types of Graph Traversals. They are: 1. Breadth First Traversal also called as Breadth First Search (BFS) 2. Depth First Traversal also called as Depth First Search (DFS) 1. Breadth First Traversal also called as Breadth First Search (BFS) In the BFS, a vertex V is taken visited first. Then the adjacent vertices of V are visited. The same procedure is performed for all the vertices in the graph. Algorithm: 1. Create a graph. 2. Read the vertex from which you want to traverse the graph say Vi. 3. Initialize the visited array to 1 at the index of Vi 4. Insert the visited vertex Vi in the queue

Page 30

5. Visit the vertex which is at the front of the queue. Delete it from the queue and place its adjacent nodes in the queue. 6. Repeat the step 5, till the queue is not empty 7. Stop. Explanation of logic of BFS

In BFS the queue is maintained for storing the adjacent nodes and an array “visited” is maintained for keeping the track of visited nodes. i.e. once a particular t is visited it should not be revisited again.

Page 31

Increment front, delete the node from Queue and print it.

Page 32

So output will be - BFS for above graph as 1

2

3

4

Routine:

2. Depth First Traversal also called as Depth First Search (DFS) In this method, we follow the path as deeply as we can go. When there is no adjacent vertex present we traverse back and search for unvisited vertex. We will maintain a visited array to mark all the visited vertices. In case of DFS the depth of a graph should be known. Algorithm: Step 1: Choose any node in the graph. Designate it as the search node and mark it as visited. Step 2: Using the adjacency matrix of the graph, find a node adjacent to the search node that has not been visited yet. Designate this as the new search node and mark it as visited. Page 33

Step 3: Repeat Step 2 using the new search node. If no node satisfying Step 2 can be found, return to the previous search node and continue. Step 4: When a return to the previous node in step 3 is impossible, the search from the originally chosen search node is complete. Step 5: If the graph still contains unvisited nodes, choose any node that has not been visited and repeat Step 1 through Step 4. Example:

In DFS the basic data structure for storing the adjacent vertices is stack. Step 1: Start with vertex 1, print it so „1‟ gets printed. Mark I as visited.

Page 34

Routine for DFS: Void DFS(Vertex V) { visisted[V]=True; for each W adjacent to V if(!visisted[W]) DFS(W); } CONNECTED COMPONENTS Page 35

Definition: An undirected graph is called connected if there is a path between every pair of distinct vertices in the graph. For example, any two computers in a network can communicate if and only if the graph of this network is connected. Note: A graph consisting of only one vertex is always connected, because it does not contain any pair of distinct vertices. Example:

Example: What are the connected components in the following graph?

Solution: The connected components are the graphs with vertices {a, b, c, d}, {e}, {f}, {f, g, h, j}. The graph is strongly connected, because there are paths between all possible pairs of vertices. Page 36

Biconnectivity A connected undirected graph is biconnected if there are no vertices whose removal disconnects the rest of the graph. If a graph is not biconnected, the vertices whose removal would disconnect the graph are known as articulation points. These nodes are critical in many applications. The graph given below is not biconnected. C and D are articulation points. The removal of C would disconnect G, and the removal of D would disconnect E and F from the rest of the graph.

Consider the Graph given below:

Page 37

PART A Question and Answers 1. Define tree. (Or) What is a tree? Trees are non-liner data structure, which is used to store data items in a shorted sequence. It represents any hierarchical relationship between any data Item. It is a collection of nodes, which has a distinguish node called the root and zero or more non-

Page 38

empty sub trees T1, T2,….Tk. each of which are connected by a directed edge from the root. 2. Define Height of tree. The height of n is the length of the longest path from root to a leaf. Thus all leaves have height zero. The height of a tree is equal to a height of a root. 3. Define Depth of tree. For any node n, the depth of n is the length of the unique path from the root to node n. Thus for a root the depth is always zero. 4. Define Degree of a node. It is the number of sub trees of a node in a given tree. 5. Define Degree of a tree. It is the maximum degree of a node in a given tree. 6. Define Terminal node or leaf? Nodes with no children are known as leaves. A leaf will always have degree zero and is also called as terminal node. 7. Define Non-terminal node? Any node except the root node whose degree is a non-zero value is called as a non-terminal node. Non-terminal nodes are the intermediate nodes in traversing the given tree from its root node to the terminal node. 8. Define sibling? Nodes with the same parent are called siblings. 9. Define binary tree? (NOV/DEC 2011)(NOV/DEC 2013) A Binary tree is a finite set of data items which is either empty or consists of a single item called root and two disjoint binary trees called left sub tree max degree of any node is two.

10. What are the postfix and prefix forms of the expression? A+B*(C-D) / (P-R) Postfix form: ABCD – * PR – / + Prefix form: + A / * B – CD - PR 11. Define expression tree?

Page 39

Expression tree is also a binary tree in which the leaf nodes or terminal nodes are operands and non-terminal or intermediate nodes are operators used. 12. Define Construction of expression trees 1. Convert the given infix expression into postfix notation 2. Create a stack and read each character of the expression and push into the stack, if operands are encountered. 3. When an operator is encountered pop 2 values from the stack. 13. Define lazy deletion? When an element is to be deleted it is left in the tree itself and marked as being deleted. This is called as lazy deletion and is an efficient procedure if duplicate keys are present in the binary search tree, because the field that keeps count of the frequency of appearance of the element can be decremented of the element can be decremented. 14. Define tree traversal and mention the type of traversals? Visiting of each and every node in the tree exactly is called as tree traversal. Three types of tree traversal 1. Inorder traversal 2. Preoder traversal 3. Postorder traversal. 15. What are the various operation performed in the binary search tree? 1. Insertion 2. Deletion3. Find 4. Find min 5. Find max 16. What is a graph? A graph consists of a set of vertices V and set of edges E which is mathematically represented as G = (V,E). Each edge in a pair (V,W) where V,W belongs to E ,edges are sometimes referred to as arcs. 17. Give the types of representation of graphs or How a Graph can be represented? The two commonly used representations of Graphs are: 1. Adjacency Matrix 2. Adjacency Lists 18. Explain about Adjacency Matrix (NOV/DEC 2010) Adjacency matrix consists of a n*n matrix where n is the no. of vertices present. In the graph, which consists of values either r0 or 1. 19. Explain about Adjacency linked list. Page 40

It consists of a table with the no. of entries for each vertex for each entry a Linked List is initiated for the vertices adjacent to the corresponding table entry. 20. What are Directed graphs? If a pair of vertices for any edge is ordered, then that graph is called as Digraph or directed graph. 21. Define Path. A path in a graph is a sequence of vertices w1,w2, w3,wN such that wi,wi+1 belongs to E for a value 1<=I<=N. The length of such a path is the number of edges on the path, which is equal to n-1. 22. Define Cycle. A cycle is a path in which the first and last vertices are the same. 23. Define Acyclic graph. A graph with no cycles is called Acyclic graph. A directed graph with no Edges is called as a directed acyclic graph (or) DAG. DAGS are used for Compiler Optimization process. 24. Define Connected graph. A graph is said to be a weighted graph is connected if there is a path from every vertex to every other vertex. A directed graph with this property is called as strongly connected graph. 25. What are the conditions for a graph to become a tree? A graph is a tree if it has two properties. i.

If it is a connected graph.

ii.

There should not be any cycles in the graph.

26. Define a Weighted Graph If every edge in the graph is assigned some weight or value. The weight of the edge is a positive value that represents the cost of moving the edge or the distance between two vertices. 27. State the properties of binary search tree. (NOV/DEC 2010) a. The left subtree of a node contains only nodes with keys less than the node's key. b. The right subtree of a node contains only nodes with keys greater than the node's key. c. Both the left and right subtrees must also be binary search trees. Page 41

28. How many trees are possible with 3 nodes? (APRIL/MAY 2010) Maximum number of trees with „n‟ nodes=2n-n. Maximum number of nodes in a tree =23-3 =8-3 =6 29. When does a graph become a tree? (NOV /DEC 2009) A minimum spanning tree of an undirected graph G is a tree formed graph edges that connects all the vertices of G at a lowest cost. A minimum spanning tree exists if and only if g is connected. An acyclic graph (also known as a forest) is a graph with no cycles. A tree is a connected acyclic graph. Thus each component of a forest is tree, and any tree is a connected forest. 30. What is a minimum spanning tree? (NOV/DEC 2012) (APRIL/MAY 2011) A minimum spanning tree of an undirected graph G is a tree formed from graph edges that connect all the vertices of G at lowest total cost. 31. What is a single source shortest path problem? Given as an input, a weighted graph, G = (V,E) and a distinguished vertex „S as the source vertex. Single source shortest path problem finds the shortest weighted path from s to every other vertex in G. 32. Explain about Unweighted shortest path. Single source shortest path finds the shortest path from the source to each and every vertex present in a unweighted graph .Here no cost is associated with the edges connecting the vertices. Always unit cost is associated with each edge.

33. Explain about Weighted shortest path Single source shortest path finds the shortest path from the source to each and every vertex present in a weighted graph. In a weighted graph some cost is always associated with the edges connecting the vertices. 34. What are the methods to solve minimum spanning tree? a) Prims algorithm b) Kruskal‟s algorithm 35. What is degree of graph? (NOV/DEC 2011) The degree of the graph will be its largest vertex degree. Page 42

36. When the tree is called complete binary tree? The tree is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right. 37. When the tree is called Complete Binary Tree? (NOV/DEC 2012) When a complete binary tree is built, its first node must be the root. The second node of a complete binary tree is always the left child of the root... The third node is always the right child of the root. The next nodes must always fill the next level from left to right. 38. Write short notes on connected components. (NOV/DEC 2014) An undirected graph is called connected if there is a path between every pair of distinct vertices in the graph. For example, any two computers in a network can communicate if and only if the graph of this network is connected. A graph consisting of only one vertex is always connected, because it does not contain any pair of distinct vertices. Example:

39. Give the representation of network of cities (Chennai, Delhi, Kolcutta and Mumbai) as weighted graph. (NOV/DEC 2014) Define Disjoint Set ADT. A disjoint data structure is a data structure that contains the partitioned set. These partitioned sets are separate non overlapping set. Example: Consider a set s= {S1, S2, S3,…, Sk } in which each members Si represents the distinct set. These sets are colled dynamic sets. There are two operations performed in the Set. They areL a. Find b. Union. Page 43

17. What is meant by equivalence relation? An equivalence relation is a relation R that satisfies three properties: c. Reflexive – a R a, for all a € S. d. Symmetric – a R b, if and only if b R a. e. Transitive – a R b and b R c implies that a R c. 18. What is Disjoint-set Forest? The disjoint –set forest is a collection of disjoint trees in which each node holds a reference to its parent node. Example: 5 5

10

10 7

20

8

7

8 20

30

30

Disjoint Trees

Disjoint-Set Forest

19. What is smart union algorithm? In the smart Union algorithm, while making the union of two trees, the smaller tree is attached to the root of the larger tree. This is also referred as weighted-union heuristic. Example: 5

7

5

10

6 20

Smart Union

10

7

6

8 30 9

20

8 Page 44

30

20. What is path compression?

9

In this method, each visited node from root will be directly attached to the root node so that the path length of the disjoint set tree will be reduced. This method will help to quickly find the desired node from the set. Example: 5

5 Path compression

6

7

6

7

8

9

10

8

9

21. What are the various applications of Disjoint-Set data Structure? Disjoint-set data structure is useful in determining the connected components of undirected graph. They are used to find whether two vertices belong to the same component or not. Disjoint-set data structure is useful in the file transfer from one computer to another in a network of computers.

Page 45

PART B 1. Explain depth first search on a graph with necessary data structures. (8) (APRIL/MAY 2008) 2. Write an algorithm to find the minimum cost spanning tree of an undirected weighted graph. (8) (APRIL/MAY 2007) 3. Explain Depth – First & Breadth – First Traversal algorithms. 4. Discuss

the

different

methods

of

traversing

a

binary

tree

with

algorithm.(NOV/DEC 2009) (APRIL/MAY 2010) 5. Convert the expression ((A+B)*C-(D-E) ^ (F+G)) to equivalent Prefix and postfix notations. (APRIL/MAY 2011) 6. Explain breadth first search algorithm for the traversal of any graph with suitable examples. Define time complexity of the algorithm. (NOV/DEC 2010) (MAY/JUNE 2013) 7. Explain DFS and BFS with suitable example (16) (Nov/Dec 2014) 8. Write C++ code for the implementation of different types of tree traversals. State few tree applications.(16) (Nov/Dec 2014) 9. Discuss the different methods of traversing a binary tree with algorithm (16) 10. Illustrate the Depth First Search algorithm with a graph and explain(16)

****ALL THE BEST****

Page 46

unit4.pdf

Page 1 of 46. Page 1. UNIT IV NON-LINEAR DATA STRUCTURES. Trees – Binary Trees – Binary tree representation and traversals –. Application of trees: Set ...

2MB Sizes 2 Downloads 116 Views

Recommend Documents

No documents