My C proggram is having trouble in the switch in main Also t
My C proggram is having trouble in the switch in main. Also the a couple of funtion like delete and display tree are failing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tree_node {
int data;
struct tree_node *left, *right;
};
typedef struct nodeT {
int key;
struct nodeT *left, *right;
int data;
} nodeT, *treeT;
//struct nodeT *t1, *t2;
void DisplayTree(nodeT*t)
{
if (t != NULL) {
DisplayTree(t->left);
printf(\"%c \", t->key);
DisplayTree(t->right);
}
}
nodeT *FindNode(nodeT *t, int key){
if(t==NULL) return 0;
if (key == t->key)
return t;
if(key < t->key){
return FindNode(t->left, key);;
} else {
return FindNode(t->right, key);;
}
}
int max(nodeT *p){
nodeT *t, *tmp;
while(tmp->right != NULL)
tmp=tmp->right;
return tmp->data;
}
int min(nodeT *t){
nodeT *tmp;
while(tmp->left != NULL)
tmp = tmp->left;
return tmp->data;
}
void ModifyPreOrderWalk(nodeT *t, int h)
{
int i;
if (t == NULL) return;
for(i=0; i<-1;i++){
printf(\"| \");
}
if(h>1)printf(\"+---\");
printf(\"%d\ “\", t->key);
ModifyPreOrderWalk(t->left, h+1);
ModifyPreOrderWalk(t->right, h+1);
}
void PrintTree(nodeT *t)
{
ModifyPreOrderWalk(t, 1);
}
void delete(nodeT **p){
nodeT *target, *imd_r, *plmd_r;
target=*p;
if (target->left==NULL && target->right==NULL) {
*p=NULL;
} else if (target->left == NULL) {
*p=target->right;
} else
if (target->right == NULL) {
*p=target->left;
} else {
plmd_r = target;
imd_r = target->right;
while( imd_r->left != NULL){
plmd_r = imd_r;
imd_r = imd_r->left;
}
if(plmd_r == target)
plmd_r->right = imd_r->right;
else
plmd_r->left = imd_r->right;
imd_r->left = target->left;
imd_r->right = target->right;
*p = imd_r;
}
free(target);
}
void listInorder(nodeT *t){
if (t != NULL) {
DisplayTree(t->left);
printf(\"%d \", t->key);
DisplayTree(t->right);
}
}
void listPreorder(nodeT *t) {
if (t != NULL) {
printf(\"%d \", t->key);
DisplayTree(t->left);
DisplayTree(t->right);
}
}
void listPostOrder(nodeT *t){
if (t != NULL) {
DisplayTree(t->left);
DisplayTree(t->right);
printf(\"%d\", t->key);
}
}
void InsertNode(nodeT **tptr, int key){
nodeT *t;
nodeT *tmp;
t=*tptr;
if (t == NULL) {
tmp= malloc(sizeof(nodeT));
tmp->key = key;
tmp->left=tmp->right=NULL;
*tptr=tmp;
return;
}
if (key < t->key) {
InsertNode
(&t->left, key);
} else {
InsertNode(&t->right, key);
}
}
int height(nodeT *t){
int lDepth = height(t->left);
int rDepth = height(t->right);
if(lDepth > rDepth)
return(lDepth+1);
else
return(rDepth+1);
}
int sum(nodeT *p){
if (p == NULL)
return 0;
else
return (p->data + sum(p->left) +sum(p->right) );
}
int count(nodeT *t){
if(t == NULL)
return 0;
int i = 1+count(t->left)+count(t->right);
return i;
}
/*nodeT *New(){
nodeT *tmp;
tmp = (nodeT *)malloc(sizeof(nodeT));
if (tmp==NULL) return NULL;
return tmp;
}*/
int main(){
nodeT *node;
nodeT *t=NULL, *findA;
int f;
int d, a, b, c, j, k, e, s, y;
while (1) {
printf(\"\ I - insert\ F - find node\ D - delete\ L - list in order\ R - list preorder\ P - list PorstOrder\ M - max\ N - min\ H - height\ C - count\ S - sum\ W - display tree\ Q - quit\ \");
printf(\"> \");
// char line = GetLine();
char ch;// = toupper(line[0]);
scanf(\"%c\",&ch);
switch (ch) {
// plmd_r->left = imd_r->right;
case \'I\': scanf(\"%d\",&y); InsertNode(&t,y); break;
case \'F\':printf(\"enter value you are tyring to find\ \");
scanf(\"%d\ \", &f);
FindNode(t, f); break;
case \'D\':printf(\"enter value you are tyring to delete\ \");
findA= FindNode(t,f);
delete(&findA); break;
case \'L\': listInorder(t); break;
case \'R\': listPreorder(t); break;
case \'P\': listPostOrder(t); break;
case \'M\': j=max(t);
//printf(\"%d\ \", &j);
break;
case \'N\': c= min(t);
// printf(\"%d\ \", &c);
break;
case \'H\': k= height(t);
// printf(\"%d\ \", &k);
break;
case \'C\': e=count(t);
//printf(\"%d\ \" &e);
break;
case \'S\': s=sum(t);
// printf(\"%d\ \", &s);
break;
case \'W\': DisplayTree(t);
case \'Q\': exit(0);
// default:printf(\"Illegal command\ \"); break;
}
}
}
struct tree_node {
int data;
struct tree_node *left, *right;
};
typedef struct nodeT {
int key;
struct nodeT *left, *right;
int data;
} nodeT, *treeT;
//struct nodeT *t1, *t2;
void DisplayTree(nodeT*t)
{
if (t != NULL) {
DisplayTree(t->left);
printf(\"%c \", t->key);
DisplayTree(t->right);
}
}
nodeT *FindNode(nodeT *t, int key){
if(t==NULL) return 0;
if (key == t->key)
return t;
if(key < t->key){
return FindNode(t->left, key);;
} else {
return FindNode(t->right, key);;
}
}
int max(nodeT *p){
nodeT *t, *tmp;
while(tmp->right != NULL)
tmp=tmp->right;
return tmp->data;
}
int min(nodeT *t){
nodeT *tmp;
while(tmp->left != NULL)
tmp = tmp->left;
return tmp->data;
}
void ModifyPreOrderWalk(nodeT *t, int h)
{
int i;
if (t == NULL) return;
for(i=0; i<-1;i++){
printf(\"| \");
}
if(h>1)printf(\"+---\");
printf(\"%d\ “\", t->key);
ModifyPreOrderWalk(t->left, h+1);
ModifyPreOrderWalk(t->right, h+1);
}
void PrintTree(nodeT *t)
{
ModifyPreOrderWalk(t, 1);
}
void delete(nodeT **p){
nodeT *target, *imd_r, *plmd_r;
target=*p;
if (target->left==NULL && target->right==NULL) {
*p=NULL;
} else if (target->left == NULL) {
*p=target->right;
} else
if (target->right == NULL) {
*p=target->left;
} else {
plmd_r = target;
imd_r = target->right;
while( imd_r->left != NULL){
plmd_r = imd_r;
imd_r = imd_r->left;
}
if(plmd_r == target)
plmd_r->right = imd_r->right;
else
plmd_r->left = imd_r->right;
imd_r->left = target->left;
imd_r->right = target->right;
*p = imd_r;
}
free(target);
}
void listInorder(nodeT *t){
if (t != NULL) {
DisplayTree(t->left);
printf(\"%d \", t->key);
DisplayTree(t->right);
}
}
void listPreorder(nodeT *t) {
if (t != NULL) {
printf(\"%d \", t->key);
DisplayTree(t->left);
DisplayTree(t->right);
}
}
void listPostOrder(nodeT *t){
if (t != NULL) {
DisplayTree(t->left);
DisplayTree(t->right);
printf(\"%d\", t->key);
}
}
void InsertNode(nodeT **tptr, int key){
nodeT *t;
nodeT *tmp;
t=*tptr;
if (t == NULL) {
tmp= malloc(sizeof(nodeT));
tmp->key = key;
tmp->left=tmp->right=NULL;
*tptr=tmp;
return;
}
if (key < t->key) {
InsertNode
(&t->left, key);
} else {
InsertNode(&t->right, key);
}
}
int height(nodeT *t){
int lDepth = height(t->left);
int rDepth = height(t->right);
if(lDepth > rDepth)
return(lDepth+1);
else
return(rDepth+1);
}
int sum(nodeT *p){
if (p == NULL)
return 0;
else
return (p->data + sum(p->left) +sum(p->right) );
}
int count(nodeT *t){
if(t == NULL)
return 0;
int i = 1+count(t->left)+count(t->right);
return i;
}
/*nodeT *New(){
nodeT *tmp;
tmp = (nodeT *)malloc(sizeof(nodeT));
if (tmp==NULL) return NULL;
return tmp;
}*/
int main(){
nodeT *node;
nodeT *t=NULL, *findA;
int f;
int d, a, b, c, j, k, e, s, y;
while (1) {
printf(\"\ I - insert\ F - find node\ D - delete\ L - list in order\ R - list preorder\ P - list PorstOrder\ M - max\ N - min\ H - height\ C - count\ S - sum\ W - display tree\ Q - quit\ \");
printf(\"> \");
// char line = GetLine();
char ch;// = toupper(line[0]);
scanf(\"%c\",&ch);
switch (ch) {
// plmd_r->left = imd_r->right;
case \'I\': scanf(\"%d\",&y); InsertNode(&t,y); break;
case \'F\':printf(\"enter value you are tyring to find\ \");
scanf(\"%d\ \", &f);
FindNode(t, f); break;
case \'D\':printf(\"enter value you are tyring to delete\ \");
findA= FindNode(t,f);
delete(&findA); break;
case \'L\': listInorder(t); break;
case \'R\': listPreorder(t); break;
case \'P\': listPostOrder(t); break;
case \'M\': j=max(t);
//printf(\"%d\ \", &j);
break;
case \'N\': c= min(t);
// printf(\"%d\ \", &c);
break;
case \'H\': k= height(t);
// printf(\"%d\ \", &k);
break;
case \'C\': e=count(t);
//printf(\"%d\ \" &e);
break;
case \'S\': s=sum(t);
// printf(\"%d\ \", &s);
break;
case \'W\': DisplayTree(t);
case \'Q\': exit(0);
// default:printf(\"Illegal command\ \"); break;
}
}
}
Solution
Answer:
There should be :
return 0 ;
As last line of int main() As return value is int...!!!







