C 자료구조 트리 순회관련 질문드립니다.
솔찬
#include stdio.h
#include stdlib.htypedef struct TreeNode{
int data;
struct TreeNode* left;
struct TreeNode* right;
}TreeNode;
TreeNode *createNode()
{
TreeNode* root;
root=(TreeNode *)malloc(sizeof(TreeNode));
root-data = 0;
root-left = NULL;
root-right = NULL;
return root;
} void preorder(TreeNode *ptr)
{
if(ptr)
{
printf(%d, ptr-data);
preorder(ptr - left);
preorder(ptr - right);
}
}
void inorder(TreeNode *ptr)
{
if(ptr){
inorder(ptr - left );
printf(%d,ptr-data);
inorder(ptr-right);
}
}
void postorder(TreeNode *ptr)
{
if(ptr) {
postorder(ptr-left);
postorder(ptr-right);
printf(%d,ptr-data);
}
} void insertNode(TreeNode *ptr, int data)
{
TreeNode *newNode;
newNode = createNode();
newNode-data = data;
newNode-left = NULL;
newNode-right = NULL;
/*
if(ptr-data == data)
{
if(ptr-data data)
{
ptr-left = newNode;
}
else
{
ptr-right = newNode;
&nbssp;
}
}
}
*/
if(ptr - data data)
{
if(ptr-left == NULL)
{
ptr-left = newNode; // 비어있으니 새로운 노드를 삽입
}
//비어있지 않더라면
else{
ptr = ptr-left;
}
if(ptr-data data)
{
if(ptr-right == NULL){
ptr-right = newNode;
}
}
else{
ptr = ptr-right;
}
}
}int main(int argc, char *argv[])
{
TreeNode* root ;
root = createNode();
int i;
int in;
for(i=0;i3;i++){
printf(input : );
scanf(%d, &in);
insertNode(root,in);
} printf(\n inorder: );
inorder(root);
printf(\n preorder: );
system(PAUSE);
return 0;
}
-------------------------
위 소스가 있습니다 질문 내용입니다.
삽입 함수를 만들어서 main 에 재귀함수가 돌아가고있습니다 즉 그러므로 insertNode 삽입 함수 안에는
무한루프라던지 반복문이 들어가지 않고 전위 후위 중위 트리가 나와야됍니다..
어떻게 무한루프 반복문을 생성하지않고 만들어야할지..고민입니다..ㅠㅠ
도와주세요..!!
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700695 | 간단한 메모장 구현을 할려고 하는데요 (9) | 늘솜 | 2025-07-07 |
2700668 | c언어 질문입니다. 도와주세요~ (3) | 가자 | 2025-07-07 |
2700639 | 한글입력받아서 ㄱㄴㄷ순서대로출력하는법좀 | 두빛나래 | 2025-07-06 |
2700610 | 정말 기초적인 더하기,여백 문제 help | 무슬 | 2025-07-06 |
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |