이 진 트 리 의 구축 과 세 가지 옮 겨 다 니 기 (선서, 중 서, 후 서)

요 며칠 동안 청명절 에 방학 을 해서 학습 효율 이 매우 낮 습 니 다. 이 진 트 리 의 구축 에 있어 지침 의 사용 이 저 를 기분 나 쁘 게 했 습 니 다. 저 는 지침 에 대한 이해 가 아직 얕 습 니 다.
해결 해 야 할 문제:
http://bbs.51cto.com/viewthread.php?tid=1103326&extra=page%3D1&frombbs=1
   높 은 사람 에 게 잘못된 방향 을 지적 해 주시 기 바 랍 니 다.
  이 진 트 리 의 저장 소 는 이 진 트 리 구 조 를 사용 합 니 다.
typedef struct node{
    char data;
    struct node *lchild, *rchild;
}btree, *bitree;

(저 는 Liux C 프로 그래 밍 을 배우 고 있 기 때문에 프로 그래 밍 스타일 은 linxu c 프로 그래 밍 스타일 을 선 호 합 니 다.)
다음은 모든 코드 입 니 다:
#include<malloc.h>
#include<stdio.h>
typedef struct node{
    char data;
    struct node *lchild, *rchild;
}btree, *bitree;
//       
void  preorder(bitree root)
{
    if(root)
    {
        printf("%3c", root->data);
        preorder(root->lchild);
        preorder(root->rchild);
    }
}
//       
int inorder(bitree root)
{
    if(root)
    {
        inorder(root->lchild);
        printf("%3c", root->data);
        inorder(root->rchild);
    }
}
//       
void postorder(bitree root)
{
    if(root)
    {
        postorder(root->lchild);
        postorder(root->rchild);
        printf("%3c", root->data);
    }
}
//     
void create_tree(bitree *root)
{
    char c;
    c = getchar();
    if(c == '.')
        *root = NULL;
    else{
        *root = (bitree)malloc(sizeof(btree));
        (*root)->data = c;
        create_tree(&(*root)->lchild);
        create_tree(&(*root)->rchild); 
    }
                                              
}
int main()
{ 
    bitree tree;
    printf("enter you tree 
"); create_tree(&tree); printf("create done
"); printf(" preorder start: "); preorder(tree); printf(" preorder end
"); printf(" inorder start: "); inorder(tree); printf(" inorder end
"); printf(" postorder start: "); postorder(tree); printf(" postorder end
"); return 0; }

좋은 웹페이지 즐겨찾기