下面是用鏈表做的一個二叉樹,包含了二叉樹的創建、先序遍歷,中序遍歷,後序遍歷的遞歸操作和非遞歸操作,還有求二叉樹的的高度,根節點到某一節點的路徑,葉子結點個數和總節點個數。
萬年的頭文件
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_SIZE 100
using namespace std;
二叉樹的鏈式存儲結構:
typedef char TElemType;
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild,*rchlid;//左子女和又子女
}BiTNode,*BiTree;
創建二叉樹,並求二叉樹總結點數
int CreateBiTree(BiTree &BT,int &count_Node){//創建二叉樹
char ch;
cout<<"請輸入當前節點值:";
cin>>ch;
if (ch=='#')//#表示空
BT=NULL;
else{
if(!(BT=(BiTree)malloc(sizeof(BiTNode)))) exit (-1);//如果沒有成功的申請到內存空間則結束程序
BT->data=ch;
count_Node++;
CreateBiTree(BT->lchild,count_Node);
CreateBiTree(BT->rchlid,count_Node);
}
return 1;
}
求葉子結點個數
void CountLeaves(BiTree BT,int &count_leaves){//葉子節點個數
if(BT){
if(!BT->lchild&&!BT->rchlid) count_leaves++;
CountLeaves(BT->lchild,count_leaves);
CountLeaves(BT->rchlid,count_leaves);
}
}
求樹高:
int BiTreeDepth(BiTree BT){//求樹高
int lchiledep,rchilddep;
if(!BT) return 0;
else{
lchiledep=BiTreeDepth(BT->lchild);
rchilddep=BiTreeDepth(BT->rchlid);
return (lchiledep>rchilddep)?(lchiledep+1):(rchilddep+1);
}
}
求根節點到指定節點之間的路徑
void Path(BiTree BT){//求根節點到指定節點之間的路徑
BiTree stack[MAX_TREE_SIZE],s;
int tag[MAX_TREE_SIZE];
char p;
int top=0,i;
cout<<"請輸入要查找的節點值:";
cin>>p;
s=BT;
do{
while (s!=NULL){
top++;
stack[top]=s;
tag[top]=0;
s=s->lchild;
}
if(top>0){
if (tag[top]==1){
if (stack[top]->data==p){
cout<<"路徑 :";
for (i = 1; i <= top; ++i) cout<<stack[i]->data<<' ';
cout<<endl;
break;
}
top--;
}
else{
s=stack[top];
if (top>0){
s=s->rchlid;
tag[top]=1;
}
}
}
} while (s!=NULL||top>0);
}
先序遍歷二叉樹的遞歸操作:
void PreOrder (BiTree BT) {//遞歸先序遍歷二叉樹
if (BT != NULL){
cout<<BT->data<<' '<<endl;
PreOrder(BT->lchild);
PreOrder(BT->rchlid);
}
}
中序遍歷二叉樹的遞歸操作
void InOrder (BiTree BT) {//遞歸中序遍歷二叉樹
if (BT != NULL){
InOrder(BT->lchild);
cout<<BT->data<<' '<<endl;
InOrder(BT->rchlid);
}
}
後序遍歷二叉樹的遞歸操作:
void PostOrder (BiTree BT) {//遞歸後序遍歷二叉樹
if (BT != NULL){
PostOrder(BT->lchild);
PostOrder(BT->rchlid);
cout<<BT->data<<' '<<endl;
}
}
先序遍歷二叉樹的非遞歸操作:
void NRPerOrder(BiTree BT){//非遞歸先序遍歷二叉樹
BiTree stack[MAX_TREE_SIZE],p;
int top;
if (BT!=NULL){
top=1;
stack[top]=BT;
while(top>0){
p=stack[top];
top--;
cout<<p->data<<' '<<endl;
if (p->rchlid!=NULL){
top++;
stack[top]=p->rchlid;
}
if (p->lchild!=NULL){
top++;
stack[top]=p->lchild;
}
}
}
}
中序遍歷二叉樹的非遞歸操作:
void NRInOrder(BiTree BT){//非遞歸中序遍歷二叉樹
BiTree stack[MAX_TREE_SIZE],p;
int top=0;
p=BT;
do{
while(p!=NULL){
top++;
stack[top]=p;
p=p->lchild;
}
if (top>0){
p=stack[top];
top--;
cout<<p->data<<' '<<endl;
p=p->rchlid;
}
}while (p!=NULL||top>0);
}
後序遍歷二叉樹的非遞歸操作:
void NRPostOrder(BiTree BT){//後序遍歷二叉樹
BiTree stack[MAX_TREE_SIZE],p;
int tag[MAX_TREE_SIZE];
int top=0;
p=BT;
do{
while (p!=NULL){
top++;
stack[top]=p;
tag[top]=0;
p=p->lchild;
}
if(top>0){
if (tag[top]==1){
cout<<stack[top]->data<<' '<<endl;
top--;
}
else{
p=stack[top];
if (top>0){
p=p->rchlid;
tag[top]=1;
}
}
}
} while (p!=NULL||top>0);
}
菜單頁面:
void Menu(){
printf(" 二叉樹\n");
printf("----------------------------------------------\n");
printf(" 菜單選項:\n");
cout<<"1 ------------------------ 創建二叉樹"<<endl;
cout<<"2 ------------------------ 先序遍歷二叉樹遞歸操作"<<endl;
cout<<"3 ------------------------ 中序遍歷二叉樹遞歸操作"<<endl;
cout<<"4 ------------------------ 後序遍歷二叉樹遞歸操作"<<endl;
cout<<"5 ------------------------ 先序遍歷二叉樹非遞歸操作"<<endl;
cout<<"6 ------------------------ 中序遍歷二叉樹非遞歸操作"<<endl;
cout<<"7 ------------------------ 後序遍歷二叉樹非遞歸操作"<<endl;
cout<<"8 ------------------------ 求葉子節點個數"<<endl;
cout<<"9 ------------------------ 求樹高"<<endl;
cout<<"10 ----------------------- 求根節點到指定節點之間的路徑"<<endl;
cout<<"11 ------------------------ 求總結點數"<<endl;
cout<<"12 ----------------------- 退出"<<endl;
cout<<"請選擇您需要的操作:";
}
主函數:
int main() {
BiTree BT,p;
int n,count_leaves=0,deep_tree,count_Node=0;
int flag=0;
Menu();
cin>>n;
while(n!=12){
switch(n){
case 1:
flag=CreateBiTree(BT,count_Node);
if(flag==1) cout<<"創建成功!"<<endl;
break;
case 2:
PreOrder (BT);
break;
case 3:
InOrder (BT);
break;
case 4:
PostOrder (BT);
break;
case 5:
NRPerOrder(BT);
break;
case 6:
NRInOrder(BT);
break;
case 7:
NRPostOrder(BT);
break;
case 8:
CountLeaves(BT,count_leaves);
cout<<count_leaves<<endl;
break;
case 9:
deep_tree=BiTreeDepth(BT);
cout<<deep_tree<<endl;
break;
case 10:
Path(BT);
break;
case 11:
cout<<count_Node<<endl;
break;
default:
cout<<"輸入有誤,請重新輸入:";
break;
}
cout<<"請繼續選擇您的操作:";
cin>>n;
}
free (BT);
return 0;
}