#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; struct Node { Node(double d, Node* l = NULL, Node* r = NULL, Node* f = NULL) :data(d), left(l), right(r), father(f) {} double data; Node* father, * left, * right; //父,左右孩子 string code; //存储编码 }; typedef Node* Tree; //通过中序,构建编码 void creatCode(Node* node, string s) { if (node != NULL) { creatCode(node->left, s + '0'); if (node->left == NULL && node->right == NULL) //是叶子节点就更新编码 node->code = s; creatCode(node->right, s + '1'); } } int main() { vector<double> w; vector<Node*> node; double tmp; Tree tree; cout << "输入权值,数字后紧跟回车结束:"; do { cin >> tmp; w.push_back(tmp); } while (getchar() != '\n'); sort(w.begin(), w.end(), greater<double>()); //降序排序 for (int i = 0; i < w.size(); i++) node.push_back(new Node(w[i])); vector<Node*> out = node; Node* left, * right; do { right = node.back(); node.pop_back(); //取出最小的两个 left = node.back(); node.pop_back(); node.push_back(new Node(left->data + right->data, left, right)); //将新结点(求和)推进数组中 left->father = node.back(); //更新父结点 right->father = node.back(); out.push_back(node.back()); //存储此结点 for (int i = node.size() - 1; i > 0 && node[i]->data > node[i - 1]->data; i--) //从末尾冒泡,排序 swap(node[i], node[i - 1]); } while (node.size() != 1); //构建树结构 tree = node.front(); //剩余的一个结点即根结点 creatCode(tree, ""); printf("结点\t父结点\t左孩子\t右孩子\t编码\n"); for (int i = 0; i < out.size(); i++) printf("%.2lf\t%.2lf\t%.2lf\t%.2lf\t%s\n", out[i]->data, out[i]->father == NULL ? 0 : out[i]->father->data, out[i]->left == NULL ? 0 : out[i]->left->data, out[i]->right == NULL ? 0 : out[i]->right->data, out[i]->code.c_str()); return 0; }根据代码写流程图
时间: 2024-03-24 22:40:21 浏览: 98
使用include <iostream>时一定要加using namespace std
```mermaid
graph TD;
A(开始) --> B(定义结构体Node和树Tree);
B --> C(定义函数creatCode);
B --> D(定义主函数main);
D --> E(定义变量w,node,tmp,tree);
E --> F(输入权值,数字后紧跟回车结束);
F --> G(获取输入的权值并降序排序);
G --> H(依次创建Node结构体并存储到node数组中);
H --> I(将node数组中最小的两个结点取出,求和后推进数组中);
I --> J(更新父结点和左右孩子);
J --> K(将新结点存储到out数组中);
K --> L(对node数组进行冒泡排序);
L --> M(重复执行I到L的步骤直到node数组中只剩一个结点);
M --> N(将最后剩余的结点作为根结点);
N --> O(构建树结构);
O --> P(调用creatCode函数,通过中序遍历构建编码);
P --> Q(输出各结点的信息);
Q --> R(结束);
```
阅读全文