/*树*/
/*注:*s与s[]的区别
*s为指针,可以随意改变大小 但是内容不得改变
s[]为数组,且不能改变大小 内容可以随意改变*/
/*(1)遍历*/
/*
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int Max_Size=100;
struct TreeNode
{
char data;
struct TreeNode *left,*right;
};
class Tree
{
private:
int index;
public:
Tree()
{
index=-1;
}
~Tree(){}
TreeNode *creat(char *s)//构造树
{
TreeNode *node;
node=NULL;
if(++index<strlen(s))
{
char a=s[index];
if(a=='#')
{
return NULL;
}
node=(TreeNode *)malloc(sizeof(TreeNode));
node->data=a;
node->left=creat(s);
node->right=creat(s);
}
return node;
}
void prePop(TreeNode *root)//前序输出
{
if(root==NULL)
{
return;
}
cout<<root->data<<" ";
prePop(root->left);
prePop(root->right);
}
void inPop(TreeNode *root)//中序输出
{
if(root==NULL)
{
return;
}
inPop(root->left);
cout<<root->data<<" ";
inPop(root->right);
}
void postPop(TreeNode *root)
{
if(root==NULL)
{
return;
}
postPop(root->left);
postPop(root->right);
cout<<root->data<<" ";
}
void leverPop(TreeNode *root)//层序 利用入栈,出栈,这时候不是字符串,而是,结构体
{
int front=-1;int area=-1;
TreeNode *tree,*Q[Max_Size];
tree=(TreeNode *)malloc(sizeof(TreeNode));
Q[++front]=root;
while(front!=area)
{
tree=Q[++area];
cout<<tree->data<<" ";
if(tree->left!=NULL)
{
Q[++front]=tree->left;
}
if(tree->right!=NULL)
{
Q[++front]=tree->right;
}
}
}
};
int main()
{
char *s="ABD#G###CE##F##";
Tree p;
TreeNode *root;
root=p.creat(s);
cout<<"前序输出"<<endl;
p.prePop(root);
cout<<endl<<"中序输出"<<endl;
p.inPop(root);
cout<<endl<<"后序输出"<<endl;
p.postPop(root);
cout<<endl<<"层序输出"<<endl;
p.leverPop(root);
return 0;
}*/
/*2.非递归算法*/
/*
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int Max_Size=100;
struct TreeNode
{
char data;
TreeNode *left,*right;
};
class Tree
{
private:
int index;
public:
Tree()
{
index=-1;
}
~Tree(){}
TreeNode *creat(char *s)
{
TreeNode *root;
while(++index<strlen(s))
{
char a=s[index];
if(a=='#')
{
return NULL;
}
root=(TreeNode *)malloc(sizeof(TreeNode));
root->data=a;
root->left=creat(s);
root->right=creat(s);
}
return root;
}
void prePop(TreeNode *root)//前序 利用入栈 出栈
{
int front=-1;
int area=-1;
}
};
int main()
{
TreeNode *root;
Tree p;
char *s="ABD#G###CE##F##";
root=p.creat(s);
return 0;
}*/
/*3.哈夫曼树及哈弗曼编码*/
/*定义:
1.哈夫曼树:带权路径长度最小的二叉树
2.做法:
首先,先定义一个结构体(包含它的父,左,右)
其次,在定义一个结构体(包含它的权值和哈夫曼编码)
然后,找到最小的两个权值,改变他们的父,以及增加一个element(其权值为两个最小权值的和,左(为小)右(为大)
最后,输出*/
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<iomanip>//这个头文件是声明一些 “流操作符”的
//比较常用的有:setw(int);//设置显示宽度,left//right//设置左右对齐。 setprecision(int);//设置浮点数的精确度。
/*或者可以用#include<bits/stdc++.h>包含所有c++头文件*/
using namespace std;
typedef struct TreeCode
{
int start;
int code[2014];
};
typedef struct TreeNode
{
int weight;
int parent,rchild,lchild;
};
void Select(TreeNode a[],int &s1,int &s2,int n)
{
for (int i = 0; i < n; i++)
{
if (a[i].parent == -1)// 初始化s1,s1的双亲为-1
{
s1 = i;
break;
}
}
for (int i = 0; i < n; i++)// s1为权值最小的下标
{
if (a[i].parent == -1 && a[s1].weight > a[i].weight)
s1 = i;
}
for (int j = 0; j < n; j++)
{
if (a[j].parent == -1&&j!=s1)// 初始化s2,s2的双亲为-1
{
s2 = j;
break;
}
}
for (int j = 0; j < n; j++)// s2为另一个权值最小的结点
{
if (a[j].parent == -1 && a[s2].weight > a[j].weight&&j != s1)
s2 = j;
}
}
void HuffmanTree(TreeNode tree[],int l,int str[])
{
for(int i=0;i<2*l-1;i++)
{
tree[i].weight=tree[i].rchild=tree[i].lchild=tree[i].parent=-1;
}
for(int i=0;i<l;i++)
{
tree[i].weight=str[i];
}
for(int i=l;i<2*l-1;i++)
{
int s1,s2;
Select(tree,s1,s2,i);
tree[i].weight=tree[s1].weight+tree[s2].weight;
tree[i].rchild=s2;
tree[i].lchild=s1;
tree[s1].parent=tree[s2].parent=i;
}
}
void BulidCode(TreeCode treecode[],TreeNode tree[],int l)
{
int c,s;
TreeCode p;
for(int i=0;i<l;i++)
{
p.start=l-1;
c=i;
s=tree[c].parent;
while(s!=-1)
{
if(tree[s].lchild==c){
p.code[p.start]=0;
}else{
p.code[p.start]=1;
}
p.start--;
c=s;
s=tree[c].parent;
}
for(int j=p.start+1;j<l;j++)
{
treecode[i].code[j]=p.code[j];
}
treecode[i].start=p.start;
}
}
void Print(TreeNode root[],TreeCode treecode[],int l)
{
cout << "index weight parent lChild rChild code" << endl;
cout << left; // 左对齐输出
for (int i = 0; i < l; i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << " ";
for(int j=treecode[i].start+1;j<l;j++)
{
cout<<treecode[i].code[j];
}
cout<<endl;
}
for(int i=l;i<l*2-1;i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << endl;
}
}
int main()
{
int str[]={2,5,3,7,8,9,11};
int l=sizeof(str)/sizeof(str[0]);
TreeNode *tree=new TreeNode [2*l-1];
HuffmanTree(tree,l,str);
TreeCode *treecode=new TreeCode [2*l-1];
BulidCode(treecode,tree,l);
Print(tree,treecode,l);
}
/*注:*s与s[]的区别
*s为指针,可以随意改变大小 但是内容不得改变
s[]为数组,且不能改变大小 内容可以随意改变*/
/*(1)遍历*/
/*
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int Max_Size=100;
struct TreeNode
{
char data;
struct TreeNode *left,*right;
};
class Tree
{
private:
int index;
public:
Tree()
{
index=-1;
}
~Tree(){}
TreeNode *creat(char *s)//构造树
{
TreeNode *node;
node=NULL;
if(++index<strlen(s))
{
char a=s[index];
if(a=='#')
{
return NULL;
}
node=(TreeNode *)malloc(sizeof(TreeNode));
node->data=a;
node->left=creat(s);
node->right=creat(s);
}
return node;
}
void prePop(TreeNode *root)//前序输出
{
if(root==NULL)
{
return;
}
cout<<root->data<<" ";
prePop(root->left);
prePop(root->right);
}
void inPop(TreeNode *root)//中序输出
{
if(root==NULL)
{
return;
}
inPop(root->left);
cout<<root->data<<" ";
inPop(root->right);
}
void postPop(TreeNode *root)
{
if(root==NULL)
{
return;
}
postPop(root->left);
postPop(root->right);
cout<<root->data<<" ";
}
void leverPop(TreeNode *root)//层序 利用入栈,出栈,这时候不是字符串,而是,结构体
{
int front=-1;int area=-1;
TreeNode *tree,*Q[Max_Size];
tree=(TreeNode *)malloc(sizeof(TreeNode));
Q[++front]=root;
while(front!=area)
{
tree=Q[++area];
cout<<tree->data<<" ";
if(tree->left!=NULL)
{
Q[++front]=tree->left;
}
if(tree->right!=NULL)
{
Q[++front]=tree->right;
}
}
}
};
int main()
{
char *s="ABD#G###CE##F##";
Tree p;
TreeNode *root;
root=p.creat(s);
cout<<"前序输出"<<endl;
p.prePop(root);
cout<<endl<<"中序输出"<<endl;
p.inPop(root);
cout<<endl<<"后序输出"<<endl;
p.postPop(root);
cout<<endl<<"层序输出"<<endl;
p.leverPop(root);
return 0;
}*/
/*2.非递归算法*/
/*
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int Max_Size=100;
struct TreeNode
{
char data;
TreeNode *left,*right;
};
class Tree
{
private:
int index;
public:
Tree()
{
index=-1;
}
~Tree(){}
TreeNode *creat(char *s)
{
TreeNode *root;
while(++index<strlen(s))
{
char a=s[index];
if(a=='#')
{
return NULL;
}
root=(TreeNode *)malloc(sizeof(TreeNode));
root->data=a;
root->left=creat(s);
root->right=creat(s);
}
return root;
}
void prePop(TreeNode *root)//前序 利用入栈 出栈
{
int front=-1;
int area=-1;
}
};
int main()
{
TreeNode *root;
Tree p;
char *s="ABD#G###CE##F##";
root=p.creat(s);
return 0;
}*/
/*3.哈夫曼树及哈弗曼编码*/
/*定义:
1.哈夫曼树:带权路径长度最小的二叉树
2.做法:
首先,先定义一个结构体(包含它的父,左,右)
其次,在定义一个结构体(包含它的权值和哈夫曼编码)
然后,找到最小的两个权值,改变他们的父,以及增加一个element(其权值为两个最小权值的和,左(为小)右(为大)
最后,输出*/
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<iomanip>//这个头文件是声明一些 “流操作符”的
//比较常用的有:setw(int);//设置显示宽度,left//right//设置左右对齐。 setprecision(int);//设置浮点数的精确度。
/*或者可以用#include<bits/stdc++.h>包含所有c++头文件*/
using namespace std;
typedef struct TreeCode
{
int start;
int code[2014];
};
typedef struct TreeNode
{
int weight;
int parent,rchild,lchild;
};
void Select(TreeNode a[],int &s1,int &s2,int n)
{
for (int i = 0; i < n; i++)
{
if (a[i].parent == -1)// 初始化s1,s1的双亲为-1
{
s1 = i;
break;
}
}
for (int i = 0; i < n; i++)// s1为权值最小的下标
{
if (a[i].parent == -1 && a[s1].weight > a[i].weight)
s1 = i;
}
for (int j = 0; j < n; j++)
{
if (a[j].parent == -1&&j!=s1)// 初始化s2,s2的双亲为-1
{
s2 = j;
break;
}
}
for (int j = 0; j < n; j++)// s2为另一个权值最小的结点
{
if (a[j].parent == -1 && a[s2].weight > a[j].weight&&j != s1)
s2 = j;
}
}
void HuffmanTree(TreeNode tree[],int l,int str[])
{
for(int i=0;i<2*l-1;i++)
{
tree[i].weight=tree[i].rchild=tree[i].lchild=tree[i].parent=-1;
}
for(int i=0;i<l;i++)
{
tree[i].weight=str[i];
}
for(int i=l;i<2*l-1;i++)
{
int s1,s2;
Select(tree,s1,s2,i);
tree[i].weight=tree[s1].weight+tree[s2].weight;
tree[i].rchild=s2;
tree[i].lchild=s1;
tree[s1].parent=tree[s2].parent=i;
}
}
void BulidCode(TreeCode treecode[],TreeNode tree[],int l)
{
int c,s;
TreeCode p;
for(int i=0;i<l;i++)
{
p.start=l-1;
c=i;
s=tree[c].parent;
while(s!=-1)
{
if(tree[s].lchild==c){
p.code[p.start]=0;
}else{
p.code[p.start]=1;
}
p.start--;
c=s;
s=tree[c].parent;
}
for(int j=p.start+1;j<l;j++)
{
treecode[i].code[j]=p.code[j];
}
treecode[i].start=p.start;
}
}
void Print(TreeNode root[],TreeCode treecode[],int l)
{
cout << "index weight parent lChild rChild code" << endl;
cout << left; // 左对齐输出
for (int i = 0; i < l; i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << " ";
for(int j=treecode[i].start+1;j<l;j++)
{
cout<<treecode[i].code[j];
}
cout<<endl;
}
for(int i=l;i<l*2-1;i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << endl;
}
}
int main()
{
int str[]={2,5,3,7,8,9,11};
int l=sizeof(str)/sizeof(str[0]);
TreeNode *tree=new TreeNode [2*l-1];
HuffmanTree(tree,l,str);
TreeCode *treecode=new TreeCode [2*l-1];
BulidCode(treecode,tree,l);
Print(tree,treecode,l);
}