• 树的遍历等操作


      1 #include<iostream>
      2 #include<queue>
      3 #include<vector>
      4 #include<stack>
      5 using namespace std;
      6 struct TreeNode {
      7     int data;
      8     struct TreeNode *left;
      9     struct TreeNode *right;
     10     TreeNode(int x) :data(x), left(NULL), right(NULL) {}
     11 };
     12 class Btree
     13 {
     14     static int n;
     15     static int m;
     16 public:
     17     TreeNode *root;
     18     Btree()
     19     {
     20         root = NULL;
     21     }
     22     void create_Btree(int);                     //二叉排序树的方式建树
     23     //void create_Btree1(int);                    //先序遍历的方式建树
     24     void BreadthFirstSearch(TreeNode *);              //层次遍历(广度优先遍历)
     25     void DepthFirstSearch(TreeNode *);              //深度遍历(深度优先遍历)
     26     void Preorder(TreeNode *);                  //先序遍历
     27     void inorder(TreeNode *);                   //中序遍历
     28     void Postorder(TreeNode *);                 //后序遍历
     29     void preOrderTravel(TreeNode* root);  //非递归先序遍历
     30     void inorderTravel(TreeNode* root);   //非递归中序遍历
     31     void postOrderTravel(TreeNode* root); //非递归后序遍历
     32     void display1() { Preorder(root); cout << endl; }
     33     void display2() { inorder(root); cout << endl; }
     34     void display3() { Postorder(root); cout << endl; }
     35     void display4() { BreadthFirstSearch(root); cout << endl; }
     36     void display5() { DepthFirstSearch(root); cout << endl; }
     37     void display6() { preOrderTravel(root); cout << endl; }
     38     void display7() { inorderTravel(root); cout << endl; }
     39     void display8() { postOrderTravel(root); cout << endl; }
     40     int count(TreeNode *);                      //计算二叉树的个数
     41     int findleaf(TreeNode *);                   //求二叉树叶子的个数
     42     int findnode(TreeNode *);                   //求二叉树中度数为1的结点数量,这是当初考数据结构时候的最后一道题目
     43 };
     44 int Btree::n = 0;
     45 int Btree::m = 0;
     46 void Btree::create_Btree(int x)              //二叉排序树的方式建树
     47 {
     48     TreeNode *newnode = new TreeNode(x);
     49     if (root == NULL)
     50         root = newnode;
     51     else
     52     {
     53         TreeNode *back = NULL;
     54         TreeNode *current = root;
     55         while (current != NULL)
     56         {
     57             back = current;
     58             if (current->data>x)
     59                 current = current->left;
     60             else
     61                 current = current->right;
     62         }
     63         if (back->data>x)
     64             back->left = newnode;
     65         else
     66             back->right = newnode;
     67     }
     68 }
     69 //void Btree::create_Btree1(int x)              //先序遍历的方式建树
     70 //{
     71 //    TreeNode *newnode = new TreeNode(x);
     72 //    if (root == NULL)
     73 //        root = newnode;
     74 //}
     75 void Btree::BreadthFirstSearch(TreeNode *temp)              //层次遍历
     76 {
     77     if (temp == NULL)
     78         return;
     79     queue<TreeNode*> treenode;
     80     treenode.push(temp);
     81     while (treenode.size())
     82     {
     83         TreeNode *pNode = treenode.front();
     84         treenode.pop();
     85         cout << pNode->data << " ";
     86         if (pNode->left) treenode.push(pNode->left);
     87         if (pNode->right) treenode.push(pNode->right);
     88     }
     89 }
     90 void Btree::DepthFirstSearch(TreeNode * temp)          // 深度优先遍历
     91 {
     92     if (temp == NULL)
     93         return;
     94     stack<TreeNode*> nodeStack;
     95     nodeStack.push(root);
     96     while (nodeStack.size())
     97     {
     98         TreeNode *pNode = nodeStack.top();
     99         cout << pNode->data << " ";
    100         nodeStack.pop();
    101         if (pNode->right)
    102         {
    103             nodeStack.push(pNode->right);
    104         }
    105         if (pNode->left)
    106         {
    107             nodeStack.push(pNode->left);
    108         }
    109     }
    110 }
    111 int Btree::count(TreeNode *p)
    112 {
    113     if (p == NULL)
    114         return 0;
    115     else
    116         return count(p->left) + count(p->right) + 1;      //这是运用了函数嵌套即递归的方法。
    117 }
    118 void Btree::Preorder(TreeNode *temp)    //这是先序遍历二叉树,采用了递归的方法。
    119 {
    120     if (temp != NULL)
    121     {
    122         cout << temp->data << " ";
    123         Preorder(temp->left);
    124         Preorder(temp->right);
    125     }
    126 }
    127 void Btree::preOrderTravel(TreeNode* root){       //这是先序遍历二叉树,采用了非递归的方法。
    128 
    129     vector<int> res;
    130     if (root == NULL)
    131         cout << 0 << endl;  //return res;
    132     stack<TreeNode*> s;
    133     while (root || !s.empty()){
    134 
    135         while (root){
    136             res.push_back(root->data);
    137             s.push(root);
    138             root = root->left;
    139         }
    140         root = s.top();
    141         s.pop();
    142         root = root->right;
    143 
    144     }
    145     for (int i = 0; i < res.size(); i++)
    146     {
    147         cout << res[i] << " ";
    148     }
    149 }
    150 void Btree::inorder(TreeNode *temp)      //这是中序遍历二叉树,采用了递归的方法。
    151 {
    152     if (temp != NULL)
    153     {
    154         inorder(temp->left);
    155         cout << temp->data << " ";
    156         inorder(temp->right);
    157     }
    158 }
    159 void Btree::inorderTravel(TreeNode* root){     //这是中序遍历二叉树,采用了非递归的方法。
    160 
    161     vector<int> res;
    162     if (root == NULL)
    163         cout << 0 << endl;//return res;
    164     stack<TreeNode*>s;
    165     while (root || !s.empty()){
    166 
    167         while (root){
    168             s.push(root);
    169             root = root->left;
    170         }
    171         root = s.top();
    172         s.pop();
    173         res.push_back(root->data);
    174         root = root->right;
    175     }
    176     for (int i = 0; i < res.size(); i++)
    177     {
    178         cout << res[i] << " ";
    179     }
    180 }
    181 void Btree::Postorder(TreeNode *temp)     //这是后序遍历二叉树,采用了递归的方法。
    182 {
    183     if (temp != NULL)
    184     {
    185         Postorder(temp->left);
    186         Postorder(temp->right);
    187         cout << temp->data << " ";
    188     }
    189 }
    190 void Btree::postOrderTravel(TreeNode* root)    //这是后序遍历二叉树,采用了非递归的方法。
    191 {     
    192         vector<int> res;
    193         if (root == NULL)
    194             cout << 0 << endl;//return res;
    195         stack<TreeNode*> s;
    196         s.push(root);
    197         TreeNode* head = root;
    198         while (!s.empty()){
    199             TreeNode* t = s.top();
    200             if (!t->left&&!t->right || t->left == head || t->right == head){
    201                 res.push_back(t->data);
    202                 s.pop();
    203                 head = t;
    204             }
    205             else{
    206                 if (t->right)s.push(t->right);
    207                 if (t->left)s.push(t->left);
    208             }
    209         }
    210         for (int i = 0; i < res.size(); i++)
    211         {
    212             cout << res[i] << " ";
    213         }
    214 }
    215 
    216 int Btree::findleaf(TreeNode *temp)
    217 {
    218     if (temp == NULL)return 0;
    219     else
    220     {
    221         if (temp->left == NULL&&temp->right == NULL)return n += 1;
    222         else
    223         {
    224             findleaf(temp->left);
    225             findleaf(temp->right);
    226         }
    227         return n;
    228     }
    229 }
    230 int Btree::findnode(TreeNode *temp)
    231 {
    232     if (temp == NULL)return 0;
    233     else
    234     {
    235         if (temp->left != NULL&&temp->right != NULL)
    236         {
    237             findnode(temp->left);
    238             findnode(temp->right);
    239         }
    240         if (temp->left != NULL&&temp->right == NULL)
    241         {
    242             m += 1;
    243             findnode(temp->left);
    244         }
    245         if (temp->left == NULL&&temp->right != NULL)
    246         {
    247             m += 1;
    248             findnode(temp->right);
    249         }
    250     }
    251     return m;
    252 }
    253 
    254 
    255 int main()
    256 {
    257     Btree A;
    258     int array[] = { 7, 4, 2, 3, 15, 35, 6, 45, 55, 20, 1, 14, 56, 57, 58 };
    259     int k;
    260     k = sizeof(array) / sizeof(array[0]);
    261     cout << "建立排序二叉树顺序: " << endl;
    262     for (int i = 0; i<k; i++)
    263     {
    264         cout << array[i] << " ";
    265         A.create_Btree(array[i]);
    266     }
    267     cout << endl;
    268     cout << "二叉树节点个数: " << A.count(A.root) << endl;
    269     cout << "二叉树叶子个数:" << A.findleaf(A.root) << endl;
    270     cout << "二叉树中度数为1的结点的数量为:" << A.findnode(A.root) << endl;
    271     cout << endl << "深度遍历序列: " << endl;
    272     A.display5();
    273     cout << endl << "层次遍历序列: " << endl;
    274     A.display4();
    275     cout << endl << "先序遍历序列: " << endl;
    276     A.display1();
    277     cout << endl << "中序遍历序列: " << endl;
    278     A.display2();
    279     cout << endl << "后序遍历序列: " << endl;
    280     A.display3();
    281     cout << endl << "先序非递归遍历:" << endl;
    282     A.display6();
    283     cout << endl << "中序非递归遍历:" << endl;
    284     A.display7();
    285     cout << endl << "后序非递归遍历:" << endl;
    286     A.display8();
    287     system("pause");
    288     return 0;
    289 }

    参考:http://www.cnblogs.com/zhangbaochong/p/5492877.html

    https ://zhidao.baidu.com/question/1176347680995216059.html

    http ://blog.csdn.net/langmanqishizaijia/article/details/51106870

  • 相关阅读:
    160720、SSM-Shiro使用详解
    Python学习(4)运算符
    Python学习(3)变量类型
    Python学习(2)基本语法
    Python学习(1)安装Python
    MonkeyRunner学习(3)脚本编辑
    MonkeyRunner学习(2)常用命令
    MonkeyRunner学习(1)测试连接
    Monkey学习(4)简单测试实例
    Monkey学习(3)如何在Android模拟器中安装apk
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/7444326.html
Copyright © 2020-2023  润新知