• 二叉树学习笔记


     1、链表实现二叉树

    1. // 树节点类
    2. class Bitreenode {
    3. char data;
    4. Bitreenode* ls, * rs, * fa; // ls为左儿子节点,rs为右儿子节点,fa为父节点
    5. public:
    6. Bitreenode() {
    7. data = '0';
    8. ls = rs = fa = nullptr;
    9. }
    10. void set_ls(Bitreenode *p) {
    11. ls = p;
    12. }
    13. void set_rs(Bitreenode* p) {
    14. rs = p;
    15. }
    16. void set_data(char d) {
    17. data = d;
    18. }
    19. void set_fa(Bitreenode* p) {
    20. fa = p;
    21. }
    22. char get_data() {
    23. return data;
    24. }
    25. Bitreenode* get_ls() {
    26. return ls;
    27. }
    28. Bitreenode* get_rs() {
    29. return rs;
    30. }
    31. Bitreenode* get_fa() {
    32. return fa;
    33. }
    34. };
    35. string s;
    36. int pos;
    37. char son[100];
    38. char fa[100];
    39. class Bitree {
    40. Bitreenode* root;
    41. int num;
    42. int cnt;
    43. int h;
    44. public:
    45. // 建树前初始化工作
    46. void Creat() {
    47. pos = 0;
    48. num = 0;
    49. cnt = 0;
    50. h = 0;
    51. root = creat_tree(nullptr, 0);
    52. }
    53. // 建树方法采用“先序遍历+空树用0表示”的方法
    54. Bitreenode* creat_tree(Bitreenode * fa, int height) {
    55. Bitreenode* cn;
    56. if (pos >= s.size()) {
    57. return nullptr;
    58. }
    59. char ch = s[pos];
    60. if (ch == '0') {
    61. pos++;
    62. cn = nullptr;
    63. }
    64. else {
    65. height++;
    66. h = max(h, height);
    67. cn = new Bitreenode;
    68. cn->set_data(ch);
    69. cn->set_fa(fa);
    70. num++;
    71. pos++;
    72. cn->set_ls(creat_tree(cn, height));
    73. cn->set_rs(creat_tree(cn, height));
    74. }
    75. return cn;
    76. }
    77. // 前序遍历
    78. void preOrder(Bitreenode *p) {
    79. if (p != nullptr) {
    80. cout << p->get_data();
    81. preOrder(p->get_ls());
    82. preOrder(p->get_rs());
    83. }
    84. }
    85. // 中序遍历
    86. void inOrder(Bitreenode* p) {
    87. if (p != nullptr) {
    88. inOrder(p->get_ls());
    89. cout << p->get_data();
    90. inOrder(p->get_rs());
    91. }
    92. }
    93. // 后序遍历
    94. void posOrder(Bitreenode* p) {
    95. if (p != nullptr) {
    96. posOrder(p->get_ls());
    97. posOrder(p->get_rs());
    98. cout << p->get_data();
    99. }
    100. }
    101. // 计算叶节点数
    102. int get_sonnode(Bitreenode *p) {
    103. if (p != nullptr) {
    104. if (p->get_ls() == nullptr && p->get_rs() == nullptr) {
    105. son[cnt] = p->get_data();
    106. fa[cnt] = p->get_fa()->get_data();
    107. cnt++;
    108. }
    109. else {
    110. get_sonnode(p->get_ls());
    111. get_sonnode(p->get_rs());
    112. }
    113. }
    114. return cnt;
    115. }
    116. // 层序遍历
    117. void levelOrder(Bitreenode* p) {
    118. queue q;
    119. if (p == nullptr) {
    120. return;
    121. }
    122. q.push(p);
    123. while (q.empty() != 1) {
    124. p = q.front();
    125. q.pop();
    126. cout << p->get_data();
    127. if (p->get_ls() != nullptr) {
    128. q.push(p->get_ls());
    129. }
    130. if (p->get_rs() != nullptr) {
    131. q.push(p->get_rs());
    132. }
    133. }
    134. }
    135. // 返回根节点
    136. Bitreenode* get_root() {
    137. return root;
    138. }
    139. // 返回树的深度
    140. int get_h() {
    141. return h;
    142. }
    143. };

    2、数组实现二叉树

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. struct Bitree {
    8. int data;
    9. int ls;
    10. int rs;
    11. };
    12. void preOrder(Bitree * p, int n, int pos) {
    13. if(p[pos].data != 0) {
    14. cout << p[pos].data << " ";
    15. if(p[pos].ls != -1)preOrder(p, n, p[pos].ls);
    16. if(p[pos].rs != -1)preOrder(p, n, p[pos].rs);
    17. }
    18. }
    19. int main() {
    20. int t;
    21. cin >> t;
    22. while (t--) {
    23. int n;
    24. cin >> n;
    25. Bitree* p = new Bitree[n + 1];
    26. for (int i = 1; i <= n; i++) {
    27. cin >> p[i].data;
    28. if (2 * i <= n) {
    29. p[i].ls = 2 * i;
    30. }
    31. else p[i].ls = -1;
    32. if (2 * i + 1 <= n) {
    33. p[i].rs = 2 * i + 1;
    34. }
    35. else p[i].rs = -1;
    36. }
    37. preOrder(p, n, 1);
    38. cout << endl;
    39. }
    40. return 0;
    41. }

    3、赫夫曼编码

    1. #include
    2. #include
    3. using namespace std;
    4. // 节点
    5. struct huffmannode {
    6. int weight;
    7. int fa, ls, rs;
    8. string code;
    9. char mean;
    10. };
    11. class huffmantree {
    12. huffmannode** tree;
    13. int n;
    14. public:
    15. huffmantree() {}
    16. // 建树
    17. void build_tree() {
    18. cin >> n;
    19. tree = new huffmannode*[2*n];
    20. for (int i = 1; i <= n; i++) {
    21. tree[i] = new huffmannode;
    22. cin >> tree[i]->weight;
    23. tree[i]->fa = tree[i]->ls = tree[i]->rs = 0;
    24. tree[i]->code = "";
    25. }
    26. for (int i = 1; i <= n; i++) {
    27. cin >> tree[i]->mean;
    28. }
    29. for (int i = n + 1; i <= 2 * n - 1; i++) {
    30. tree[i] = new huffmannode;
    31. tree[i]->weight = 0;
    32. tree[i]->fa = tree[i]->ls = tree[i]->rs = 0;
    33. tree[i]->code = "";
    34. tree[i]->mean = '%';
    35. }
    36. for (int i = n + 1; i <= 2 * n - 1; i++) {
    37. int s1, s2;
    38. search(i, s1, s2);
    39. tree[i]->ls = s1, tree[i]->rs = s2;
    40. tree[i]->weight = tree[s1]->weight + tree[s2]->weight;
    41. }
    42. }
    43. // 寻找weight最小的两个节点
    44. void search(int idx, int& s1, int& s2) {
    45. int minn = 99999999;
    46. for (int i = 1; i < idx; i++) {
    47. if (minn > tree[i]->weight && tree[i]->fa == 0) {
    48. minn = tree[i]->weight;
    49. s1 = i;
    50. }
    51. }
    52. tree[s1]->fa = idx;
    53. minn = 99999999;
    54. for (int i = 1; i < idx; i++) {
    55. if (minn > tree[i]->weight && tree[i]->fa == 0) {
    56. minn = tree[i]->weight;
    57. s2 = i;
    58. }
    59. }
    60. tree[s2]->fa = idx;
    61. }
    62. // 编码
    63. void build_code() {
    64. for (int i = 1; i <= n; i++) {
    65. int fa = tree[i]->fa;
    66. int son = i;
    67. while (fa != 0) {
    68. if (tree[fa]->ls == son) {
    69. tree[i]->code = "0" + tree[i]->code;
    70. }
    71. else if (tree[fa]->rs == son) {
    72. tree[i]->code = "1" + tree[i]->code;
    73. }
    74. son = fa;
    75. fa = tree[fa]->fa;
    76. }
    77. }
    78. }
    79. // 展示编码
    80. void show_code() {
    81. for (int i = 1; i <= n; i++) {
    82. cout << tree[i]->weight << "-" << tree[i]->code << endl;
    83. }
    84. }
    85. // 解码
    86. int Decode(const string codestr, string &txtstr) {
    87. int p = 2 * n - 1;
    88. for (int i = 0; i < codestr.size(); i++) {
    89. char ch = codestr[i];
    90. if (ch == '0') p = tree[p]->ls;
    91. else if (ch == '1') p = tree[p]->rs;
    92. else return -1;
    93. if (tree[p]->ls == 0) {
    94. txtstr += tree[p]->mean;
    95. p = 2 * n - 1;
    96. }
    97. else if(tree[p]->ls != 0 && i == codestr.size() - 1) {
    98. return -1;
    99. }
    100. }
    101. return 1;
    102. }
    103. };
    104. int main() {
    105. huffmantree tree;
    106. tree.build_tree();
    107. int t;
    108. cin >> t;
    109. while (t--) {
    110. string codestr = "", txtstr = "";
    111. cin >> codestr;
    112. if (tree.Decode(codestr, txtstr) == 1) {
    113. cout << txtstr << endl;
    114. }
    115. else {
    116. cout << "error" << endl;
    117. }
    118. }
    119. return 0;
    120. }

  • 相关阅读:
    走到上市前夕,叮当健康如何勾画“医药检险”蓝图?
    C++学习笔记01-入门基础
    使用Fiddler如何抓取手机上的包
    C语言判断一个数转二进制的某位是1或者0
    开源知识库软件xwiki在Windows下的安装
    SQLSERVER 临时表和表变量到底有什么区别?
    Linux入门常用命令使用
    SparkSQL的Join的实现方式
    如何完全卸载Visual Studio 2013
    【Python零基础入门篇 · 38】:正则的高级用法
  • 原文地址:https://blog.csdn.net/m0_74099951/article/details/133816899