64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream.h>
#include <stdlib.h>
typedef struct BiNode
{
char data;
struct BiNode *lchild;
struct BiNode *rchild;
} BiNode; //分号
typedef BTNode *BinTree; //分号
void createBinTree(BinTree *root)//以先序来建立二叉树
{
char ch;
if((ch=getchar())==' ')//这个代表空格,可换别的字符
*root=NULL; //建立空二叉树
else
{
*root=(BTNode*)new(sizeof(BTNode));//开辟空间,生成节点
(*root)->data=ch;
CreateBinTree(&((*root)->lchild));
CreateBinTree(&((*root)->rchild));
}
}
void PreOrder(BinTree root)//先序遍历
{
if(root!=NULL)
{
cout < < root->data;//访问根节点
PreOrder(root->lchild);//遍历左子树
PreOrder(root->rchild);//遍历右子树
}
}
vygoid InOrder(BinTree root)//中序遍历
{
if(root!=NULL)
{
PreOrder(root->lchild); //遍历左子树
cout < <root->data;//访问根节点
PreOrder(root->rchild);//遍历右子树
}
}
void PostOrder(BinTree root)//后序遍历
{
if(root!=NULL)
{
PreOrder(root->lchild);//遍历左子树
PreOrder(root->rchild);//遍历右子树
cout < <root->data);//访问根节点
}
}
void main()
{
BinTree root;
CreateBinTree(&root);
cout < <先序:;
PreOrder(root);
cout < <\n;
cout < <中序:;
InOrder(root);
cout < <\n;
cout < <后序:;
PostOrder(root);
cout < <\n;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode {
char data;
struct BiTNode *Lchild, *Rchild; /* 左、右孩子指针 */
} *BiTree;
void CreateBiTree(struct BiTNode* T){
/* 在先序遍历二叉树的过程中输入二叉树的"先序字符串", */
/* 建立根指针为 T的二叉链表存储结构。在先序字符串中, */
/* 字符'#'表示空树,其它字母字符为结点的数据元素 */
char ch;
scanf("%c",&ch);
if (ch=='#'){
T=NULL; /* 建空树 */
}
else
{
T = (struct BiTNode*)malloc(sizeof(struct BiTNode)) ; /* "访问"操作为生成根结点 */
T->data = ch;
CreateBiTree(T->Lchild); /* 递归建(遍历)左子树 */
CreateBiTree(T->Rchild); /* 递归建(遍历)右子树 */
}/* else */
}/* CreateBiTree */
/* 先序遍历以T为根指针的二叉树 */
void PreOrder(struct BiTNode* T){
if(T){ /* T=NULL时,二叉树为空树,不做任何操作 */
printf("%c ",T->data);
PreOrder(T->Lchild); /* 先序遍历左子树 */
PreOrder(T->Rchild); /* 先序遍历右子树 */
}/* if */
}
/* 中序遍历以T为根指针的二叉树 */
void InOrder(struct BiTNode* T){
if(T){ /* T=NULL时,二叉树为空树,不做任何操作 */
InOrder(T->Lchild); /* 先序遍历左子树 */
printf("%c",T->data); /* 通过函数指针 *visit 访问根结点 */
InOrder(T->Rchild); /* 先序遍历右子树 */
}/* if */
}
/* 后序遍历以T为根指针的二叉树 */
void PostOrder(struct BiTNode* T){
if(T){ /* T=NULL时,二叉树为空树,不做任何操作 */
PostOrder(T->Lchild); /* 先序遍历左子树 */
PostOrder(T->Rchild); /* 先序遍历右子树 */
printf("%c ",T->data); /* 通过函数指针 *visit 访问根结点 */
}/* if */
}
/* 先序遍历二叉树,以 count 返回二叉树中叶子结点的数目 */
void CountLeaf(struct BiTNode* T, int &count){
if (T) {
if ((!T->Lchild)&& (!T->Rchild))
count++; /* 对叶子结点计数 */
CountLeaf( T->Lchild, count);
CountLeaf( T->Rchild, count);
} /* if */
} /* CountLeaf */
/* T指向二叉树的根,level 为 T 所指结点所在层次, */
/* 其初值为1,depth 为当前求得的最大层次,其初值为0 */
void BiTreeDepth(struct BiTNode* T, int level, int &depth){
if (T){
if (level>depth) depth=level;
BiTreeDepth(T->Lchild, level+1, depth);
BiTreeDepth(T->Rchild, level+1, depth);
}/* if */
}/* BiTreeDepth */
int main()
{
printf("请依次输入字符: AB#CD###E#F##\n");
/*BiTree T;*/
BiTNode* T;
CreateBiTree(T);
printf("先序遍历\n");
PreOrder(T);
printf("中序遍历: \n");
InOrder(T);
printf("后序遍历: \n");
PostOrder(T);
int count = 0;
CountLeaf (T,count);
printf("此二叉树叶子节点个数: %d\n",count);
int depth = 0;
BiTreeDepth(T,1,depth);
printf("此二叉树深度: %d\n",depth);
return (0);
}