# 2.PTA实验作业

## 2.1.2 设计思路

//建树函数InitExpTree

当str[i]是操作数
建值为str[i]树结点，入树结点栈stacktree
当str[i]为运算符
while(op栈顶优先级比str[i]高)
op栈顶出栈，stacktree出栈两个元素
这两个元素分别为op栈顶元素的左右孩子结点
树结点入栈
当op栈顶优先级比str[i]低
str[i]入栈
当op栈顶优先级与str[i]相等
op栈顶出栈
将op栈中还有的元素继续和树结点栈stacktree中的元素建立树
//计算表达式函数EvaluateExTree



## 2.2.2 设计思路

//建树函数CreatBinTree(int *last,int *in,int n)

n为0时为空栈，返回NULL

s->Left=CreatBinTree(last,in,k);
s->Right=CreatBinTree(last+k,p+1,n-k-1);



## 2.3.2 设计思路

//计算WPL函数wpl(BTree BT)

lastNode初始化为根结点，newlastNode为空

出栈一个元素t
当t为叶结点时
累加权值wpl
当t左孩子不为空
左孩子入队列，置newlastNode为t->lchild
当t右孩子不为空
右孩子入队列，置newlastNode为t->rchild
当t与lastNode相等时
更新lastNode为newlastNode，deep+1



230

# 4. 阅读代码

## 题目：哈夫曼编码解码

#include<iostream>
#include<string>
using namespace std;

struct Node
{
double weight;
string ch;
string code;
int lchild, rchild, parent;
};

void Select(Node huffTree[], int *a, int *b, int n)//找权值最小的两个a和b
{
int i;
double weight = 0; //找最小的数
for (i = 0; i <n; i++)
{
if (huffTree[i].parent != -1)     //判断节点是否已经选过
continue;
else
{
if (weight == 0)
{
weight = huffTree[i].weight;
*a = i;
}
else
{
if (huffTree[i].weight < weight)
{
weight = huffTree[i].weight;
*a = i;
}
}
}
}
weight = 0; //找第二小的数
for (i = 0; i < n; i++)
{
if (huffTree[i].parent != -1 || (i == *a))//排除已选过的数
continue;
else
{
if (weight == 0)
{
weight = huffTree[i].weight;
*b = i;
}
else
{
if (huffTree[i].weight  < weight)
{
weight = huffTree[i].weight;
*b = i;
}
}
}
}
int temp;
if (huffTree[*a].lchild < huffTree[*b].lchild)  //小的数放左边
{
temp = *a;
*a = *b;
*b = temp;
}
}

void Huff_Tree(Node huffTree[], int w[], string ch[], int n)
{
for (int i = 0; i < 2 * n - 1; i++) //初始过程
{
huffTree[i].parent = -1;
huffTree[i].lchild = -1;
huffTree[i].rchild = -1;
huffTree[i].code = "";
}
for (int i = 0; i < n; i++)
{
huffTree[i].weight = w[i];
huffTree[i].ch = ch[i];
}
for (int k = n; k < 2 * n - 1; k++)
{
int i1 = 0;
int i2 = 0;
Select(huffTree, &i1, &i2, k); //将i1，i2节点合成节点k
huffTree[i1].parent = k;
huffTree[i2].parent = k;
huffTree[k].weight = huffTree[i1].weight + huffTree[i2].weight;
huffTree[k].lchild = i1;
huffTree[k].rchild = i2;
}
}

void Huff_Code(Node huffTree[], int n)
{
int i, j, k;
string s = "";
for (i = 0; i < n; i++)
{
s = "";
j = i;
while (huffTree[j].parent != -1) //从叶子往上找到根节点
{
k = huffTree[j].parent;
if (j == huffTree[k].lchild) //如果是根的左孩子，则记为0
{
s = s + "0";
}
else
{
s = s + "1";
}
j = huffTree[j].parent;
}
cout << "字符 " << huffTree[i].ch << " 的编码：";
for (int l = s.size() - 1; l >= 0; l--)
{
cout << s[l];
huffTree[i].code += s[l]; //保存编码
}
cout << endl;
}
}

string Huff_Decode(Node huffTree[], int n,string s)
{
cout << "解码后为：";
string temp = "",str="";//保存解码后的字符串
for (int i = 0; i < s.size(); i++)
{
temp = temp + s[i];
for (int j = 0; j < n; j++)
{
if (temp == huffTree[j].code)
{
str=str+ huffTree[j].ch;
temp = "";
break;
}
else if (i == s.size()-1&&j==n-1&&temp!="")//全部遍历后没有
{
str= "解码错误！";
}
}
}
return str;
}

int main()
{
//编码过程
const int n=5;
Node huffTree[2 * n];
string str[] = { "A", "B", "C", "D", "E"};
int w[] = { 30, 30, 5, 20, 15 };
Huff_Tree(huffTree, w, str, n);
Huff_Code(huffTree, n);
//解码过程
string s;
cout << "输入编码：";
cin >> s;
cout << Huff_Decode(huffTree, n, s)<< endl;;
system("pause");
return 0;
}


# 5. 代码Git提交记录截图

posted @ 2018-05-05 19:57  久念  阅读(243)  评论(0编辑  收藏  举报