zoukankan      html  css  js  c++  java
  • [数据结构] 二叉树

      1 数据结构的练习与巩固
      2 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      3 #include <iostream>
      4 using namespace std;
      5 
      6 struct BTNode
      7 {
      8     char Data;
      9     BTNode* lChild, * rChild;
     10     BTNode()
     11     {
     12         lChild = NULL;
     13         rChild = NULL;
     14     }
     15     BTNode(char& data, BTNode* l = NULL, BTNode* r = NULL)
     16     {
     17         Data = data;
     18         lChild = l;
     19         rChild = r;
     20     }
     21 };
     22 
     23 class BT
     24 {
     25 public:
     26     BTNode* root;
     27     char RefValue;
     28 
     29 public:
     30     BT() { root = NULL; }
     31     BT(char value)
     32         { RefValue = value, root = NULL; }
     33 
     34     BTNode* lChild(BTNode* t)
     35         { return(t != NULL) ? t->lChild : NULL; }
     36     BTNode* rChild(BTNode* t)
     37         { return(t != NULL) ? t->lChild : NULL; }
     38     BTNode*& getRoot()
     39         { return root; }
     40 
     41     bool IsEmpty()
     42         { return root == NULL; }
     43 
     44     void preOrder(BTNode*& subTree)
     45     {
     46         if (subTree != NULL)
     47         {
     48             cout << subTree->Data << ' ';
     49             preOrder(subTree->lChild);
     50             preOrder(subTree->rChild);
     51         }
     52     }
     53     void inOrder(BTNode*& subTree)
     54     {
     55         if (subTree != NULL)
     56         {
     57             inOrder(subTree->lChild);
     58             cout << subTree->Data << ' ';
     59             inOrder(subTree->rChild);
     60         }
     61     }
     62     void postOrder(BTNode*& subTree)
     63     {
     64         if (subTree != NULL)
     65         {
     66             postOrder(subTree->lChild);
     67             postOrder(subTree->rChild);
     68             cout << subTree->Data << ' ';
     69         }
     70     }
     71 
     72     void createBT(BTNode*& subTree)
     73     {
     74         char ch;
     75         cin >> ch;
     76         if (ch == RefValue)subTree = NULL;
     77         else
     78         {
     79             subTree = new BTNode(ch);
     80             createBT(subTree->lChild);
     81             createBT(subTree->rChild);
     82         }
     83     }
     84     
     85     void size(BTNode*& subTree, int& count)
     86     {
     87         if (subTree != NULL)
     88         {
     89             count++;
     90             size(subTree->lChild, count);
     91             size(subTree->rChild, count);
     92         }
     93         else
     94             return;
     95     }
     96 
     97     void leaf(BTNode*& subTree, int& count)
     98     {
     99         if (subTree != NULL)
    100         {
    101             if (subTree->lChild == NULL && subTree->rChild == NULL)
    102             {
    103                 count++;
    104                 return;
    105             }
    106             else
    107             {
    108                 leaf(subTree->lChild, count);
    109                 leaf(subTree->rChild, count);
    110             }
    111         }
    112 
    113         else return;
    114     }
    115 
    116     void IsBST(BTNode*& subTree, int& Flag)
    117     {
    118         if (subTree != NULL)
    119         {
    120             if (subTree->lChild == NULL && subTree->lChild == NULL)
    121             {
    122                 return;
    123             }
    124 
    125             if (subTree->lChild == NULL)
    126             {
    127                 if (subTree->rChild->Data < subTree->Data) Flag = 0;
    128                 IsBST(subTree->rChild, Flag);
    129             }
    130 
    131             if (subTree->rChild == NULL)
    132             {
    133                 if (subTree->lChild->Data > subTree->Data) Flag = 0;
    134                 IsBST(subTree->lChild, Flag);
    135             }
    136 
    137             else
    138             {
    139                 if (subTree->lChild->Data > subTree->Data) Flag = 0;
    140                 if (subTree->rChild->Data < subTree->Data) Flag = 0;
    141 
    142                 IsBST(subTree->lChild, Flag);
    143                 IsBST(subTree->rChild, Flag);
    144             }
    145         }
    146     }
    147 
    148     void height(BTNode*& subTree, char X, int& Height)
    149     {
    150         if (subTree != NULL)
    151         {
    152             if (subTree->Data == X)
    153             {
    154                 return;
    155             }
    156             else if (subTree->Data > X)
    157             {
    158                 Height++;
    159                 height(subTree->lChild, X, Height);
    160             }
    161             else if (subTree->Data < X)
    162             {
    163                 Height++;
    164                 height(subTree->rChild, X, Height);
    165             }
    166         }
    167     }
    168 };
    169 
    170 int main()
    171 {
    172     int c1 = 0;
    173     int c2 = 0;
    174     int Height = 1;
    175     int Flag = 1;
    176     BT t('#');
    177     t.createBT(t.getRoot());
    178 
    179     cout << endl << "前序遍历" << endl;
    180     t.preOrder(t.getRoot());
    181     cout << endl << "中序遍历" << endl;
    182     t.inOrder(t.getRoot());
    183     cout << endl << "后序遍历" << endl;
    184     t.postOrder(t.getRoot());
    185 
    186     t.size(t.getRoot(), c1);
    187     cout << endl << "结点个数:" << c1 << endl;
    188     t.leaf(t.getRoot(), c2);
    189     cout << endl << "叶子个数:" << c2 << endl;
    190 
    191     t.IsBST(t.getRoot(), Flag);
    192     if (Flag) cout << "是排序树" << endl;
    193     else cout << "非排序树" << endl;
    194 
    195     char X;
    196     cin >> X;
    197     t.height(t.getRoot(), X, Height);
    198     cout << endl << "当前高度:" << Height << endl;
    199 }
  • 相关阅读:
    jquery同步请求
    js换空格为别的元素
    获取页面的checkbox,并给参数赋值
    jQuery判断checkbox是否选中的3种方法
    opencv基础知识------IplImage, CvMat, Mat 的关系和相互转换
    Opencv基础知识-----视频的读取和操作
    OpenCV 基础知识------图像创建、访问、转换
    windows消息钩子注册底层机制浅析
    Windows内核遍历驱动模块源码分析
    VC 快速创建多层文件夹
  • 原文地址:https://www.cnblogs.com/nagishiro/p/13236747.html
Copyright © 2011-2022 走看看