#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_TREE_SIZE 100 //二叉树的最大结点数 #define OK 1 typedef int Status; typedef char DataType; typedef struct Node { DataType data; struct Node *lchild,*rchild; }BinTNode,*BinTree; Status CreateBiTree(BinTree &bt) {//按照先序遍历次序递归建立二叉树 //ABC##DE#G##F## char ch; scanf("%c",&ch); if(ch == '#') bt = NULL; else { bt = (BinTNode*)malloc(sizeof(BinTNode)); bt->data = ch; //生成根结点 CreateBiTree(bt->lchild); //构造左子树 CreateBiTree(bt->rchild); //构造右子树 } return OK; } void PreOrderTraverse(BinTree bt) {//二叉树先序遍历递归操作 if(bt) { printf("%3c",bt->data); PreOrderTraverse(bt->lchild); PreOrderTraverse(bt->rchild); } } void InOrderTraverse(BinTree bt) {//二叉树中序遍历递归操作 if(bt) { InOrderTraverse(bt->lchild); printf("%3c",bt->data); InOrderTraverse(bt->rchild); } } void PosOrderTraverse(BinTree bt) {//二叉树后序遍历递归操作 if(bt) { PosOrderTraverse(bt->lchild); PosOrderTraverse(bt->rchild); printf("%3c",bt->data); } } void InOrder(BinTree bt) {//二叉树中序遍历非递归操作 BinTNode *stack[MAX_TREE_SIZE]; //定义栈数组 int top = 0; stack[top] = bt; do { while(NULL!=stack[top]) {//扫描根结点及其所有的左结点并入栈 top = top+1; stack[top] = stack[top-1]->lchild; } top = top-1; //退栈 if(top>=0) //判断栈是否为空 { printf("%3c",stack[top]->data); //访问结点 stack[top] = stack[top]->rchild; //扫描右子数 } }while(top>=0); return; } //void PreOrder(BinTree bt) //{//二叉树先序遍历非递归操作 //} int Size(BinTree bt) {//统计二叉树中所有结点的个数 int num1,num2; if(bt==NULL) return 0; else if(bt->lchild==NULL && bt->rchild==NULL) return 1; else { num1 = Size(bt->lchild); num2 = Size(bt->rchild); return(num1+num2+1); } } int LeafCount(BinTree bt) {//统计二叉树的叶子结点数 int LeafNum; if(bt==NULL) LeafNum = 0; else if((bt->lchild==NULL) && (bt->rchild==NULL)) LeafNum = 1; else LeafNum = LeafCount(bt->lchild)+LeafCount(bt->rchild); //叶子结点数为左右子树之和 return LeafNum; } int Depth(BinTree bt) {//统计二叉树的深度 int hl,hr,max; if(bt!=NULL) { hl = Depth(bt->lchild); //求左子树的深度 hr = Depth(bt->rchild); //求右子树的深度 max = hl>hr?hl:hr; return(max+1); //返回树的深度 } else return 0; } void Exchange(BinTree bt) {//交换左右二叉树 if(bt == NULL) return; BinTNode *temp; temp = bt->lchild; bt->lchild = bt->rchild; bt->rchild = temp; Exchange(bt->lchild); Exchange(bt->rchild); } void main(int argc,char *argv[]) { BinTree bt; int xz = 1; int yz,sd; while(xz) { printf("二叉树的建立及其基本操作\n"); printf("=============================\n"); printf("1,建立二叉树的存储结构\n"); printf("2,二叉树的基本操作\n"); printf("3,交换二叉树的左右\n"); printf("0,退出系统\n"); printf("=============================\n"); printf("请选择:(0-3)\n"); scanf("%d",&xz); getchar(); switch(xz) {//输入:ABC##DE#G##F### case 1: printf("输入二叉树的先序序列结点值:\n"); CreateBiTree(bt); printf("二叉树的链式存储结构已经建立完成\n"); printf("\n"); break; case 2: printf("该二叉树的先序遍历序列是:"); PreOrderTraverse(bt); printf("\n"); //输出ABCDEGF printf("该二叉树的中序遍历序列是:"); InOrderTraverse(bt); printf("\n"); //输出CBEGDFA printf("该二叉树的后序遍历序列是:"); PosOrderTraverse(bt); printf("\n"); //输出CGEFDBA printf("该二叉树的中序非递归遍历序列是;"); InOrder(bt); printf("\n"); //输出CBEGDFA printf("该二叉树的结点个数是;%d\n",Size(bt)); yz = LeafCount(bt); printf("该二叉树的叶子结点个数是:%d\n",yz); sd = Depth(bt); printf("该二叉树的深度是:%d\n",sd); printf("\n"); break; case 3: Exchange(bt); printf("该二叉树已交换左右字树:\n"); printf("\n"); break; case 0: break; } } }