원하는 값이 제대로 안 나와요..
에가득
이진탐색트리 이름을 반복적 중위순회 하는겁니다.문제가 뭐냐면 중위순회가 제대로 작동이 안합니다
그냥 입력된 이름 그대로 출력 되네요. 제 의도는 중위순회 된 순서로 출력이 되야되는데 말이죠..질문 내용 :
#include stdio.h
#include stdlib.h
#include string.h
#define max 100
typedef struct node *tree_pointer;
typedef struct node {
tree_pointer left_child;
char *data;
tree_pointer right_child;
}node;
tree_pointer stack[max];
int top=-1;
void insert_node(tree_pointer *, char *);
tree_pointer modified_search(tree_pointer, char *);
void iter_inorder(tree_pointer);
void push(int*, tree_pointer);
tree_pointer pop(int*);
int main(void)
{
tree_pointer tree=null;
char i;
char *num[]={go, do, kim, ahn, lee, jung,
hong, baek, park, nam, mun, choi};
for (i=0; i12; i++)
insert_node(&tree, num[i]);
iter_inorder(tree);
printf(\n\n);
system(pause);
return 0;
}
void insert_node(tree_pointer *node, char *num) {
tree_pointer ptr, temp = modified_search(*node, num);
if (temp || !(*node)) {
ptr = (tree_pointer) malloc(sizeof(struct node));
ptr-data = (char*)malloc(sizeof(*num) / sizeof(char*));
strcpy(ptr-data, num);
ptr-left_child = ptr-right_child = null;
if (*node)
if (numtemp-data) temp-left_child = ptr;
else temp-right_child = ptr;
else *node = ptr;
}
}
tree_pointer modified_search(tree_pointer tree, char *key) {
tree_pointer temp;
if (!tree) return null;
while (tree) {
temp=tree;
if (!strcmp(key, tree-data)) {
printf(duplicate key: %d\n, key);
system(pause);
exit(1);
}
if (keytree-data)
tree = tree-left_child;
else
tree = tree-right_child;
}
return temp;
}
void iter_inorder(tree_pointer ptr) {//이부분이 스택을 이용한 중위순회 함수입니다..
for (;;) {
for (;ptr;ptr=ptr-left_child) push(&top, ptr);
ptr=pop(&top);
if (!ptr) break; /* 공백 스택 */
printf( %s, ptr-data);
ptr=ptr-right_child;
}
}
/* 스택의 삽입 함수 */
void push(int *in, tree_pointer ptr) {
if (*in = max -1) {
printf(stack full);
exit(1);
}
stack[++* in] = ptr;
}
/* 스택의 삭제 함수 */
tree_pointer pop(int *out){
if(*out 0) {
printf(\n);
fprintf(stderr, stack is emptied!!);
return null;
&nb
}
return stack[(*out)--];
}