programming language in c
16 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 
Results  1 to 10 of 16  « Prev 1 2 Next » 

Posted By:   nandini_borgohain
Posted On:   Monday, March 29, 2004 10:20 PM

1.Write a program in C language to merge the contents of two binary search trees into one.

2.Write a program in C language to count the number of internal nodes of a tree.

3.Write a Program in C language to evaluate an expression using a Queue. You should not use the concept of Stack any where in your program.

Re: programming language in c

Posted By:   Anonymous  
Posted On:   Saturday, May 8, 2004 12:00 PM

1.Write a program in C language to merge the contents of two binary search trees into one. 2.Write a program in C language to count the number of internal nodes of a tree. 3.Write a Program in C language to evaluate an expression using a Queue. You should not use the concept of Stack any where in your program.

Re: programming language in c

Posted By:   Anonymous  
Posted On:   Saturday, May 1, 2004 08:01 AM

i am neede with your source code and algo.

Re: Write a program in C language to merge the contents of two binary search trees into one.

Posted By:   Simon_Ablett  
Posted On:   Wednesday, April 28, 2004 02:24 AM

Has anybody noticed that this is a Java forum?

Write a program in C language to merge the contents of two binary search trees into one.

Posted By:   Debmalya_Jash  
Posted On:   Wednesday, April 28, 2004 12:18 AM

/*
Write a program in ‘C’ language to merge the contents of two binary search trees into one.
What are the time and storage complexities of your program ?
*/


#include

#include

#include


typedef struct tree{

struct tree* left;

int data;

struct tree* right;

}bst;


bst *tree1_root=NULL,

*tree2_root=NULL;


int get_number();


void ins(bst*);

void insert(bst*,int);

bst* make_node(int);


void merge_tree(bst*,bst*);


void in_order(bst*);

void pre_order(bst*);

void post_order(bst*);


void main(){

time_t begin,end;

ins(tree1_root);

ins(tree2_root);


textcolor(WHITE);

cprintf("
Pre order traversal tree1 :");

pre_order(tree1_root);

cprintf("
In order traversal tree1 :");

in_order(tree1_root);

cprintf("
Post order traversal tree1 :");

post_order(tree1_root);


textcolor(CYAN);

cprintf("
Pre order traversal tree2 :");

pre_order(tree2_root);

cprintf("
In order traversal tree2 :");

in_order(tree2_root);

cprintf("
Post order traversal tree2 :");

post_order(tree2_root);


begin=clock()/CLK_TCK;

merge_tree(tree1_root,tree2_root);

end=clock()/CLK_TCK;

textcolor(GREEN);

cprintf("
Time taken for merging two trees %7.2lf",difftime(end,begin));


textcolor(YELLOW);

cprintf("
Pre order traversal merged tree1 & tree2 :");

pre_order(tree2_root);

cprintf("
In order traversal merged tree1 & tree2 :");

in_order(tree2_root);

cprintf("
Post order traversal merged tree1 & tree2 :");

post_order(tree2_root);


printf("
press any key to continue");

getch();


}


void ins(bst* p){

char ch;

do {

insert(p,get_number());


gotoxy(10,24);

printf("Continue(y/n)?");

ch=getch();

} while(toupper(ch)=='Y');

}
int get_number(){

int n;

clrscr();

gotoxy(30,12);

cprintf("Enter the element :");

scanf("%d",&n);

return n;

}

void insert(bst* p,int n){

bst* temp=NULL;

if (p==NULL){

p=make_node(n);

cprintf("
%d inserted as root ",n);

return;

}

else{

if (tree1_root!=NULL && tree2_root==NULL)

temp=tree1_root;

if (tree1_root!=NULL && tree2_root!=NULL)

temp=tree2_root;

p=temp;

while (1){


if (n>p->data)

p=p->left;

else if (ndata)

p=p->right;

else if (n==p->data){

textcolor(RED);

cprintf("
%d already inserted",n);

textcolor(WHITE);

return;

}

if (p==NULL){

p=make_node(n);

cprintf("
%d inserted",n);

return;

}

} /* end while (1) */

} /* end else part */

} /* end of function */


bst* make_node(int n){

bst* p=NULL;

p=(bst*)malloc(sizeof(bst));

if (p==NULL){

textcolor(RED);

cprintf("
Memroy allocation failure");

textcolor(WHITE);

}

else{

p->data=n;

p->left=NULL;

p->right=NULL;

}

return p;

}


void in_order(bst* p){

if (p!=NULL){

in_order(p->left);

cprintf("%5d",p->data);

in_order(p->right);

}

}


void pre_order(bst* p){

if (p!=NULL){

cprintf("%5d",p->data);

pre_order(p->left);

pre_order(p->right);

}

}



void post_order(bst* p){

if (p!=NULL){

post_order(p->left);

post_order(p->right);

cprintf("%5d",p->data);

}

}


void merge_tree(bst* t1,bst* t2){

if (t2!=NULL){

merge_tree(t1,t2->left);

insert(t1,t2->data);

merge_tree(t1,t2->right);

}

}

Re: programming language in c

Posted By:   vkm_maurya  
Posted On:   Tuesday, April 27, 2004 10:26 PM

Write a Program in C language to evaluate an expression using a Queue. You should not use the concept of Stack any where in your program.

Re: programming language in c

Posted By:   pooja_bhatt  
Posted On:   Saturday, April 24, 2004 08:56 AM

write a program in c language to evaluate expression using a queue.you should not use the concept of stack anywhere in your program.

Re: programming language in c

Posted By:   Vinit_Sharma  
Posted On:   Tuesday, April 20, 2004 12:13 PM

Still People need this... Guys I gave the solution. What are you waiting for now.......

www.asciiblue.com

Re: programming language in c

Posted By:   Vinit_Sharma  
Posted On:   Thursday, April 15, 2004 08:16 AM

Visit :
Find ur answers on :

http://www.asciiblue.com


This is my website... I have tried giving the solution to your problems. I hope you guys will find this useful.

Re: programming language in c

Posted By:   zia_mustafa  
Posted On:   Thursday, April 15, 2004 02:07 AM

Evaluation of prefix expression using queues
To evaluate the prefix expression, scan it repeatedly until the final expression value is known. In each scan, read the tokens and store them in a queue. In each scan replace an operator followed by two operands by the calculated values. For example, the following expression is a prefix expression, which is evaluated to 159.
- + * 9 + 2 8 * + 4 8 6 3
We scan the expression and store it in a queue. During the scan, when an operator is followed by two operands, such as + 2 8, we put the result, 10 , in the queue.
After the first scan, we have
- + * 9 10 * 12 6 3
After the second scan, we have
- + 90 72 3
After the third scan, we have
- 162 3
After the fourth scan, we have
159





Evaluating expressions.
Below are evaluations of the same expression in three different notations. Time "flows" from top to bottom.
Infix notation prefix notation postfix notation
0: (1 + (2 * 3) + 1 * 2 3 1 2 3 * +
1: (1 + 6) + 1 6 1 6 +
2: 7 7 7
The basic properties of rewriting are well-known. At each step the algorithm works by (1) inspecting parts of an expression, and then (2) replacing that part by a value. So the method actually consumes the original expression. Note also that all rewriting is in-place.
For larger expressions there may be several places where rewriting could take place, and the rewriting could then even proceed concurrently. For infix and prefix expressions it may be necessary to search to the left or to the right to find a suitable expressions to evaluate next. For postfix it is always possible to restrict the search to the right, this is used in the stack machine below.
Evaluating expressions by a queue machine
Here is a method which uses the prefix expression as a queue. Time again flows "flows" from top to bottom.
0: + 1 * 2 3
1: 1 * 2 3 + (2b)
2: * 2 3 + 1 (2c)
3: + 1 6 (2a)
4: 7 (2a)

The algorithm is quite different from in-place rewriting in the first section and also different from the stack-rewriting in the second section.

WHILE the queue has more than one element DO

(1) Inspect the front element.
(2a) If it is an operator followed by all its arguments,
append the value to the rear of the queue AND THEN
remove operator and arguments from the front of the queue
(2b) If it is an operator not followed by all its arguments,
remove it from the front, copy it to the rear.
(2c) (it is an operand)
remove it from the front, copy it to the rear.
The algorithm resembles rewriting by starting with a prefix expression and ending with a value, but the rewriting is not in-place. The algorithm resembles the stack machine by successively examining the next symbol of the expression, but it does not use an auxiliary structure. The significance of the "AND THEN" emphasis in step (2a) will become apparent shortly.
In the same way, it is just as simple to evaluate a postfix expression as a queue - merely by reading the queue from right to left.
0: 1 2 3 * +
1: + 1 2 3 * (2b)
2: 6 + 1 (2a)
3: 1 6 + (2c)
4: 7 (2a)
Of course postfix and prefix are not just reverses of each other, as is seen by examples with non-commutative operators such as subtraction or division. But as the two examples demonstrate, there is no inherent connection between stack evaluation and postfix, or between queue evaluation and prefix. Any such connection is just by convention. This being said, I now follow this convention and speak of stack machines being driven by postfix expressions. In the same way I shall now take queue machines to be certain rewritings of prefix expressions.
For any particular prefix evaluation the efficiency will depend on the ratio of the (2a) steps that really do the work, and the (2b) and (2c) bookkeeping steps that are at best a nuisance. The ratio depends on the patters of (binary) operators b and values v. In the example, of the form
b v b v v
the ratio was 2:1:1. For expressions of the form
b b v v b v v
the ratio is 3:1:0 which is better. For expressions of the form
b b v b v v v
the ratio is 3:3:3 which is worse. In general, the algorithm is best for expressions representing a balanced binary tree. Since most expressions depart from this ideal, the entire method would seem to be barely more than a cute but silly curiosity.
But let us recapitulate: the other methods use in-place rewriting of either an original infix or prefix or postfix expression, or of a separate stack. For word-size values such as integers this hardly matters because (1) integer operations are indivisible single machine operations, and (2) the result will always fit into the space that has been vacated by the operands. The queue machine is also a rewriting method, but it is not in-place. This has a number of repercussions, to be discussed below.

Re: programming language in c

Posted By:   kvijay_shankar  
Posted On:   Monday, April 12, 2004 12:49 PM

Hi I am doing my mca , please send me these
programs to my mail..
kvijay_shankar@epatra.com
Results  1 to 10 of 16  « Prev 1 2 Next » 
About | Sitemap | Contact