[C/C++] 先序建立二叉树| 先序、中序、后序遍历二叉树| 求二叉树深度、节点数、叶节点数 算法实现

 

/* * BinTree.h */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; //Status是函数的类型,其值是函数结果状态代码 /* 定义二叉树结点类型 */ typedef char ElemType; /* 定义二叉树的数据结构 */ typedef struct BTreeNode { ElemType data; // 存储节点值 struct BTreeNode* lchild; // 左孩子指针 struct BTreeNode* rchild; // 右孩子指针 }BiTNode, BiTree; /* 先序建立二叉树 */ BiTree *CreateBiTree(BiTree *T); /* 先序遍历二叉树 */ Status PreOrderTraverse(BiTree *head); /* 中序遍历二叉树 */ Status InOrderTraverse(BiTree *head); /* 后序遍历二叉树 */ Status PostOrderTraverse(BiTree *head); /* 求二叉树的深度 */ int BTreeDepth(BiTree *BT); /* 求二叉树所有结点数 */ int BTreeCount(BiTree *BT); /* 求二叉树中所有叶子结点数 */ int BTreeLeafCount(BiTree *BT);  

//------------------------------------------------------------------------------------------------------------------------------------------

 

/* * BinTree.c */ #include "BinTree.h" #include <stdio.h> #include <stdlib.h> BiTree *CreateBiTree(BiTree *T) { char ch; fflush(stdin); if((ch=getchar()) == '#') return NULL; T = (BiTree *)malloc(sizeof(BiTree)); T->data = ch; T->lchild = CreateBiTree(T->lchild); T->rchild = CreateBiTree(T->rchild); return T; } Status PreOrderTraverse(BiTree *head) { if(head == NULL) return 0; printf("%c, ", head->data); PreOrderTraverse(head->lchild); PreOrderTraverse(head->rchild); return 1; } Status InOrderTraverse(BiTree *head) { if(head == NULL) return 0; InOrderTraverse(head->lchild); printf("%c, ", head->data); InOrderTraverse(head->rchild); return 1; } Status PostOrderTraverse(BiTree *head) { if(head == NULL) return 0; InOrderTraverse(head->lchild); InOrderTraverse(head->rchild); printf("%c, ", head->data); return 1; } int BTreeDepth(BiTree *BT) { int iL, iR; if(BT == NULL) return 0; iL = 1 + BTreeDepth(BT->lchild); iR = 1 + BTreeDepth(BT->rchild); return iL > iR ? iL : iR; } int BTreeCount(BiTree *BT) { if(BT->lchild == NULL && BT->rchild == NULL) return 1; if(BT->lchild == NULL) return BTreeCount(BT->rchild) + 1; if(BT->rchild == NULL) return BTreeCount(BT->lchild) + 1; return BTreeCount(BT->lchild) + BTreeCount(BT->rchild) + 1; } int BTreeLeafCount(BiTree *BT) { if(BT->lchild == NULL && BT->rchild == NULL) return 1; if(BT->lchild == NULL) return BTreeLeafCount(BT->rchild); if(BT->rchild == NULL) return BTreeLeafCount(BT->lchild); return BTreeLeafCount(BT->lchild) + BTreeLeafCount(BT->rchild); } 

 

 

 

//------------------------------------------------------------------------------------------------------------------------------------------

 

/* * BiT_main.cpp */ #include<iostream.h> #include"BinTree.c" void main() { cout<<"二叉树的实现操作!"<<endl; cout<<"This is the programme to test Bitree!"<<endl; BiTree *BT; BT = CreateBiTree(BT); cout<<"PreOrderTraverse:"<<endl; PreOrderTraverse(BT); cout<<endl; cout<<"InOrderTraverse:"<<endl; InOrderTraverse(BT); cout<<endl; cout<<"PostOrderTraverse:"<<endl; PostOrderTraverse(BT); cout<<endl; cout<<"The depth of this BITREE is: "<<BTreeDepth(BT)<<endl; cout<<"It has "<<BTreeCount(BT)<<" nodes, "; cout<<BTreeLeafCount(BT)<<" of them are leaves."<<endl; } 

 

 

 

你可能感兴趣的:([C/C++] 先序建立二叉树| 先序、中序、后序遍历二叉树| 求二叉树深度、节点数、叶节点数 算法实现)