Sei sulla pagina 1di 21

Asked Questions and Answers:

1.)Your Introduction?
2.) Your objective?
3.) Tell me something about your family? (This is their favorite question so pre
pare it well)
4.) Why HCL?
5.) If you are already placed in another company, then why HCL over that company
?
6.) What do u know about HCL?.(www.hcltech.com and PPT)
The HCL Enterprise is one of India's largest electronics, computing and
information technology company. Based in Noida, near Delhi, the company compris
es two publicly listed Indian companies, HCL Technologies and HCL Infosystems.
HCL was founded in 1976 by Shiv Nadar, Arjun Malhotra, Subhash Arora, Ajai Chowd
hry, DS Puri, & Yogesh Vaidya. HCL was focused on addressing the IT hardware mar
ket in India for the first two decades of its existence with some sporadic activ
ity in the global market.
On termination of the joint venture with HP in 1996, HCL became an enterprise wh
ich comprises HCL Technologies (to address the global IT services market) and HC
L Infosystems (to address the Indian and APAC IT hardware market). HCL has since
then operated as a holding company.
1976 - HCL (Hindustan Computers Limited) is created.
1977 - Forms distribution alliance with Toshiba for copiers and notebooks
1978 - Developed the first indigenous Microcomputer
1988 - Development of fine-grained multiprocessor Unix operating system
1986 - HCL becomes the largest IT company in India
1989 - HCL America is created with Sanmina SCI as its manufacturing partner.
1991 - Entered into a partnership with HP to form HCL HP Limited. Developed a cu
stom Multiprocessor Unix for HP
1994 - Tied up with Nokia for mobile phone distribution and Ericsson for telepho
ne switch distribution.[3]
1996 - Partnership with HP ends.
1997 - HCL's R&D division is spun off as HCL Technologies [4]
2001 - HCL BPO is created.
7.) Incidents in support of all that u have mentioned in C.V? (V.imp)
8.) How are you different than other candidates? Why should we recruit you?
9.) What s unique about you?
10.)Any plans for higher studies? (Big No!!!!)
11.) Any Problem in Relocation? (Strict no no.).
12.)Your Hobbies, Interests, Strengths and Weaknesses, some other ques from C.V.
13.) One thing that irritates you the most and why?
14.) You may need to explain your projects also.
15.) Do you want to ask something from me?
Don't forget to ask a question because it shows that you are interested
in this job. You may ask anything about company and job you are applying for.
16.Encapsulation?
Encapsulation is also known as information hiding, it is to protect data
from the client using the classes but still allowing the client to access the d
ata, but not modify it. Through a public interface, the private data can be used
by the client class without the worry of the user messing with the private data
. An example of encapsulation is declaring an instance variable private, and hav
ing an accessor method that allow access to the variable.
17.Which sort show the best average behavior?
quick sort
18.What is a datastructure?
A data structure is a specialized format for organizing and storing data
. General data structure types include the
array, the file, the record, the table, the tree, and so on. Any data structure
is designed to organize data to suit a
specific purpose so that it can be accessed and worked with in appropriate ways.
19. How would you sort a linked list?
using merge sort.
20.What is atmost complete binary tree?
An almost complete binary tree is a tree in which each node that has a r
ight child also has a left child.
Having a left child does not require a node to have a right child. Stated lterna
tely, an almost complete binary tree is a
tree where for a right child, there is always a left child, but for a left child
there may not be a right child.
The number of nodes in a binary tree can be found using this formula: n = 2^h Wh
ere n is the amount of nodes in the tree,
and h is the height of the tree.
21.what is hashing?
Hashing is a way retrieving records from memory in faster way.Record is
inserted into memory by using hash function
(division,midsqure,folding,digit analysis)and also records are retrieved using s
ame hash function.
22. What are the parts of root node?
A root node contains data part and has link part. i.e links to its child
. if it is binary tree it has two links i.e
left child and right child.
23.What does abstract data type means?
If for a particular collection of data only the structure of data and th
e functions to be performed on the data is
defined but the implementation is not defined,then such a collection of data is
called Abstrct data type.
24.What is B+ tree?
A B+ tree is a data structure in which records associated with the searc
h keys are at the leaves of the tree.
This provide efficient retrieval,insertion and removal of records.Keys are tripl
icale to the non-leaf nodes to provide a
path to the searched record.NT file system,JFS2 file system and Rationaldata bas
e often used this data structure for indices.
25.Convert the following infix expression to post fix notation ((a+2)*(b+4)) -1
a2+b4+*1-

( ( a + 2 ) * ( b + 4 ) ) - 1
\ / /
a2+ b4+ /
\ / /
/
a2+b4+* /
\ /
a2+b4+*1-
26.What do you mean by Base case, Recursive case, Binding Time, Run-Time Stack a
nd Tail Recursion?
These terms are found in Recursion.
1.Base Case:it is the case in recursion where the answer is
known,or we can say the termination condition for a
recursion to unwind back.
For example to find Factorial of num using recursion:
int Fact(int num){
if(num==1 || num==0)//base case
return 1;
else // recursive case:
return num*Fact(num-1);
}
2.Recursive case:It is the case whcih brings us to the
closer answer.
Run Time Stack:It is a system stack us to save the frame
stack of a function every recursion or every call.
This frame stack consists of the return address,local
variables and return value if any.
Tail Recursion:The case where the function consist of
single recursive call and it is the last statement to be
executed.A tail Recursion can be replace by iteration.
The above funtion consists of tail recursion case.
where as the below function does not.
void binary(int start,int end,int el){
int mid;
if(end>start){
mid=(start+end)/2;
if(el==ar[mid])
return mid;
else{
if(el>ar[mid])
binary(mid+1,end,ele);
else
binary(start,mid-11,ele);
}
}
}
27.Stack can be described as a pointer. Explain?
Because stack will contain a head pointer which will always point to the
top of the Stack.
All Stack Operations are done using Head Pointer. Hence Stack ca be Described as
a Pointer
28.Write a Binary Search program?
#include<conio.h>
#include<iostream.h>
#include<process.h>
int binarysearch(int list[], int end, int target, int &locn)
{
int first=0, mid, last=end;
while(first<=last)
{
mid=(first+last)/2;
if(target>list[mid])
first=mid+1;
else if(target<list[mid])
last=mid-1;
else
break;
}
locn=mid+1;
return(target==list[mid]);
}
void main()
{
int a[10],i,s=0,n,loc,flag=0;
clrscr();
cout<<"\n Enter the no. of element to store:\n";
cin>>n;
cout<<"Enter the Elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n The Elements are:\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<"\n Enter the Element to search:\n";
cin>>s;
if(binarysearch(a,n,s,&loc))
cout<<"\nThe element "<<s<< " is available at location
"<<loc<<endl;
else
cout<<"\nThe element "<<s<< " is not found in the List"<<endl;

29.What do you mean by: Syntax Error, Logical Error, Runtime Error?
Syntax Error-Errors in coding which do not follw language syntax format.
It can occur by human mistak in typing &
due lack knowledge of language .
EX - Ram are a boy.-error correct- Ram is a boy.
Logical Error- Here is correct, program will execute properly but not gi
ve answer correct.
EX- Table is a boy syntax correct but meaning is not
Runtime Error- overflow, underflow, out of memory capacity.

30.Which data structure is needed to convert infix notations to post fix notatio
ns?
Stack
31.How will inorder, preorder and postorder traversals print the elements of a t
ree?
void inorder(node * tree)
{
if(tree != NULL)
{
inorder(tree->leftchild);
printf("%d ",tree->data);
inorder(tree->rightchild);
}
else
return;
}
void postorder(node * tree)
{
if(tree != NULL)
{
postorder(tree->leftchild);
postorder(tree->rightchild);
printf("%d ",tree->data);
}
else
return;
}
void preorder(node * tree)
{
if(tree != NULL)
{
printf("%d ",tree->data);
preorder(tree->leftchild);
preorder(tree->rightchild);
}
else
return;
}
32.what is the need of data structure?
if we can learn about large amount of information the concept of data st
ructure is used.
33.Parenthesis are never needed in prefix or postfix expressions. Why?
Basically Parenthesis indicate the operations which need to be carried o
ut first ie according to the BODMAS rule..
SO in case of postfix or prefix expression they are actualy conversions of the o
rginal standard equation.
Where the brackets have already been taken into consideration,,,and the formed p
refix/postfix expression is the correct
order of expansion of a given mathematical statement..
34.what is binary tree?
A tree with at most two children for each node.
OR
A binary tree either
* is empty (no nodes), or
* has a root node, a left binary tree, and a right binary tree.
35.Convert following infix expression to the prefix expression a - b + c * (d /
e - (f + g))
+-ab*c-/de+fg
a-b+c*(d/e-(f+g))
a-b+c*(d/e-(+fg))
a-b+c*(/de-(+fg))
a-b+c*(-/de+fg)
a-b+(*c-/de+fg)
-ab+(*c-/de+fg)
+-ab*c-/de+fg (final answer)
36.Explain binary searching, Fibinocci search?
Searching a binary tree for a value that matches a key value is FAST, es
pecially for tightly packed trees (a tightly packed tree contains about twice as
many elements as the
previous level)
o Therefore at most log(2)n comparisons are required either to find a match or d
etermine that no match exists.
o For example, searching a tightly packed 1000-element binary search tree requie
s at most 10 comparisons because 2
o a binary search tree with n elements has a minimum of log(2)n levels
o in-order: 0. if parameter node is null then return; 1. recursive traverse left
subtrees entirely 2. display data 3. recursive traverse right subtrees entirely
o pre-order: 0 if param node is null return 1. display data 2. recursive travers
e left subtrees entirely 3. recursive traverse right subtree entirely.
o post-order: 0 if param node is null then return; 1. recursive traverse left su
btrees entirely 2. recursive traverse right subtree entirely 3. display data
o to keep it efficient you must make sure to keep tree balanced. Best way is to
ensure the inputted data is coming in with random values...If they are sorted in
ascending/descending order the tree will definitely become unbanlanced
Fibnacci search
The Fibonacci Sequence is defined such that the first two numbers of the sequenc
e are 0 and 1. subesequent numbers are the sum of the previous two.
e.g.0, 1, 1, 2, 3, 5, 8, 13 ......
37.A list is ordered from smaller to largest when a sort is called. Which sort w
ould take the shortest time to execute?
Bubble Sort Or Insertion Sort
38.How is it possible to insert different type of elements in stack?
you ca implement stack with union datatype... u may think of structures.
. but struct use storin of all values and mem allocated for all in each.. but in
union only one at a
time... this is right i think...
39.Which one is faster? A binary search of an orderd set of elements in an array
or a sequential search of the elements?
binary search if of order log n where as sequential search is of order
n, binary search is faster..
40.Explain about the types of linked lists?
Singly linked list- which is linear direction that has only
head part.
Doubly linked list- which is bi-directional that has both
head and tail part
Circular linked list- which as no ends.
41.Write programs for Bubble Sort, Quick sort?
//PROGRAM FOR BUBBLE SORT
#include<stdio.h>
#include<conio.h>
#define SIZE 5
void main()
{
int a[SIZE],n,i,j,temp;
clrscr();
printf("enter the elements ");
for(i=0;i<SIZE;i++)
scanf("%d",&a[i]);
printf("the sorted list is :->\n");
for(i=0;i<SIZE;i++)
for(j=i;j<SIZE-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
for(i=0;i<SIZE;i++)
printf("%d",a[i]);
getch();
}
/*QUICK SORT*/
#include<stdio.h>
#include<conio.h>
int split(int [],int,int);
void quicksort(int [],int,int);
void main()
{
int arr[20],n,i;
clrscr();
printf("\nQUICk SORT\n");
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nArray before sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,n);
printf("\nArray after sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
getch();
}

void quicksort(int a[],int lower,int upper)


{
int i;
if(upper>lower)
{
i=split(a,lower,upper);
quicksort(a,lower,i-1);
quicksort(a,i+1,upper);
}
}
int split(int a[],int lower,int upper)
{
int i,p,q,t;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return(q);
}

42.Write the programs for Linked List (Insertion and Deletion) operations?
/*OPERATIONS ON SINGLY LINKED LIST*/
#include<stdio.h>
#include<conio.h>
struct link
{
int item;
struct link *next;
};
typedef struct link node;
void addfirst();
void addlast();
void addmid();
void delfirst();
void dellast();
void delmid();
void display();
node *head=NULL;

void main()
{
int ch;
clrscr();
do
{
printf("\nSINGLY LINKED LIST OPERATIONS\n");
printf("\n1.Addfirst\n2.AddMid\n3.AddLast\n4.DeleteFirst\n5.DeleteMiddle
\n6.DeleteLast\n7.Display\n8.Exit\n");
printf("Enter your option:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
addfirst();
display();
break;
case 2:
addmid();
display();
break;
case 3:
addlast();
display();
break;
case 4:
delfirst();
display();
break;
case 5:
delmid();
display();
break;
case 6:
dellast();
display();
break;

case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
while(ch<=8);
getch();
}

void addfirst()
{
node *temp;
temp=(node *)malloc(sizeof(node));
printf("Enter the data....\t");
scanf("%d",&temp->item);
temp->next=head;
head=temp;
}
void addmid()
{
int i=1,pos;
node *cur=head,*temp;
printf("\nEnter the position\t");
scanf("%d",&pos);
while(pos!=i+1&&cur!=NULL)
{
cur=cur->next;
i++;
}
if(pos==i+1)
{
temp=(node *)malloc(sizeof(node));
printf("Enter the data...");
scanf("%d",&temp->item);
temp->next=cur->next;
cur->next=temp;
}
}

void addlast()
{
node *temp,*cur=head;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data....");
scanf("%d",&temp->item);
while(cur->next!=NULL)
{
cur=cur->next;
}
temp->next=cur->next;
cur->next=temp;
}

void delfirst()
{
node *temp=head;
head=head->next;
printf("Deleted item is %d\n",temp->item);
free(temp);
}

void delmid()
{
int i=1,pos;
node *cur=head,*temp;
printf("Enter the position to be deleted\t");
scanf("%d",&pos);
while(pos!=i+1&&cur->next!=NULL)
{
cur=cur->next;
i++;
}
if(pos==i+1)
{
temp=cur->next;
cur->next=temp->next;
printf("Deleted item is %d\n",temp->item);
free(temp);
}
}

void dellast()
{
node *temp,*cur=head;
while(cur->next->next!=NULL)
{
cur=cur->next;
}
temp=cur->next;
cur->next=NULL;
printf("Deleted item is %d\n",temp->item);
free(temp);
}

void display()
{
node *cur=head;
printf("\nHead->");
while(cur!=NULL)
{
printf("\t%d",cur->item);
cur=cur->next;
}
printf("<-NULL\n");
}

43. In which data structure, elements can be added or removed at either end, but
not in the middle?
Double ended queue
44.2D array?
An ordered arrangement of data elements. A vector is a one dimensional a
rray, a matrix is a two-dimensional array.
Most programming languages have the ability to store and manipulate arrays in on
e or more dimensions.
Multi-dimensional arrays are used extensively in scientific simulation and mathe
matical processing;
however, an array can be as simple as a pricing table held in memory for instant
access by an order entry program.
Two-dimensional arrays are declared by specifying the number of rows the
n the number of columns.
int a[30][10]; // declares an int array of 30 rows and 10 columns.
char ticTacToeBoard[3][3]; // three rows and three columns of chars.
45.Array?
An array is a systematic arrangement of objects, usually in rows and col
umns
46.Insertion Sort?
Insertion sort is a simple sorting algorithm, a comparison sort in which
the sorted array (or list) is built one
entry at a time. It is much less efficient on large lists than more advanced alg
orithms such as quicksort, heapsort, or
merge sort. However, insertion sort provides several advantages:Adaptive,Stable,
Complexity.In-place.
47.Heap Sort?
Heapsort is a comparison-based sorting algorithm, and is part of the sel
ection sort family.
Although somewhat slower in practice on most machines than a good implementation
of quicksort, it has the advantage of a
more favorable worst-case T(n log n) runtime. Heapsort is an in-place algorithm,
but is not a stable sort.
48.Quick Sort?
Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare
that, on average,
makes \mathcal{O}(n\log n) (big O notation) comparisons to sort n items. In the
worst case,
it makes \mathcal{O}(n^2) comparisons, though if implemented correctly this beh
avior is rare.
Typically, quicksort is significantly faster in practice than other \mathcal{O}(
n \log n) algorithms,
because its inner loop can be efficiently implemented on most architectures, and
in most real-world data,
it is possible to make design choices which minimize the probability of requirin
g quadratic time.
Additionally, quicksort tends to make excellent usage of the memory hierarchy, t
aking perfect advantage of
virtual memory and available caches. Coupled with the fact that quicksort is an
in-place sort and uses no temporary memory,
it is very well suited to modern computer architectures.
49.Merge Sort?
Merge sort is an O(n log n) comparison-based sorting algorithm. In most
implementations it is stable,
meaning that it preserves the input order of equal elements in the sorted output
.
50. what is the main difference between c and c++?
Actually c is a procedural programming language which cann't face the re
al world problem. It has some drawback
like a global data is shared by all function and if in a large program it is fin
d out difficult that which function uses
which data.
On the other hand c++ is an object oriented programming language which eliminate
some pitfall of conventional or
procedural programming language. It is a concept or approach for designing a new
software. It is nothing to do with
any programming language although a programming language which support the oops
concept to make it easier to implement.
This is the main different between c and c++.
c is a topdown approach while c++ is bottom up approach.c is not objec
t oriented but c++ is object oriented.
51.What is a virtual base class?
Virtual base class is a base class acts as an indirect base for more tha
n one without duplication of its data members.
A single copy of its data members is shared by all the base classes that
use it as a virtual base.
For example:
A
/ \
B C
\ /
D
class A { /* ... */ }; // indirect base class
class B : virtual public A { /* ... */ };
class C : virtual public A { /* ... */ };
class D : public B, public C { /* ... */ }; // valid
Using the keyword virtual in this example ensures that an object of clas
s D inherits only one subobject of class A.

52.What are the advantages and disadvantages of pointer in C language?


The main advantages of using pointers are
1.) Function cannot return more than one value. But when the same functi
on can modify many pointer variables and
function as if it is returning more than one variable.
2.) In the case of arrays, we can decide the size of th array at runtime
by allocating the necessary space.
3.) In the case of pointers to classes, we can use polymorphism and virt
ual classes to change the behavior of
pointers to various types of classes at runtime
Coming to the disadvantages of pointers
1.) If sufficient memory is not available during runtime for the storage
of pointers, the program may crash
(least possible)
2.) If the programmer is not careful and consistent with the use of poin
ters, the program may crash
(very possible)
53.Static Variable?
A variable that retains the same data throughout the execution of a prog
ram. In contrast, a dynamic variable can have
different values during the course of a program.
54.What are the differences between a union and a structure in C?
The difference between structure and union in c are: 1. union allocates
the memory equal to the maximum memory
required by the member of the union but structure allocates the memory equal to
the total memory required by the members.
2. In union, one block is used by all the member of the union but in case of str
ucture, each member have their own memory
space
55.Advantages of Microcontroller compared to Microprocessor?
The advantages of microcontroller are that all MCUs have on-chip resourc
es to achieve a higher level of integration and reliability at a lower cost. An
on-chip resource is a block of circuitry built into the MCU which performs some
useful function under control of the MCU. Built-in resources increase reliabilit
y because they do not require any external circuitry to be working for the resou
rce to function. They are pre-tested by the manufacturer and conserve board spac
e by integrating the circuitry into the MCU.
Some of the more popular on-chip resources are memory devices, timers, s
ystem clock/oscillator, and I/O. Memory devices include read/write memory (RAM),
read-only memory (ROM), erasable programmable ROM (EPROM), electrically erasabl
e programmable ROM (EEPROM), and electrically erasable memory (EEM). The term EE
M actually refers to an engineering development version of an MCU where EEPROM i
s substituted for the ROM to reduce development time.
Timers include both real-time clocks and periodic interrupt timers. Othe
r timer functions include timer compare and/or input capture lines.
I/O includes serial communication ports, parallel ports (I/O lines), ana
log-todigital (A/D) converters, digital-to-analog (D/A) converters, liquid cryst
al display drivers (LCD), and vacuum fluorescent display drivers (VFD).
Other built-in resources may include computer operating properly (COP) w
atchdog system which can be hardware or software based.
at 6:47 AM
56.Binary Tree Program?
/*Binary Tree Travesals*/
#include
#include
#include
typedef struct bst
{
int data;
struct bst *left,*right;
}node;
void insert(node *, node *);
void inorder(node *);
void postorder(node *);
void preorder(node *);
node *get_node();
/*Main Function*/
void main()
{
int ch,ans=5;
node *New,*root;
root=NULL;
clrscr();
while(1)
{
printf("\nEnter:\n1-Create\n2-Preorder\n3-Inorder\n4-Postorder\n5-Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter Elements 1 by 1: (0 to stop entering)\n");
do
{
New=get_node();
scanf("%d",&New->data);
ans=New->data;
if(ans!=0)
if(root==NULL)
root=New;
else
insert(root,New);
}while(ans!=0);
break;
case 2:
if(root==NULL)
printf("\nNo element In Tree\n");
else
{
printf("\n~~~PREORDER TRAVERSALS~~~\nThe Tree is:\n");
preorder(root);
}
break;
case 3:
if(root==NULL)
printf("\nNo element In Tree\n");
else
{
printf("\n~~~INORDER TRAVERSALS~~~\nThe Tree is:\n");
inorder(root);
}
break;
case 4:
if(root==NULL)
printf("\nNo element In Tree\n");
else
{
printf("\n~~~POSTORDER TRAVERSALS~~~\nThe Tree is:\n");
postorder(root);
}
break;
default:
printf("\n~~~Exit~~~\n");
getch();
exit(0);
break;
}
}
}
/*Get node*/
node *get_node()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
return temp;
}
/*Insert Function*/
void insert(node *root,node *New)
{
if(New->data < root->data)
{
if(root->left==NULL)
root->left=New;
else
insert(root->left,New);
}
if(New->data > root->data)
{
if(root->right==NULL)
root->right=New;
else
insert(root->right,New);
}
}
/*preorder Traversals*/
void preorder(node *temp)
{
if(temp!=NULL)
{
printf("-> %d ",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
/*Inorder Traversals*/
void inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
printf("-> %d ",temp->data);
inorder(temp->right);
}
}
/*postorder Traversals*/
void postorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("-> %d ",temp->data);
}
}

OUTPUT:

/*Binary Tree Traversals*/

Enter:
1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit
1
Enter Elements 1 by 1: (0 to stop entering)
5 3 7 2 4 6 9
0

Enter:
1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit
2

~~~PREORDER TRAVERSALS~~~
The Tree is:
-> 5 -> 3 -> 2 -> 4 -> 7 -> 6 -> 9

Enter:
1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit
3

~~~INORDER TRAVERSALS~~~
The Tree is:
-> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9

Enter:
1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit
4

~~~POSTORDER TRAVERSALS~~~
The Tree is:
-> 2 -> 4 -> 3 -> 6 -> 9 -> 7 -> 5

Enter:
1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit
5

or

#include<stdio.h>
#include<conio.h>
struct rec
{long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void *exchange(struct rec *tree);
struct rec *temp;
void main()
{ struct rec *tree=NULL;
int choice; long digit; do{
choice=select();
switch(choice)
{
case 1: puts("Enter integer: To quit enter 0");
scanf("%ld",&digit);
while(digit!=0)
{
tree=insert(tree,digit);
scanf("%ld",&digit);
}continue;
case 2: printf("%5d\n",tree->num);exchange(tree);continue;
case 3: puts("END");exit(0);
}}while(choice!=3);
}int select()
{int selection;
do
{puts("Enter 1: Insert a node");
puts("Enter 2: Exchange subtrees");
puts("Enter 3: End");
puts("Enter your choice");
scanf("%d",&selection);
if((selection<1)||(selection>3))
{puts("Wrong choice: Try again");
getchar();
}}while((selection<1)||(selection>3));
return selection;
}struct rec *insert(struct rec *tree,long digit)
{if(tree==NULL)
{tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;
}else
if(digit<tree->num)
tree->left=insert(tree->left,digit);
else if(digit>tree->num)
tree->right=insert(tree->right,digit);
else if(digit==tree->num)
{puts("Duplicates Nodes: Program Exited");exit(0);
}return(tree);
}void *exchange(struct rec *tree)
{if((tree->left->num!=0)&&(tree->right->num!=0))
{temp=tree->left;
tree->left=tree->right;
tree->right=temp;
printf("%5ld\n",tree->left->num);
printf("%5ld\n",tree->right->num);
exchange(tree->left);
exchange(tree->right);
}}

Potrebbero piacerti anche