Ok. Made some good strideson the program but its still not working and I don\'t
ID: 3611127 • Letter: O
Question
Ok. Made some good strideson the program but its still not working and I don't know why. Irun the program and their are no errors but it reads my tree as"six six six six six six". It should put my input which is (inorder of submition) "hello to three four five six". Ive spent hourson this and I cannot fint the problem. Thanks for thehelp.
[code]
/*main.c*/
#include"tree.h"
int main() {
int c;
char s[22],*n;
n=s;
tnode * t = NULL, *x;
while (scanf("%s",n) !=EOF) {t=insert(n,t);}
printf(" ");
preoder(t);
printf(" ");
printf("count: %d ",c =count(t));
printf(" ");
return 0;
}
/*tree.h*/
#ifndef TREE_H
#define TREE_H
#include <string.h>
#include <stdio.h>
typedef struct node {
char *info;
struct node * right,*left;
} tnode;
tnode * insert(char *target,tnode * t);
tnode * makenode(char *x);
tnode * tsearch(char * x,tnode * t);
void inorder(tnode * t);
void preoder(tnode * t);
int height(tnode * t);
int count(tnode * t) ;
#endif
/*insert.c*/
#include "tree.h"
tnode * insert(char * x,tnode * t) {
if (t == NULL) return makenode(x);
int j; j=strcmp(x,t->info);
if (j<=0) {t->left = insert(x,t->left); return t;}
t->right = insert(x,t->right); return t;
}
/*proder.c*/
void preoder(tnode * t) {
if (t == NULL)return;
printf("%s",t->info);
preoder(t->left);
preoder(t->right);
}
[/code]
Explanation / Answer
//Dear, u wll need to do two changes, one in typedef, other in mknode, this will solve your problem. typedef struct node { charinfo[50]; struct node * right,*left; } tnode; tnode * makenode(char *x) { tnode *p =(tnode*) malloc(sizeof(tnode));; p->right=p->left=NULL; //p->info=x; strcpy(p->info,x); return p; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.