Professional Documents
Culture Documents
#include<conio.h>
#include<stdio.h>
void main()
{ int i,j,a[10],n,temp;
clrscr();
scanf("NO. of elements: %d ",&n);
for (i=0;i<n;i++) scanf(“%d”,&a[i]);
clrscr();
printf("You array...\n");
for (i=0;i<n;i++) printf(&a[i]," ");
for (i=0;i<n;i++)
{ for (j=0;j<(n-1)-i;j++)
if (a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\n\nSorted Array...\n");
for (i=0;i<n;i++) cout<<a[i]<<" ";
getch();
}
2. Demonstrate with your own programming example
the usage of structures within an array.
Ans –
#include<conio.h>
#include<stdio.h>
void main()
{ struct student
{ int roll;
Char name[20];
}
} std[5]; //declaring an array of structure
Trees
3. A designated node of the set, r, is called the root of the tree: and
Binary Tree
Used to implement lists whose elements have a natural order (e.g.
numbers) and either (a) the application would like the list kept in this
order or (b) the order of elements is irrelevant to the application (e.g.
this list is implementing a set).
Hash Tables
The resulting short lists were easily sorted, and could just as easily be
searched
Ideally we would’ build a data structure for which both the insertion
and find operations are 0(1) in the worst case. However, this kind of
performance can only be achieved with complete a priori knowledge. We
need to know beforehand specifically which items are to be inserted into
the container. Unfortunately, we do not have this information in the
general case. So, if we cannot guarantee 0(1) performance in the worst
case, then we make it our design objective to achieve 0(1) performance
in the average case.
Clearly, neither the ordered list nor the sorted list meets our
performance objectives. The essential problem is that a search, either
linear or binary, is always necessary. In the ordered list, the find
operation uses a linear search to locate the item. In the sorted list, a
binary search can be used to locate the item because the data is sorted.
However, in order to keep the data sorted, insertion becomes O(n).
Ans –
· Preorder traversal
· Inorder traversal
· Postorder traversal
Preorder Traversal
Preorder traversal starts by visiting the current node – call it c,
then its left child, and then its right child. Starting with the BST’s root as
c, this algorithm can be written out as:
Step 1: Visit c. This might mean printing out the value of the node,
adding the node to a List, or something else. It depends on what you
want to accomplish by traversing the BST.
Inorder Traversal
Step 2: Visit c. This might mean printing out the value of the node,
adding the node to an ArrayList, or something else. It depends on what
you want to accomplish by traversing the BST.
Postorder Traversal
This might mean printing out the value of the node, adding the
node to an ArrayList, or something else. It depends on what you want to
accomplish by traversing the BST.
Spanning trees
Now suppose the edges of the graph have weights or lengths. The
weight of a tree is just the sum of weights of its edges. Obviously,
different trees have different lengths. The problem: how to find the
minimum length spanning tree?
On the other hand, if you draw a path tracing around the minimum
spanning tree, you trace each edge twice and visit all points, so the TSP
weight is less than twice the MST weight. Therefore this tour is within a
factor of two of optimal.
Telecommunication problem
Graph: The vertices of the graph are the computers. The edges are
the wires between the computers. Graph problem: minimum dominating
sub-graph.
Knight moves