디시인사이드 갤러리

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

갤러리 본문 영역

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

김성엽(203.253) 2012.01.09 20:43:58
조회 139 추천 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 겨울가전 SALE! 쿨한 겨울 HOT세일 운영자 25/11/12 - -
298011 정식 SE 데뷔!!! [9] 씨발라드세요갤로그로 이동합니다. 12.01.10 139 0
298010 html 태그 질문 드릴께요. iframe [8] ares4you갤로그로 이동합니다. 12.01.10 88 0
298009 어려운 질문은 아닌데 왠지 알아두어야 될 것 같아서 질문드려요 [14] C++(112.172) 12.01.10 108 0
298008 자바 질문 좀 드릴게요ㅠㅠㅠ [2] 끼용(14.36) 12.01.10 55 0
298007 '甲-乙-丙 하청구조'…단계마다 인건비 따먹기 [1] 거칠게갤로그로 이동합니다. 12.01.10 143 0
298003 현업 언니들 사장이 [4] dot(125.128) 12.01.10 137 0
298001 니들 이름은 어떻게 결정한거냐? [13] iljeomobolt갤로그로 이동합니다. 12.01.10 161 0
298000 대학원 질문 [2] 리누슽발즈(220.70) 12.01.10 127 0
297997 플밍배열에 관하여 질문드립니다. [3] 플밍꼬꼬마(221.165) 12.01.10 77 0
297996 카르마1해보신분 손 [5] Forit갤로그로 이동합니다. 12.01.10 88 0
297995 파견 나가는데 아오 맨붕 [2] 개떙보갤로그로 이동합니다. 12.01.10 104 0
297993 나 이것좀 풀어줘 [9] 고기호빵갤로그로 이동합니다. 12.01.10 114 0
297991 영어 한문장만 물어볼게 횽들... [13] 캐영어닭(115.92) 12.01.10 121 0
297990 우리 회사 병특 현역 1명 뽑는당... [6] ㅇㅇㅇ(59.13) 12.01.10 193 0
297989 근데 말야 자동 수강신청프로그램같은거 [7] 싴보이갤로그로 이동합니다. 12.01.10 209 0
297988 횽들.......JSP 도움좀 [9] 헬프욤(121.138) 12.01.10 116 0
297987 오늘 면접보러감 꿀레(121.168) 12.01.10 94 0
297986 html5가 따로 배워야할 수준이냐? html4랑 많이 틀림? [2] ㄴㄹㅇ(218.48) 12.01.10 203 0
297985 프로그래밍에서 중요한것. 10년차임 [3] sstt(110.14) 12.01.10 207 1
297984 나 먼가 장애있는거야 형들? 크롬 탭이 매일 20개가 넘어감 [5] 엌ㅋ(112.223) 12.01.10 117 0
297983 회사에서 웹디자이너 한테 HTML5 배우래 [5] (112.76) 12.01.10 203 0
297982 C# 질문점 리누슽발즈(220.70) 12.01.10 48 0
297981 티스토리 초대장 하나만주셍요 굽신굽신 [1] ㅁㅁㅁ(211.232) 12.01.10 56 0
297980 HTML5에서 아이폰 바탕화면 드래그(?) 같은걸 구현해야하는데 [2] 흑흑(112.223) 12.01.10 89 0
297978 table 이 졸라 많은 한국 웹싸이트.. [1] iljeomobolt갤로그로 이동합니다. 12.01.10 103 0
297976 어우 졸려... [1] ㅋㄱ(183.96) 12.01.10 54 0
297975 성대아싸횽 [7] ♪♩안봉봉♬갤로그로 이동합니다. 12.01.10 139 0
297974 형들 웹하드에서 게임 돈주고 다운받았는데 멀티가 안되요;; [10] 도와주세요(115.23) 12.01.10 144 0
297973 윈도폰 개발공부중인데 공부 참조할만한 커뮤니티 아시는거좀.. [3] 파괴대마왕갤로그로 이동합니다. 12.01.10 63 0
297970 재귀썌끼 존나 맘에 안들어요.. [2] 뇌성능(61.80) 12.01.10 104 0
297969 아무래도 인간에 머릿속에 여러가지 칩들을 넣어야될것같아 [2] lucy_han갤로그로 이동합니다. 12.01.10 54 0
297968 궁금한게 있어서 물어봤는데요... [1] 뇌성능(61.80) 12.01.10 68 0
297966 연산자 오버로딩에 대해 자세히 나와있는 글 없나 [1] elwlwlwk갤로그로 이동합니다. 12.01.10 69 0
297964 안녕하세요 C에 대해서 좀 여쭤보겠습니다^^ [3] 몽키C갤로그로 이동합니다. 12.01.10 97 0
297963 뇌성능이 딸리는게 이런거임? [5] 뇌성능(61.80) 12.01.10 159 0
297960 VB보다 RPG만들기가 프로그래밍 입문으로 더 좋은듯 [6] 영어쓰던남자갤로그로 이동합니다. 12.01.10 188 0
297959 스레드 문제인 것 같은데 어디가 문제인지 모르겠음-_- [1] ㅋㄱ(183.96) 12.01.10 65 0
297958 몇 몇 높으신분들과 경력쌓은분들은 참 신기해... [7] 참신기해(211.221) 12.01.10 171 0
297956 ASSA3d 엔진 샌드박스 [17] [성대아싸]갤로그로 이동합니다. 12.01.10 159 1
297955 네이버는 운이 따라서 발전한 웹이냐? [4] ;ㅇㄴㄻ(121.173) 12.01.10 125 0
297954 touching..........// Good..!!! 학교가자갤로그로 이동합니다. 12.01.10 41 0
297953 소드마스터님 [24] 헬프미(125.177) 12.01.10 163 0
297952 이정도 스펙이면 대기업si 뚫을수있냐 [8] 평가좀(110.15) 12.01.10 583 0
297951 모바일 웹페이지(스마트폰용)을 개발할려고 하는데 책좀 추천해줘.. (112.152) 12.01.10 80 0
297950 부모님 결혼기념일 선물 적당한거 머 있을까요~? [3] 양랩(220.119) 12.01.10 115 0
297949 나 프로그래밍에 소질이 없는거지? [6] C초보 ㅋ(121.157) 12.01.10 210 0
297948 정중하게 부탁드립니다 [9] 헬프미(125.177) 12.01.10 117 0
297945 2차원 배열을 해서 곱을 구하는거같은데 핼프좀 [1] C초보 ㅋ(121.157) 12.01.09 87 0
297943 간단한 자바스크립트 질문여.. [3] Partyup갤로그로 이동합니다. 12.01.09 86 0
297942 게임 프로그래밍 하기 좋은 언어랑 책좀 추천요 [2] 야요이갤로그로 이동합니다. 12.01.09 108 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2