#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR 0;
#define OVERFLOW -1;
#define INFEASIBLE -2;
typedef int Status;
typedef char Elemtype;
typedef struct BiTNode{
Elemtype data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status InitBiTree(BiTree &BT){
BT = NULL;
return OK;
}
Status CreateBiTree(BiTree &T){
int ch;
scanf("%c",&ch);
if(ch == ' ') T = NULL;
else{
if( !( T = (BiTNode *)malloc(sizeof(BiTNode) ) ) )
// exit(OVERFLOW);
int k = 1;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
Status PreOrder1(BiTree T){ //先序遍历
if(T){
if(!(T->data)) return ERROR;
printf("%c",T->data);
PreOrder1(T->lchild);
PreOrder1(T->rchild);
return OK;
}
}
Status PreOrder2(BiTree T){ //中序遍历
if(T){
if(!(T->data)) return ERROR;
PreOrder2(T->lchild);
printf("%c",T->data);
PreOrder2(T->rchild);
return OK;
}
}
Status PreOrder3(BiTree T){ //后序遍历
if(T){
if(!(T->data)) return ERROR;
PreOrder3(T->lchild);
PreOrder3(T->rchild);
printf("%c",T->data);
return OK;
}
}
Status Ponter(BiTree T){//求双分支节点数
int l,r;
if(T==NULL)
return 0;
else{
l = Ponter(T->lchild);
r = Ponter(T->rchild);
if(T->lchild!=NULL&&T->rchild!=NULL)
return l+r+1;
else return l+r;
}
}
int main()
{
BiTree BT;
InitBiTree(BT);//构建空的二叉树
printf("请输入节点数据\n");
CreateBiTree(BT);//先序构建二叉树
printf("\n先序遍历结果为:");
PreOrder1(BT);//先序遍历
printf("\n中序遍历结果为:");
PreOrder2(BT);//中序遍历
printf("\n后序遍历结果为:");
PreOrder3(BT);//后序遍历
printf("\n双节点数为:%d",Ponter(BT));
return 0;
}
11万+

被折叠的 条评论
为什么被折叠?



