트리 순회코드인데요 오류의 원인이 뭘까요??
한결
#include stdio.h
#include malloc.h
struct tnode
{
int data;
struct tnode *left_child;
struct tnode *right_child;
};
typedef struct tnode node;
typedef node *tree_ptr;
tree_ptr temp insert(tree_ptr head, int number)
{
tree_ptr temp=NULL;
tree_ptr insertpoint = NULL;
if(!head)
{
temp=(tree_ptr)malloc(sizeof(node));
temp-data=number;
temp-left_child=temp-right_child=NULL;
return temp;
}
insertpoint = head;
for(;;)
{
if((insertpoint-datanumber)&&(insertpoint-left_child!=NULL))
insertpoint = insertpoint-left_child;
else if(insertpoint-data==number)
return head;
else if((insertpoint-datanumber)&&(insertpoint-right_child!=NULL))
insertpoint = insertpoint-right_child;
else break;
}
temp=(tree_ptr)malloc(sizeof(node));
temp-data=number;
temp-left_child=temp-right_child=NULL;
if(insertpoint-datanuber)
insertpoint-right_child=temp;
else
insertpoint-left_child=temp;
return head;
}
void inorder(tree_ptr ptr)
{
if(ptr)
{
inorder(ptr-left_child);
printf(%d,ptr-data);
inorder(ptr-right_child);
}
}
void postorder(tree_ptr ptr)
{
if(ptr)
{
postorder(ptr-left_child);
postorder(ptr-right_child);
printf(%d,ptr-data);
}
}
void preorder(tree_ptr ptr)
{
if(ptr)
{
printf{%d,ptr-data);
preorder(ptr-left_child);
preorder(ptr-right_child);
}
}
int main()
{
int i;
int arr[100];
tree_ptr head=NULL;
for(i=0;i100;i++)
{
scanf{%d,&arr[i]};
head=insert(head,number[i]);
if(arr[i]=-1)
break;
}
printf(inorder:);
inorder(head);
printf(\n);
printf(postorder:);
postorder(head);
printf(\n);
printf(preorder:);
preorder(head);
printf(\n);
}
-
송아리
printf{\%d\
-
사라
비주얼스튜디오로 작성하는경우
디버깅하는방법만알면 충분히 할수있어요
프로그래을 ctrl+f5는 이제부터 누르지마시고
디버깅을 해보세요.
먼저 자신이 자신있게 잘짯다고 생각하거나,
이부분이 의심스러운곳에서
f9 눌러요. 빨간점이 왼쪽에 생기구요
이제 f5(디버깅시작이며 빨간점에서 멈춤)
누르면 아까 점찍은데서 멈출거에요
이때 화면하단쯤에 자동,지역,조사식 등이 나올거에요
f10을 누르면 다음 라인을 진행하구
하단의 변수 값들도 변하