디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

형들 이진트리 반복문을 통한 순회 코드인데요....

김성엽(203.253) 2012.01.09 20:43:58
조회 144 추천 0 댓글 6

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
 char data;
 struct Node *LeftChild;
 struct Node *RightChild;
 struct Node *next;
}Node;

Node *RootNode;
Node *topp;

int currentNodeCount = 0;

void push(Node *node)
{
 if(currentNodeCount == 0)
 {
  topp = (Node *)malloc(sizeof(Node));
  topp = node;
  topp->next = NULL;
  currentNodeCount++;
 }
 else
 {
  node->next = topp;
  topp = node;
  currentNodeCount++;
 }
}

Node *pop()
{
 Node *temp;
 if(currentNodeCount != 0)
 {
  temp = topp;
  printf("%c ", topp->data);
  topp = topp->next;
  currentNodeCount--;
  temp->next = NULL;
  return temp;
 }
}


void insertLeftChild(Node *New, Node *Parent)
{
 Parent->LeftChild = New;
 New->LeftChild = NULL;
 New->RightChild = NULL;
}

void insertRightChild(Node *New, Node *Parent)
{
 Parent->RightChild = New;
 New->LeftChild = NULL;
 New->RightChild = NULL;
}

Node* GetLeftChild(Node *Parent)
{
  if(Parent->LeftChild != NULL)
  {
   return Parent->LeftChild;
  }
  else
  {
   return NULL;
  }
}

Node* GetRightChild(Node *Parent)
{
 if(Parent->RightChild != NULL)
 {
  return Parent->RightChild;
 }
 else
 {
  return NULL;
 }
}

void deleteNode(Node *Parent)
{
 if(Parent != NULL)
 {
  deleteNode(Parent->LeftChild);
  deleteNode(Parent->RightChild);
  free(Parent);
 }
}

void deleteTree()
{
 deleteNode(RootNode);
}

void posttravel(Node *parent)
{
 static int count = 0;
 if(count == 0)
 {
  count++;
  posttravel(RootNode);
 }
 else
 {
  if(parent != NULL)
  {
  printf("%c ", parent->data);
  posttravel(parent->LeftChild);
  posttravel(parent->RightChild);
  }
 }
}

void midtravel(Node *parent)
{
 static int count = 0;
 if(count == 0)
 {
  count++;
  midtravel(RootNode);
 }
 else
 {
  if(parent != NULL)
  {
   midtravel(parent->LeftChild);
   printf("%c ", parent->data);
   midtravel(parent->RightChild);
  }
 }
}

void finaltravel(Node *parent)
{
 static int count = 0;
 if(count == 0)
 {
  count++;
  finaltravel(RootNode);
 }
 else
 {
  if(parent != NULL)
  {
   finaltravel(parent->LeftChild);
   finaltravel(parent->RightChild);
   printf("%c ", parent->data);
  }
 }
}
void main()
{
 Node *B, *C, *D, *E, *F, *G;
 Node *parent;
 Node *LeftChild;
 Node *RightChild;
 Node *Now;

 topp = (Node *)malloc(sizeof(Node));
 topp->next = NULL;
 RootNode = (Node *)malloc(sizeof(Node));
 RootNode->data = 'A';
 RootNode->LeftChild = NULL;
 RootNode->RightChild = NULL;
 

 B = (Node *)malloc(sizeof(Node));
 C = (Node *)malloc(sizeof(Node));
 D = (Node *)malloc(sizeof(Node));
 E = (Node *)malloc(sizeof(Node));
 F = (Node *)malloc(sizeof(Node));
 G = (Node *)malloc(sizeof(Node));
 B->data = 'B';
 C->data = 'C';
 D->data = 'D';
 E->data = 'E';
 F->data = 'F';
 G->data = 'G';
 
 insertLeftChild(B, RootNode);
 insertRightChild(C, RootNode);
 insertLeftChild(D, B);
 insertRightChild(E, B);
 insertLeftChild(F, C);
 insertRightChild(G, C);

 posttravel(RootNode);
 printf("\n");
 midtravel(RootNode);
 printf("\n");
 finaltravel(RootNode);
 printf("\n");

 push(RootNode);
 parent = RootNode;
 //여기부터
 while(1)
 {
  if(currentNodeCount == 0)
   break;
  else
  {
   Now = pop();
   
   if(RootNode->LeftChild != NULL)
    LeftChild = GetLeftChild(RootNode);
   if(RootNode->RightChild != NULL)
    RightChild = GetRightChild(RootNode);
   if(LeftChild != NULL)
   {
    push(RightChild);
   }
   if(RightChild != NULL)
   {
    push(LeftChild);
   }
  }
  free(Now);
 }
}
 
아마도 //여기부터 라고 쓴 부분에 잘못된 게 있는 것 같은데.......
재귀호출을 통한 순회는 잘 되거든요. 에휴 어디가 문젠지... 구원좀 부탁할게요 ㅠㅠ

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 대박 날 것 같아서 내 꿈에 나와줬으면 하는 스타는? 운영자 25/11/17 - -
AD 대학생 필수템! What's in my Bag 운영자 25/11/21 - -
298441 님들 해커 간지 내는법좀 알려주세여 [16] ㅇㅇ(121.132) 12.01.12 240 0
298440 프로그래밍 갤러리에도 공무원 생각중인 형들 있어? 광란의디씨질갤로그로 이동합니다. 12.01.12 94 0
298439 제가요 보안쪽으로 가려고 하는데요 [2] 장떡갤로그로 이동합니다. 12.01.12 103 0
298438 삼성소프트웨어멤버십?? 여기무슨 쓰레기집단이냐 [2] 아넬카(218.209) 12.01.12 382 2
298435 프로그래밍 하면 하드웨어도 잘뜯어고칠줄 안다? [9] 에헹헹헤(211.221) 12.01.12 224 0
298434 초봉 실수령 300이면 존나 많이 받는거? [1] 킁킁(183.99) 12.01.12 212 0
298433 비주알 정품 쓰는 사람 있음? [3] 1(211.197) 12.01.12 87 0
298432 나 연봉 4000만 되도 아이코 감사 핥핥 하면서 살게싿 [1] 1(211.197) 12.01.12 80 0
298431 다들꿈의 연봉이 얼마라고 생각함? [2] 부왘(183.99) 12.01.12 181 0
298430 웹프로그래밍 할줄 아는 형들.. 이 태그 뭐가 잘못된거? ㅇㅇ(115.86) 12.01.12 58 0
298429 운동 끝났어여 뿌우 SODMaster갤로그로 이동합니다. 12.01.12 45 0
298427 형아 한양대 컴공 신입생인대여 [6] 신입생ㅇㄴㅁ(124.54) 12.01.12 221 0
298426 혹시?? 독수리산 보셨어요//?//? 학교가자갤로그로 이동합니다. 12.01.12 40 0
298425 삼전 무선 초봉이 [1] 푸의미래갤로그로 이동합니다. 12.01.12 172 0
298424 삼성 멤버십 기술면접 후기 [6] ㅅㅅㅅ(182.208) 12.01.12 434 0
298423 형들 자바 질문 있어요 초보(203.241) 12.01.12 58 0
298422 미국 회사 분위기 VS 한국 회사 분위기 [2] 1(211.197) 12.01.12 212 0
298421 나름대로 희망적인 말을 들엇는데 [5] 신미애갤로그로 이동합니다. 12.01.12 211 0
298419 유럽은 17살부터 일할수 있음 1(211.197) 12.01.12 58 0
298417 내일 세미나 자료준비중인데..키노트 정말 괜찮네 ㅋ [1] 캐맥닭(115.92) 12.01.12 96 0
298416 신조어 만들어 내자 다운증후군 1(211.197) 12.01.12 46 0
298414 여자가 병신인 이유 [1] 1(211.197) 12.01.12 101 0
298413 SM에 대해 어찌 생각해?? [1] 거칠게갤로그로 이동합니다. 12.01.12 154 0
298411 님들 중에 운동 하시는분? [3] 1(211.197) 12.01.12 74 0
298410 비수알 2010 prenium 받는중 1(211.197) 12.01.12 39 0
298409 스티브잡스는 97년 애플으로 돌아온 뒤에 단 1주도 매각하지 않았다 ㅁㄴㄻㄹ갤로그로 이동합니다. 12.01.12 87 0
298408 프로그래밍하는 형들은 성격이 내성적이지만 않으면 좋을텐데 [6] 광란의디씨질갤로그로 이동합니다. 12.01.12 252 0
298406 난 일단 운동해야 하니두시간 후에 오겠습니다. [1] SODMaster갤로그로 이동합니다. 12.01.12 45 0
298405 아니 나 게이컨셉 버렸어. [5] SODMaster갤로그로 이동합니다. 12.01.12 73 0
298404 쿄스케형은 아스형 얼굴 알지? [4] SODMaster갤로그로 이동합니다. 12.01.12 72 0
298403 webGL 지원되는 브라우저 뭐있냐? [2] 웹지엘(183.1) 12.01.12 101 0
298402 형들 질문좀 쒦뚫쉚뚫샒휆뚫뚫쒦갤로그로 이동합니다. 12.01.12 38 0
298401 우리는 imf 세대 아니냐 [1] 거칠게갤로그로 이동합니다. 12.01.12 101 0
298400 근데 아스횽이 그렇게 잘생겼다매? [3] SODMaster갤로그로 이동합니다. 12.01.12 65 0
298398 이러니 죄다 대기업, 공기업, 공뭔 이야기 하는거다. [4] 거칠게갤로그로 이동합니다. 12.01.12 229 0
298393 어쨋든 다시, 쿄스케형이 남자를 끊다니 이게 무슨소리? [5] SODMaster갤로그로 이동합니다. 12.01.12 85 0
298392 야잌 짤이 뭐 어떻다고 지우냐 알바야! SODMaster갤로그로 이동합니다. 12.01.12 54 0
298391 쎾쓰들아 5900도 못받는 애들은 칼퇴하자!!! 거칠게갤로그로 이동합니다. 12.01.12 129 0
298389 1년전에 SK T-TOWER에 두달 정도 출근했었는데 말야..... [2] 야요이갤로그로 이동합니다. 12.01.12 657 0
298388 깝깝하네 [6] 퐈응퐈응갤로그로 이동합니다. 12.01.12 97 0
298387 나도 피해자다 개심심(61.73) 12.01.12 45 0
298386 나도피해자다..근데 엿같은 중고등학교교육 싴보이갤로그로 이동합니다. 12.01.12 71 0
298385 아 진짜 솔까 교육문제도 터질때 되지 않았냐? [3] 쒦뚫쉚뚫샒휆뚫뚫쒦갤로그로 이동합니다. 12.01.12 113 0
298384 컴공 전망이 어떤가요?? [2] 질문좀요(121.167) 12.01.12 458 0
298383 나도 주입식,암기식 교육의 피해자지.... [9] 야요이갤로그로 이동합니다. 12.01.12 228 0
298381 오늘은 교육드립인가,,, [1] dot(125.128) 12.01.12 66 0
298379 역시 대세는 자바인가.........C#은 잉여? [2] 거칠게갤로그로 이동합니다. 12.01.12 200 0
298376 대학가면은 컴공과가면은잇자나 [5] 코딩...?(58.145) 12.01.12 194 0
298375 형님들 물어볼게 있습니다. [3] TIT(58.234) 12.01.12 63 0
298374 신학교 > 길드 > 공방 > 군국주의식 교육 > 변질된 대한민국 교육 [3] 1(183.104) 12.01.12 74 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2