首页 > C/C++语言 > C/C++数据结构 > 二叉树实现源代码
2006
06-05

二叉树实现源代码

二叉树实现源代码如下:

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

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int status;

typedef struct BiNode
{
    char Data;
    struct BiNode* lChild;
    struct BiNode* rChild;
}BiNode,*pBiNode;

status CreateTree(BiNode** pTree);
status PreOrderTraval(BiNode* pTree);
status Visit(char Data);
status Display(BiNode* pTree,int Level);
status Clear(BiNode* pTree);

BiNode *pRoot=NULL;

main()
{
    clrscr();
    CreateTree(&pRoot);

    printf(“\nPreOrder:”);
    PreOrderTraval(pRoot);
    printf(“\n”);

    printf(“\nInOrder:”);
    InOrderTraval(pRoot);
    printf(“\n”);

    printf(“\nPostOrder:”);
    PostOrderTraval(pRoot);
    printf(“\n”);

    printf(“\nShowLeaves:”);
    ShowLeaves(pRoot);
    printf(“\n———————–\n” );
    printf(“\n”);

    Display(pRoot,0);

    printf(“\n”);
    printf(“\nDeleting Tree:\n”);
    DelTree(pRoot);
    printf(“BiTree Deleted.”);

    getch();
}
status CreateTree(BiNode** pTree) /*Input Example: abd##e##cf##g##*/
{
    char ch;
    scanf(“%c”,&ch);
    if(ch==‘#‘)
    {
        (*pTree)=NUL L;
    }
    else
    {
        if(!((*pTree )=(BiNode*)malloc(sizeof(BiNode))))
        {
             exit(OVERFLOW);
        }
        (*pTree)-&gt ;Data=ch;
        CreateTree(&((*pTree)->lChild));
        CreateTree(&((*pTree)->rChild));
    }
return OK;
}
status PreOrderTraval(BiNode* pTree)
{
    if(pTree)
    {
        if(Visit(pTr ee->Data))
        {
             if(PreOrderTraval(pTree->lChild))
             {
                 if(PreOrderTraval(pTree- >rChild))
                  {
                      return OK;
                 }
             }
        }
        return ERROR;
    }
    else
    {
        return OK;
    }
}
status InOrderTraval(BiNode* pTree)
{
    if(pTree)
    {
        if(InOrderTr aval(pTree->lChild))
        {
             if(Visit(pTr ee->Data))
             {
                 if(InOrderTraval(pTree-& gt;rChild))
                  {
                      return OK;
                 }
             }
             return ERROR;
        }
        return ERROR;
    }
    else
    {
        return OK;
    }
}
status PostOrderTraval(BiNode* pTree)
{
    if(pTree)
    {
        if(PostOrder Traval(pTree->lChild))
        {
             if(PostOrderTraval(pTree->rChild))
             {
                 if(Visit(pTr ee->Data))
                  {
                      return OK;
                 }
                  return ERROR;
             }
        }
        return ERROR;
    }
    else
    {
        return OK;
    }
}
status Visit(char Data)
{
    printf(“%c”,Data);
    return OK;
}
status Display(BiNode* pTree,int Level)
{
    int i;
    if(pTree==NULL) return;
    Display(pTree->lChild,Level+1);
    for(i=0;i<Level-1;i++)
    {
        printf(” “);
    }
    if(Level>=1)
    {
        printf(“–”) ;
    }
    printf(“%c\n”,pTree->Data);
    Display(pTree->rChild,Level+1);
}
status ShowLeaves(BiNode* pTree)
{
    if(pTree)
    {
        if(ShowLeave s(pTree->lChild))
        {
             if(ShowLeaves(pTree->rChild))
             {
                 if((pTree->lChild==NU LL)&&(pTree->rChild==NULL))
                  {
                      if(!Visit(pTree->Data))
                      {
                           return ERROR;
                      }
                 }
                  return OK;
             }
        }
        return ERROR;
    }
    else
    {
        return OK;
    }
}
status DelTree(BiNode* pTree)
{
    if(pTree)
    {
        if(DelTree(p Tree->lChild))
        {
             if(DelTree(pTree->rChild))
             {
                 printf(“Deleting %c\n”,pTree->Data);
                 free((void*)pTree);
                  return OK;
             }
        }
        return ERROR;
    }
    else
    {
        return OK;
    }


二叉树实现源代码》有 9 条评论

  1. 心随梦-16岁的过错 说:

    看不懂啊!

  2. powers7 说:

    看晕了!!!!!!!!

  3. yuqangy 说:

    大哥请问您是学五级C的还是六级

  4. greed_4 说:

    晕啊,居然不加注释

  5. greed_4 说:

    递归?!

  6. luowei01051145 说:

    在VC 6.0 下编译出现N多错误!

    楼主试一下!

  7. whos 说:

    楼上的,拜托这是C语言,不是C++。

  8. analog2006 说:

    搞编程的都不看数据结构么..这个也要在论坛上搞一搞

  9. IFandFOR 说:

    最近再看数据结构,遇到一些问题呀,关于遍历的.

留下一个回复