There seems to be no problem in the insertion process, but only the root node is output when printing. What is the problem? text error
- n = (node*)m(sizeof(node))
- print , scan
Data structure, tree
struct node {
int data;
node* l, *r;
};
node* Insert(node* st, int item)
{
node* n, * p; p = st;
n = (node*)m(sizeof(node));
n->data = item; n->l = NULL; n->r = NULL;
if (st == NULL) {
return n;
}
while (p != NULL) {
if (p->data > item) {
p = p->l;
}
else p = p->r;
}
p = n;
return st;
}
node* st = NULL;
node* a = NULL;
while (1) {
print("\n트리 1)삽입 2)삭제 3)전위순회 출력 4) 종료 "); scan("%d", &menu);
if (menu == 4) break;
switch (menu) {
case 1:
print("삽입할 값 입력 : "); scan("%d", &item);
st = Insert(st, item);
break;
case 2:
a = st;
print("%d", a->data); a = a->l; print("%d", a->data); a = st->r; print("%d", a->data);
break;