Sei sulla pagina 1di 13

2008 PAPER

Q1 Sol-The memory of a computer is linear and not matrixes like a 2D array. So, the
elements of the array are stored either by row, called "row-major", or by column, called "column-major". Row-major order is used most notably in C and C++ during static declaration of arrays. In C, since the length of each row is always known, the memory can be filled row one row at a time, one after the other. Example: a[i][j] = 1 2 3 4 5 6 7 8 9 Representation in the memory: In row-major: 1 2 3 4 5 6 7 8 9 In column-major: 1 4 7 2 5 8 3 6 9

Q2 Sol-Queue is an ordered list of elements in which we can add elements only at one
end, called the rear of the queue and delete element only at the other end called front of the queue. We can see in the queue of cars that the people or car that comes first in the queue will be out first. Its behavior seems to be first in first out. So queue is also called FIFO data structure.

Q3
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.

Full

Binary Tree - A full binary tree (sometimes proper binary tree or 2tree or strictly binary tree) is a tree in which every node other than the leaves has two children.

Degree of a tree - A binary tree is a tree in which all nodes are connected and each node has in degree of maximum and out degree of maximum 2 Height of a tree - For a tree with just one node, the root node, the height is defined to be 0, if there are 2 levels of nodes the height is 1 and so on. A null tree (no nodes except the null node) is defined to have a height of 1.

Ancestor of a node - Parent, parent of parent, etc.

Q4E= (a-b)/((c*d)+e) Sol Q5


B-tree: proposed by Bayer 1972 A B-tree operates and McCreight

closely with

secondary storage and can be tuned to reduce the impediments imposed by this storage One important property of B-trees is the size of each node which can be made as large as the size of the block. (the basic unit of I/O operations associated with a disk is a block) a B-tree of order t is a multiway search tree. A B-tree is not a binary tree because B-tree has many more than two children A B-tree is balanced. Every leaf in a B-tree has the same depth

Unit 1
Q2(a)

Sol-Matrices, which have more zero entries, are called as sparse matrices. Matrix has
presented in row and column only. So it can be presented in two dimensional arrays. Suppose a matrix has many zero entries and we have work only with non zero entries. So its wastage of memory. Now we want to save the memory space which every zero entry is using. So alternate method is to save only non-zero entries which can be through sparse matrix.

0 3 0

1 5 0

0 0 2 is a sparse matrix

(b)

Sol-In normal algebra we use the infix notation like a+b*c. The corresponding postfix
notation is abc*+. The algorithm for the conversion is as follows:
 Scan the Postfix string from left to right.  Initialize an empty stack.  If the scanned character is an operand, add it to the stack. If the scanned 

character is an operator, there will be at least two operands in the stack. If the scanned character is an Operator, then we store the top most element of the stack (top Stack) in a variable temp. Pop the stack. Now evaluate top Stack (Operator) temp. Let the result of this operation be retVal. Pop the stack and Push retVal into the stack. Repeat this step till all the characters are scanned.

After all characters are scanned, we will have only one element in the stack. Return topStack.

(c)Sol-void addToQueue(int temp, QUEUE *q) {


if(!(isQueueFull(q))) { q->elt[++q->rear] = temp; q->count++; } else { printf("Queue is Full"); return; // returning a control not a value } }

Q3 (a+b)/(c*d+e) Sol-In computer science, a stack is a last in, first out (LIFO) abstract data type and data
structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top. The push operation adds a new item to the top of the stack, or initializing the stack if it is empty, but if the stack is full and does not contain more space to accept the given item it is considered as an Overflow state (It means that the stack is overloaded or no more space for new item). The pop operation removes an item from the top of the stack, A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes under underflow state (It means no items are present in stack to be removed). The stack top operation removes the data from top most position without deleting it and returns it to user, the same underflow state can also occur in stack top operation if stack is empty. In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows:
 Scan the Infix string from left to right.  Initialize an empty stack.  If the scanned character is an operand, add it to the Postfix string. If the

scanned character is an operator and if the stack is empty PUSH the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (top Stack). If top Stack has higher precedence over the scanned character Popthe stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and top Stack has precedence over the character. Repeat this step till all the characters are scanned.

(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.  Return the Postfix string.


Postfix expression:- ab+cd*e+/

Unit 2
Q4
Sol-void delete_node(node *list, int n) { node *p; p=list; int i=1; while(i<n-1) { p=p->next; ++i; } node *q; q=p->next; p->next=q->next; free(q); }

(b) Sol-In computer science, a doubly linked list is a linked data structure that consists of
a set of sequentially linked recordscalled nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.

A doubly linked list whose nodes contain three fields: an integer value, the link to the next node, and the link to the previous node. The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified. void del() {node *ptr=new Node; assert(ptr != 0); ptr=current->succ; current->pred->succ=current->succ; current->succ->pred=current->pred; delete current; current=ptr;}

(c )
Ans-list_ traversal(tree_ tree, list_ inorder) { if(IsTreeEmpty(tree)) { returninorder; } else { return traversal(LeftTree(tree), MakeList(TreeNode(tree), inorder); } }

Q5- (a)

Sol-To search for an


element in a linked list, you iterate the list, looking for the element, and either return the element or an indication that it was not found. for (ptr = first; ptr != null; ptr = ptr.next) if (ptr.value == searchvalue) break; This will either leave ptr with the address of the found element, or null, if not found.

(b) Sol-Inorder : D B H E I A F C G J
Preorder : A B D E H I C F G J Postorder : D H I E B F J G C A

2010 PAPER

1.(a) Matrices, which have zero entries are called as sparse matrices. Matrix has presentation in row and column only. So it can be presented in two dimensional array.

(b) Main tree use are 1- searching. 2-reduce the length through the Huffman tree. (c) Stack is used to perform recursion. (e) 21 nodes are there in a binary tree of 20 nodes. (f) Structure is used in linked list. (g)A linear search works by looking at each element in a list of data until it either finds the target or reaches the end. This results in O(n) performance on a given list. A binary search comes with the prerequisite that the data must be sorted. We can leverage this information to decrease the number of items we need to look at to find our target. We know that if we look at a random item in the data (let's say the middle item) and that item is greater than our target, then all items to the right of that item will also be greater than our target. This means that we only need to look at the left part of the data. Basically, each time we search for the target and miss, we can eliminate half of the remaining items. This gives us a nice O(log n) time complexity. (h)int count (const Tree *t) { int sum; if (!t) return 0; else sum=1;

if (t->left) sum += count (t->left); if (t->right) sum += count (t->right); return sum; } advanced: int count (const Tree *t) { int sum= 0; while (t) { sum++; if (t->left) { if (t->right) sum += count (t->right); t = t->left; } else t = t->right; } return sum; } Q2Circular queue have less memory consuption as comared to linear queue because while doing insertion after deletion operation it allocate an extra space the first remaining vacant but in circular queue the first is used as it comes immediate after the last. Q3(a)linked list consists of data nodes, each pointing to the next in the list. An array consists of contiguous chunks memory of predetermined size Linked lists are quite flexible. They can grow to any size -- up to resource limits -- and each node can be a different size. Memory allocation for a node is done as needed, usually when a new node is added Linked lists can be circular, or doubly-linked (pointers forward and backward), and new nodes can be inserted anywhere in the chain. The greatest downside to linked lists is sequential access: to find any node in memory, each previous node must be examined and the pointers followed. Linked lists are also more complex to program and manage. Arrays are fixed in size, so resources must be anticipated and consumed in advance. Each element is the same size and must contain the same data type. But access to a particular element is very fast, because its location in memory can be determined mathematically and accessed directly. (b)#include <stdio.h> #include <conio.h> #include <alloc.h>

struct node { int data ; struct node *link ; };

void addatend ( struct node **, int ) ; void display ( struct node * ) ;

void main( ) { struct node *p ;

p = NULL ;

addatend ( &p, 1 ) ; addatend ( &p, 2 ) ; addatend ( &p, 3 ) ; addatend ( &p, 4 ) ; addatend ( &p, 5 ) ; addatend ( &p, 6 ) ; addatend ( &p, 10 ) ;

clrscr( ) ; display ( p ) ; }

/* adds a new node at the end of the linked list */ void addatend ( struct node **s, intnum ) { if ( *s == NULL ) { *s = malloc ( sizeof ( struct node ) ) ;

( *s ) -> data = num ; ( *s ) -> link = NULL ; } else addatend ( &( ( *s ) -> link ), num ) ; }

/* displays the contents of the linked list */ void display ( struct node *q ) { printf ( "\n" ) ;

/* traverse the entire linked list */ while ( q != NULL ) { printf ( "%d ", q -> data ) ; q = q -> link ; } } Q4. B-tree is a tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic amortized time. The B-tree is a generalization of a binary search tree in that a node can have more than two children

Q5. (a)#include<stdio.h> int main(){ int s,i,j,temp,a[20]; printf("\nEnter size of the array :"); scanf("%d",&s); printf("\nEnter %d elements in to the array:"); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=0;i<s;i++){ for(j=i+1;j<s;j++){ if(a[i]>a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } }

} printf("\nThe array after sorting is: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; }

(b)#include "stdio.h" void main( ) { intarr[5] = { 25, 17, 31, 13, 2 } ; int i, j, k, temp ; printf ( "Insertion sort.\n" ) ; printf ( "\nArray before sorting:\n") ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%d\t", arr[i] ) ; for ( i = 1 ; i <= 4 ; i++ ) { for ( j = 0 ; j < i ; j++ ) { if ( arr[j] >arr[i] ) { temp = arr[j] ; arr[j] = arr[i] ; for ( k = i ; k > j ; k-- ) arr[k] = arr[k - 1] ; arr[k + 1] = temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%d\t", arr[i] ) ;

Potrebbero piacerti anche