• 使用c++ 生成一颗简单的二叉树


    二叉树的生成方式有千千万万种,下面介绍使用c++来实现简单的二叉树的方法,通过链表的结构来生成。

    二叉树是由若干个节点组成,所以我们先要声明一个节点的类;头文件和函数实现方法分开;

     1 //
     2 //  node.h
     3 //  data_structer
     4 //
     5 //  Created by 张钊 on 2020/3/2.
     6 //  Copyright © 2020 张钊. All rights reserved.
     7 //
     8 
     9 #ifndef node_h
    10 #define node_h
    11 #include<iostream>
    12 #include<cmath>
    13 using namespace std;
    14 
    15 class Node;
    16 typedef void (*Visit)(const Node *node);
    17 class Node
    18 {
    19 public:
    20     int nodeValue;
    21     Node *l_child;
    22     Node *r_child;
    23     
    24 public:
    25     Node(int value = 0);
    26     ~Node();
    27     
    28 public:
    29     int degree() const;
    30     int depth() const;
    31     int count() const;
    32     
    33 public:
    34     void preOrder(Visit visit) const;
    35     void midOrder(Visit visit) const;
    36     void postOrder(Visit visit) const;
    37 };
    38 
    39 #endif /* node_h */
      1 //  node.cpp
      2 //  data_structer
      3 //
      4 //  Created by 张钊 on 2020/3/8.
      5 //  Copyright © 2020 张钊. All rights reserved.
      6 //
      7 
      8 #include "node.h"
      9 
     10 Node::Node(int value):nodeValue(value)
     11 {
     12     l_child = NULL;
     13     r_child = NULL;
     14 }
     15 
     16 Node::~Node()
     17 {
     18     if(l_child != NULL)
     19     {
     20         delete l_child;
     21     }
     22     if(r_child != NULL)
     23     {
     24         delete r_child;
     25     }
     26 }
     27 
     28 int Node::degree() const
     29 {
     30     if(l_child == NULL && r_child == NULL)
     31     {
     32         return 0;
     33     }
     34     if(l_child != NULL && r_child != NULL)
     35     {
     36         return 2;
     37     }
     38     return 1;
     39 }
     40 
     41 int Node::depth() const
     42 {
     43     int l_depth = 0;
     44     int r_depth = 0;
     45     if(l_child != NULL)
     46     {
     47         l_depth += l_child->depth();
     48     }
     49     if(r_child != NULL)
     50     {
     51         r_depth += r_child->depth();
     52     }
     53     return max(l_depth, r_depth) + 1;
     54 }
     55 
     56 int Node::count() const
     57 {
     58     int l_count = 0;
     59     int r_count = 0;
     60     if(l_child != NULL)
     61     {
     62         l_count += l_child->count();
     63     }
     64     if(r_child != NULL)
     65     {
     66         r_count += r_child->count();
     67     }
     68     return l_count + r_count + 1;
     69 }
     70 
     71 void Node::preOrder(Visit visit) const
     72 {
     73     (*visit)(this);
     74     if(this->l_child != NULL)
     75     {
     76         this->l_child->preOrder(visit);
     77     }
     78     if(this->r_child != NULL)
     79     {
     80         this->r_child->preOrder(visit);
     81     }
     82 }
     83 
     84 void Node::midOrder(Visit visit) const
     85 {
     86     if(this->l_child != NULL)
     87     {
     88         this->l_child->preOrder(visit);
     89     }
     90     (*visit)(this);
     91     if(this->r_child != NULL)
     92     {
     93         this->r_child->preOrder(visit);
     94     }
     95 }
     96 
     97 void Node::postOrder(Visit visit) const
     98 {
     99     if(this->l_child != NULL)
    100     {
    101         this->l_child->preOrder(visit);
    102     }
    103     if(this->r_child != NULL)
    104     {
    105         this->r_child->preOrder(visit);
    106     }
    107     (*visit)(this);
    108 }

    这样我们就实现了基本的节点的功能,下面开始进行二叉树的构建,这里创建二叉树利用map里面的key-》value,而map也是利用红黑树实现的。。

     1 //  tree.h
     2 //  data_structer
     3 //
     4 //  Created by 张钊 on 2020/3/2.
     5 //  Copyright © 2020 张钊. All rights reserved.
     6 //
     7 
     8 #ifndef tree_h
     9 #define tree_h
    10 #include "node.h"
    11 #include <map>
    13 using namespace std;
    14 
    15 class BinaryTree
    16 {
    17 private:
    18     Node *root;
    19     
    20 private:
    21     Node *create(map<int, int> &tree, int index);
    22     
    23 public:
    24     BinaryTree();
    25     BinaryTree(map<int, int> &tree);
    26     ~BinaryTree();
    27     
    28 public:
    29     int depth() const;
    30     int count() const;
    31     
    32 public:
    33     void preOrder(Visit visit) const;
    34     void midOrder(Visit visit) const;
    35     void postOrder(Visit visit) const;
    36 };
    37 
    38 #endif
     1 //  tree.cpp
     2 //  data_structer
     3 //
     4 //  Created by 张钊 on 2020/3/8.
     5 //  Copyright © 2020 张钊. All rights reserved.
     6 //
     7 
     8 #include "tree.h"
     9 
    10 BinaryTree::BinaryTree()
    11 {
    12     root = NULL;
    13 }
    14 
    15 Node* BinaryTree::create(map<int, int> &tree, int index)
    16 {
    17     if(tree.find(index) == tree.end())
    18     {
    19         return NULL;
    20     }
    21     Node *node = new Node();
    22     node->nodeValue = tree[index];
    23     node->l_child = create(tree, index * 2);
    24     node->r_child = create(tree, index * 2 + 1);
    25     return node;
    26 }
    27 
    28 BinaryTree::BinaryTree(map<int, int> &tree)
    29 {
    30     root = create(tree, 1);
    31 }
    32 
    33 BinaryTree::~BinaryTree()
    34 {
    35     if(root != NULL)
    36     {
    37         delete root;
    38     }
    39 }
    40 
    41 int BinaryTree::depth() const
    42 {
    43     return root->depth();
    44 }
    45 
    46 int BinaryTree::count() const
    47 {
    48     return root->count();
    49 }
    50 
    51 void BinaryTree::preOrder(Visit visit) const
    52 {
    53     root->postOrder(visit);
    54     cout << endl;
    55 }
    56 
    57 void BinaryTree::midOrder(Visit visit) const
    58 {
    59     root->midOrder(visit);
    60     cout << endl;
    61 }
    62 
    63 void BinaryTree::postOrder(Visit visit) const
    64 {
    65     root->postOrder(visit);
    66     cout << endl;
    67 }

    最后进行简单的调试

     1 //  main.cpp
     2 //  data_structer
     3 //
     4 //  Created by 张钊 on 2020/3/2.
     5 //  Copyright © 2020 张钊. All rights reserved.
     6 //
     7 #include "node.h"
     8 #include "tree.h"
     9 using namespace std;
    10 
    11 void visit(const Node *node)
    12 {
    13     cout << node->nodeValue << " ";
    14 }
    15 
    16 void test01()
    17 {
    18     map<int, int> tree;
    19     for(int i = 1; i < 10; ++i)
    20     {
    21         tree[i] = i * i;
    22     }
    23     BinaryTree *binarytree = new BinaryTree(tree);
    24     cout << "the depth is " << binarytree->depth() << endl;
    25     cout << "the total number is " << binarytree->count() << endl;
    26     binarytree->postOrder(visit);
    27     binarytree->midOrder(visit);
    28     binarytree->postOrder(visit);
    29 }
    30 
    31 int main(int argc, const char * argv[]) {
    32     // insert code here...
    33     test01();
    34     return 0;
    35 }

    运行结果如下:

    the depth is 4

    the total number is 9

    4 16 64 81 25 9 36 49 1 

    4 16 64 81 25 1 9 36 49 

    4 16 64 81 25 9 36 49 1 

    Program ended with exit code: 0

  • 相关阅读:
    MySQL 中的 3 种注释
    Macbook 彻彻底底的卸载MySQL
    MacBook 安装 MySQL 5.7.29(新手都看得懂的安装教程)
    Java Junit单元测试
    理解 Java 方法引用(方法引用符:“双冒号 :: ”)
    iOS应用启动时间
    iOS遍历数组的同时删除元素
    Xcode 中的断言
    Mac 下 查看 使用某端口的进程和关闭该进程的命令
    RAC 数据库的启动与关闭
  • 原文地址:https://www.cnblogs.com/zz1314/p/12444229.html
Copyright © 2020-2023  润新知