遞歸遍歷樹
//遍歷算法
#include<iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreratBiTree(BiTree &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=new BiTree;
T->data=ch;
CreratBiTree(T->lchild);
CreratBiTree(T->rchild)
}
}
void InOderTraverserve(BiTree T){
if(T){
InOderTraverserve(T->lchild);
cout<<T-data;
InOderTraverserve(T->rchild);
}
}
void main(){
BiTree tree;
cout<<"please input\n";
CreratBiTree(tree);
cout<<"middle result\n";
CreratBiTree(tree);
cout<<"front result\n";
cout<<endl;
}
非遞歸遍歷樹
//非遞歸遍歷二×樹
#include<iostream>
using namespace std;
//二叉鏈存儲
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
//鏈棧
typedef struct StackNode{
BiTNode data;
struct StackNode *next;
}StackNode,*LinkStack;
void CreatBiTree(BiTree &T){
char ch;
cin>>ch;
if(ch=='#') T==NULL;
else{
T=new BiTNode;
T->data=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
}
void InitStack(LinkStack &S)
{
//構造一個空棧S,棧頂指針置空
S=NULL;
}
void StackEmpty(LinkStack S){
if(!S){
return true;
}else{
return false;
}
}
void Push(LinkStack &S,BiTree e){
StackNode *p=new StackNode;
p->data=*e;
p->next=S;
S=p;
}
void pop(LinkStack &S BiTree e){
if(S!=NULL){
*e=S->data;
StackNode *p=S;
S=S->next;
delete p;
}
}
//中序
void InOderTraversel1(BiTree T){
LinkStack S;BiTree p;
BiTree q=new BiTNode;
InitStack(S); p=T;
while(p||!StackEmpty(S)){
if(p){
push(S,p);
p->lchild;
}else{
pop(S,p);
cout<<q->data;
p=q->rchild;
}
}
}
void main(){
BiTree tree;
cout<<"please input\n";
CreatBiTree(tree);
cout<<"result:\n";
InOderTraversel1(tree);
cout<<endl;
}
建樹
//建樹
#include<iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void main(){
BiTree tree;
cout<<"請輸入創建二叉鏈表的序列:\n";
CreateBiTree(tree);
cout<<"所創建的二叉鏈表中序序列:\n";
InOrderTraverse(tree);
cout<<endl;
}
複製樹
#include<iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct StackNode{
BiTNode data;
struct StackNode *next;
}
void CreateBiTree(BiTree &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void InitStack(LinkStack &S)
{
//構造一個空棧S,棧頂指針置空
S=NULL;
}
bool StackEmpty(LinkStack S)
{
if(!S)
return true;
return false;
}
void Push(LinkStack &S,BiTree e)
{
//在棧頂插入元素*e
StackNode *p=new StackNode;
p->data=*e;
p->next=S;
S=p;
}
void Pop(LinkStack &S,BiTree e)
{
if(S!=NULL)//原書上寫的是if(S==NULL)return ERROR;
{
*e=S->data;
StackNode *p=S;
S=S->next;
delete p;
}
}
void Copy(BiTree T,BiTree &newT){
if(T==NULL){
newT=NULL;
return;
}else{
newT=new BiTNode;
newT->data=T->data;
Copy(t->lchild,newT->lchild);
Copy(t->rchild,newT->rchild);
}
}
void InOrderTraverse(BiTree T)
{
//中序遍歷二叉樹T的遞歸算法
if(T){
InOrderTraverse(T->lchild);
cout << T->data;
InOrderTraverse(T->rchild);
}
}
void main()
{
BiTree tree,new_tree;
cout<<"請輸入創建二叉樹的序列:\n";
CreateBiTree(tree);
Copy(tree,new_tree);
cout<<"複製獲得的新樹的中序序列:\n";
InOrderTraverse(new_tree);
cout<<endl;
}
求樹的深度
#include<iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T){
char ch;
cin >> ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int Depth(BiTree T){
int m,n;
if(T==NULL) retue 0;
else{
m=Depth(T->lchild);
n=Depth(T->rchild);
if(m>n) return(m+1);
else{
return (n+1);
}
}
}
void main(){
BiTree tree;
cout<<"please input:\n";
CreateBiTree(tree);
cout<<"deepth is:"<<Depth(tree)<<endl;
}
求節點數
#include<iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int NodeCount(BiTree T){
if(T==NULL) return 0;
else return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}
void main(){
BiTree tree;
cout<<"請輸入創建二叉鏈表的序列:\n";
CreateBiTree(tree);
cout<<"結點個數爲:"<<NodeCount(tree)<<endl;
}
中序線索化
//樹的線索化中序
#include<iostream>
using namespace std;
typedef struct BiThrNode{
char data;
struct BiThrNode *lchild,*rchlild;
int LTag,RTag;
}BiThrNode,*BiThrTree;
BiThrNode *pre = new BiThrNode;
void CreateBiTree(BiThrNode &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=new BiThrNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchlild);
}
}
void InThreading(BiThrTree p){
if(p){
InThreading(p->lchild);
if(!p->lchild){
p->LTag=1;
p->lchild=pre;
}else{
p->LTag;
}
if(!pre->rchlild){
pre->RTag=1;
pre->rchlild=p;
}else{
pre->RTag=0;
}
pre=p;
InThreading(p->rchlild);
}
}
void main(){
pre->RTag=1;
pre->rchlild=NULL;
BiThrTree tree;
cout<<"please input:\n";
CreateBiTree(tree);
InThreading(tree);
cout<<"finish!\n";
}
帶頭結點的中序線索化
//帶頭結點的中序線索化
#include<iostream>
using namespace std;
typedef struct BiThrNode{
char data;
struct BiThrNode *lchild,*rchild;
int LTag,RTag;
}BiThrNode,*BiThrTree;
BiThrNode *pre=new BiThrNode;
void CreateBiTree(BiThrTree &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=newBiThrNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void InThreading(BiThrTree p){
if(p){
InThreading(p->lchild);
if(!p->lchild){
p->LTag=1;
p->lchild=pre;
}else{
p->LTag=0;
}
if(!pre->rchild){
pre->RTag=1;
pre->rchild=p;
}else{
pre->RTag=0;
}
pre=p;
InThreading(p->rchild);
}
}
void InOrderThreading(BiThrTree &Thrt,BiThrTree T){
Thrt=new BiThrNode;
Thrt->LTag=0;
Thrt->RTag=1;
Thrt->rchild=Thrt;
if(!T) Thrt->lchild=Thrt;
else{
Thrt->lchild=T;pre=Thrt;
InThreading(T);
pre->rchild=Thrt;
pre->RTag=1;
Thrt->rchild=pre;
}
}
void main(){
pre->RTag=1;
pre->rchild=NULL;
BiThrTree tree,Thrt;
cout<<"please:\n";
CreateBiTree(tree);
InThreading(Thrt,tree);
cout<<"finish:\n";
}
遍歷線索二叉樹
//遍歷線索二叉樹
#include<iostream>
using namespace std;
typedef struct BiThrNode{
char data;
struct BiThrNode *lchild,*rchild;
int LTag,RTag;
}BiThrNode,*BiThrTree;
BiThrNode *pre=new BiThrNode;
void CreateBiTree(BiThrTree &T){
char ch;
cin>>ch;
if(ch='#') T=NULL;
else{
T=new BiThrNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void InTreading(BiThrTree p){
if(p){
InTreading(p->lchild);
if(!p->lchild){
p->LTag=1;
p->lchild=pre;
}else{
p->LTag=0;
}
if(!pre->rchild){
pre->RTag=1;
pre->rchild=p;
}else{
pre->RTag=0;
}
pre=p;
InTreading(p->rchild);
}
}
void InOrderTreading(BiThrTree &Thrt,BiThrTree T){
Thrt=new BiThrNode;
Thrt->LTag=0;
Thrt->RTag=1;
Thrt->rchild=Thrt;
if(!T){
Thrt->lchild=Thrt;
}else{
Thrt->lchild=T;
pre=Thrt;
InTreading(T);
pre->rchild=Thrt;
pre->RTag=1;
Thrt->rchild=pre;
}
}
//start
void InOrderTraverse_Thr(BiThrTree T){
BiThrTree p;
p=T->lchild;
while(p!=T){
while(p->LTag==0){
p=p->lchild;
}
cout<<p->data;
while(p->RTag==1&&p->rchild!=T){
p=p->rchild;
cout<<p-<data;
}
p=p->rchild;
}
}
void main(){
pre->RTag=1;
pre->rchild=NULL;
BiThrTree tree,Thrt;
cout<,"please input:\n";
CreateBiTree(tree);
InOrderTreading(Thrt,tree);
cout<<"result is:\n";
InOrderTraverse_Thr(Thrt);
cout<<endl;
}
構造哈夫曼樹
//構造哈夫曼樹
#include<iostream>
using namespace std;
typedef struct{
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
//選出最小的兩個
void Select(HuffmanTree HT.int len,int &s1,int &s2){
int i,min1=0x3f3f3f3f,min2=0x3f3f3f3f;
for(i=1;i<=len;i++){
if(HT[i].weight<min1&&HT[i].parent==0){
min1=HT[i].weight;
s1=i;
}
}
int temp=HT[s1].weight;
HT[s1].weight=0x3f3f3f3f;
for(i=1;i<=len;i++){
if(HT[i].weight<min2&&HT[i].parent==0){
min2=HT[i].weight;
s2=i;
}
}
HT[s1].weight=temp;
}
void CreateHuffmanTree(HuffmanTree &HT,int n){
im,s1,s2,i;
if(n<=1) return;
m=2*n-1;
HT=new HTNode[m+1];
for(i=1;i<m;i++){
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
cout<<"please input the weight of leaf child:\n";
for(i=1;i<n;i++)
cin>>HT[i]weight;
for(i=n+1;i<=m;i++){
Select(HT,i-1;s1,s2);
HT[s1].parent=i;
HT[s2].rchild=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
}
void main(){
HuffmanTree HT;
int n;
cout<<"please input the num of leaf child:\n";
cin>>n;
CreateHuffmanTree(HT,n);
cout<<"finish!\n";
}
根據赫夫曼樹求赫夫曼編碼
//根據赫夫曼樹求赫夫曼編碼
#include<iostream>
using namespace std;
typedef struct{
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree HT,int len,int&s1,int &s2){
int i,min1=0x3f3f3f3f,min2=0x3f3f3f3f;
for(i=1;i<len;i++){
if(HT[i].weight<min1&&HT[i].parent==0){
min1=HT[i].weight
s1=i;
}
}
int temp=HT[s1].weight
HT[s1].weight=0x3f3f3f3f;
for(i=1;i<=len;i++){
if(HT[i].weight<min2&&HT[i]parent==0){
min2=HT[i].weight;
s2=i;
}
}
HT[s1].weight=temp;
}
void CreateHuffmanTree(HuffmanTree &HT,int n){
int m,s1,s2,i;
if(n<=1) return;
m=2*n-1;
HT=new HTNode[m+1];
for(i=1;i<m;++i){
HT[i].parent=0;HT[i].lchild=0;HT[i].rchild=0;
}
cout<<"please input the weigth of leaf child:\n";
for(i=1;i<=n;++i){
cin>>HT[i].weight;
}
for(i=m+1;i<=m;++i){
Select(HT,i-1,s1,s2);
HT[s1].parent=i;
HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2 ;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
}
void CreateHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n){
int i,start,c,f;
HC=new char*[n+1];
char *cd=new char[n];
cd[n-1]='\0';
for(i-1;i<=n;++i){
start=n-1;
c=i;
f=HT[i].parent;
while(f!=0){
--start;
if(HT[f].lchild==c)
cd[start]='0';
else
cd[start]='1';
c=f;
f=HT[f].parent;
}
HC[i]=new char[n-start];
strcpy(HC[i],&cd[start]);
}
delete cd;
}
void show(HuffmanTree,HuffmanCode HC){
for(int i=1;i<=sizeof(HC)+1;i++){
cout<<HT[i].weight<<"result is:\n"<<HC[i]<<endl;
}
}
void main(){
HuffmanTree HT;
HuffmanCode HC;
int n;
cout<<"please input the number of leaf child:\n";
CreateHuffmanTree(HT,n);
CreateHuffmanCode(HT,HC,n);
show(HT,HC);
}
交換左右子樹
//算法5.5 計算二叉樹的深度,增長左右子數交換等功能
#include<iostream>
using namespace std;
//二叉樹的二叉鏈表存儲表示
typedef struct BiNode
{
char data; //結點數據域
struct BiNode *lchild,*rchild; //左右孩子指針
}BiTNode,*BiTree;
//用算法5.3創建二叉鏈表
void CreateBiTree(BiTree &T)
{
//按先序次序輸入二叉樹中結點的值(一個字符),建立二叉鏈表表示的二叉樹T
char ch;
cin >> ch;
if(ch=='#') T=NULL; //遞歸結束,建空樹
else{
T=new BiTNode;
T->data=ch; //生成根結點
CreateBiTree(T->lchild); //遞歸建立左子樹
CreateBiTree(T->rchild); //遞歸建立右子樹
} //else
} //CreateBiTree
int Depth(BiTree T)
{
int m,n;
if(T == NULL ) return 0; //若是是空樹,深度爲0,遞歸結束
else
{
m=Depth(T->lchild); //遞歸計算左子樹的深度記爲m
n=Depth(T->rchild); //遞歸計算右子樹的深度記爲n
if(m>n) return(m+1); //二叉樹的深度爲m 與n的較大者加1
else return (n+1);
}
}
void InOrderTraverse(BiTree T){
//中序遍歷二叉樹T的遞歸算法
if(T){
InOrderTraverse(T->lchild);
cout << T->data;
InOrderTraverse(T->rchild);
}
}
void inChangeLR(BiTree &T)
{
BiTree temp;
if(T){
if(T->lchild==NULL&&T->rchild==NULL){
return;
} else{
temp=T->lchild;
T->lchild = T->rchild;
T->rchild=temp;
}
inChangeLR(T->lchild);
inChangeLR(T->rchild);
}
}
void preChangeLR(BiTree &T)
{
BiTree temp;
if(T){
inChangeLR(T->lchild);
if(T->lchild==NULL&&T->rchild==NULL){
return;
} else{
temp=T->lchild;
T->lchild = T->rchild;
T->rchild=temp;
}
inChangeLR(T->rchild);
}
}
void postChangeLR(BiTree &T)
{
BiTree temp;
if(T){
inChangeLR(T->lchild);
inChangeLR(T->rchild);
if(T->lchild==NULL&&T->rchild==NULL){
return;
} else{
temp=T->lchild;
T->lchild = T->rchild;
T->rchild=temp;
}
}
}
int main()
{
BiTree tree;
cout<<"請輸入創建二叉鏈表的序列:\n";
CreateBiTree(tree);
InOrderTraverse(tree);
cout<<"數的深度爲:"<<Depth(tree)<<endl;
cout<<"中序遍歷的結果爲:\n";
InOrderTraverse(tree);
cout<<"\n";
//如下三種執行其中一種
cout<<"交換後中序遍歷的結果爲:\n";
inChangeLR(tree);
InOrderTraverse(tree);
cout<<"\n";
cout<<"交換後前序序遍歷的結果爲:\n";
preChangeLR(tree);
InOrderTraverse(tree);
cout<<"\n";
cout<<"交換後後序遍歷的結果爲:\n";
postChangeLR(tree);
InOrderTraverse(tree);
return 0;
}
求葉子節點數目,求寬度,按層遍歷
//中序遍歷的遞歸與非遞歸算法
#include<iostream>
using namespace std;
#define MAXQSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct BiNode{ //二叉鏈表定義
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
/************************************* 隊列 ***************************************/
typedef BiTree QElemType;
typedef struct{
QElemType *base;//初始化時動態分配存儲空間
int front;//頭指針
int rear;//尾指針
int last;
}SqQueue;
//算法3.13 循環隊列的初始化
Status InitQueue(SqQueue &Q)
{ // 構造一個空隊列Q
Q.base = new QElemType[MAXQSIZE];
if(!Q.base)
{
return OVERFLOW; // 存儲分配失敗
}
Q.front = 0;
Q.rear = 0;
return OK;
}
//算法3.14 求循環隊列的長度
int QueueLength(SqQueue Q)
{// 返回Q的元素個數,即隊列的長度
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
int QueueEmpty(SqQueue &Q)
{
if (Q.front==Q.rear) return OK;
else return ERROR;
}
//算法3.15 循環隊列的入隊
Status EnQueue(SqQueue &Q,QElemType e)
{// 插入元素e爲Q的新的隊尾元素
if((Q.rear+1)%MAXQSIZE == Q.front)
{
return ERROR;//尾指針在循環意義上加1後等於頭指針,代表隊滿
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%MAXQSIZE;
return OK;
}
//算法3.16 循環隊列的出隊
Status DeQueue(SqQueue &Q,QElemType &e)
{
if(Q.rear == Q.front)
{
return ERROR;
}
e = Q.base[Q.front];
Q.front = (Q.front+1)%MAXQSIZE;
return OK;
}
BiTree GetHead(SqQueue Q)
{//返回Q的隊列元素,不修改隊頭指針
if(Q.front!=Q.rear) //隊列非空
return Q.base[Q.front]; //返回隊頭元素的值,隊頭指針不變
}
/************************************************************************************/
//用算法5.3 先序遍歷的順序創建二叉鏈表
void CreateBiTree(BiTree &T){
//按先序次序輸入二叉樹中結點的值(一個字符),建立二叉鏈表表示的二叉樹T
char ch;
cin >> ch;
if(ch=='#') T=NULL; //遞歸結束,建空樹
else{
T=new BiTNode;
T->data=ch; //生成根結點
CreateBiTree(T->lchild); //遞歸建立左子樹
CreateBiTree(T->rchild); //遞歸建立右子樹
} //else
} //CreateBiTree
void InOrderTraverse(BiTree T){
//中序遍歷二叉樹T的遞歸算法
if(T){
InOrderTraverse(T->lchild);
cout << T->data;
InOrderTraverse(T->rchild);
}
}
//實現按層遍歷二叉樹的非遞歸算法(隊列)
void HierarchyTraverse(BiTree T)
{
BiTree bt = T;
SqQueue Q;
InitQueue(Q);
if(!bt){
return;
}
EnQueue(Q,bt);
while(Q.rear!=Q.front){
DeQueue(Q,bt);
cout<<bt->data;
if(bt->lchild!=NULL){
EnQueue(Q,bt->lchild);
}
if(bt->rchild!=NULL){
EnQueue(Q,bt->rchild);
}
}
}
//統計二叉樹中的葉子結點個數
int LeafNodeCount(BiTree T){
if(T==NULL) return 0;
else if(T->lchild==NULL&&T->rchild==NULL)
return 1;
else
return LeafNodeCount(T->lchild)+LeafNodeCount(T->rchild);
}
//-------------------
int Depth(BiTree T)
{
int m,n;
if(T == NULL ) return 0; //若是是空樹,深度爲0,遞歸結束
else
{
m=Depth(T->lchild); //遞歸計算左子樹的深度記爲m
n=Depth(T->rchild); //遞歸計算右子樹的深度記爲n
if(m>n) return(m+1); //二叉樹的深度爲m 與n的較大者加1
else return (n+1);
}
}
int LevelWidth(BiTree root,int level)//find the width of a level(amounts of nodes in the level).
{
if(!root)return 0;
else
{
if(level==1)return 1;
level=LevelWidth(root->lchild,level-1)+LevelWidth(root->rchild,level-1);
}
return level;
}
int Width(BiTree root)//find the maximum width of the btree.
{
int width,i;
int w[20];
for(i=0;i<20;i++)w[i]=0;
if(!root)width=0;
else
{
for(i=0;i<=Depth(root);i++)w[i]=LevelWidth(root,i+1);
}
i=0;
while(w[i])
{
if(w[i]>width)width=w[i];
i++;
}
return width;
}
// // -------------------
nt main(){
BiTree tree;
cout<<"請輸入創建二叉鏈表的序列:\n";
CreateBiTree(tree);
cout<<"中序遍歷的結果爲:\n";
InOrderTraverse(tree);
cout<<endl;
//按層遍歷二叉樹
cout<<"按層遍歷的結果爲:\n";
HierarchyTraverse(tree);
cout<<endl;
cout<<"二叉樹的寬度爲"<<Width(tree)<<endl;
//統計葉子結點樹
cout<<"葉子結點數爲"<<LeafNodeCount(tree);
cout<<"\n";
return 0;
}