<b>二叉树实现源代码</b>[VC/C++编程]
本文“<b>二叉树实现源代码</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
二叉树实现源代码以下:
#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)=NULL;
}
else
{
if(!((*pTree)=(BiNode*)malloc(sizeof(BiNode))))
{
exit(OVERFLOW);
}
(*pTree)->Data=ch;
CreateTree(&((*pTree)->lChild));
CreateTree(&((*pTree)->rChild));
}
return OK;
}
status PreOrderTraval(BiNode* pTree)
{
if(pTree)
{
if(Visit(pTree->Data))
{
if(PreOrderTraval(pTree->lChild))
{
if(PreOrderTraval(pTree->rChild))
{
return OK;
}
}
}
return ERROR;
}
else
{
return OK;
}
}
status InOrderTraval(BiNode* pTree)
{
if(pTree)
{
if(InOrderTraval(pTree->lChild))
{
if(Visit(pTree->Data))
{
if(InOrderTraval(pTree->rChild))
{
return OK;
}
}
return ERROR;
}
return ERROR;
}
else
{
return OK;
}
}
status PostOrderTraval(BiNode* pTree)
{
if(pTree)
{
if(PostOrderTraval(pTree->lChild))
{
if(PostOrderTraval(pTree->rChild))
{
if(Visit(pTree->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(ShowLeaves(pTree->lChild))
{
if(ShowLeaves(pTree->rChild))
{
if((pTree->lChild==NULL)&&(pTree->rChild==NULL))
{
if(!Visit(pTree->Data))
{
return ERROR;
}
}
return OK;
}
}
return ERROR;
}
else
{
return OK;
}
}
status DelTree(BiNode* pTree)
{
if(pTree)
{
if(DelTree(pTree->lChild))
{
if(DelTree(pTree->rChild))
{
printf("Deleting %c\n",pTree->Data);
free((void*)pTree);
return OK;
}
}
return ERROR;
}
else
{
return OK;
}
}
以上是“<b>二叉树实现源代码</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
- ·上一篇文章:C++的布尔范例
- ·下一篇文章:C++的算符重载
- ·中查找“<b>二叉树实现源代码</b>”更多相关内容
- ·中查找“<b>二叉树实现源代码</b>”更多相关内容