可能编译时会有些语法小错误(比如分号,->,等),很容易就自己纠正了哦,思路绝对是完全正确的,所以用的话就自己试着改改吧,直接复制粘贴,就正确,岂不是太没写代码体验了,自己改改才印象更加深刻的呢(▽)~~~~;
递归遍历树
//遍历算法
#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;
}