• 二叉树的基本运算


    BTree.h

      1 #include <iostream>
      2 using namespace std;
      3 template<class T>
      4 class SeqQueue
      5 {
      6 public:
      7     SeqQueue(int msize);
      8     ~SeqQueue() { delete[] q; };
      9     bool IsEmpty() const { return front == rear; }
     10     bool IsFull() const { return (rear + 1) % maxsize == front; }
     11     bool Front(T& x) const;
     12     bool EnQueue(T x);
     13     bool DeQueue();
     14     void clear() { front = rear = 0; }
     15 private:
     16     int front, rear;
     17     int maxsize;
     18     T *q;
     19 };
     20 template<class T>
     21 SeqQueue<T>::SeqQueue(int msize)
     22 {
     23     maxsize = msize;
     24     q = new T[maxsize];
     25     front = rear = 0;
     26 }
     27 template<class T>
     28 bool SeqQueue<T>::Front(T& x) const
     29 {
     30     if (IsEmpty())
     31     {
     32         cout << "empty" << endl;
     33         return false;
     34     }
     35     x = q[(front + 1) % maxsize];
     36     return true;
     37 }
     38 template<class T>
     39 bool SeqQueue<T>::EnQueue(T x)
     40 {
     41     if (IsFull())
     42     {
     43         cout << "Full" << endl;
     44         return false;
     45     }
     46     q[(rear = (rear + 1) % maxsize)] = x;
     47     return true;
     48 }
     49 template<class T>
     50 bool SeqQueue<T>::DeQueue()
     51 {
     52     if (IsEmpty())
     53     {
     54         cout << "Underflow" << endl;
     55         return false;
     56     }
     57     front = (front + 1) % maxsize;
     58     return true;
     59 }
     60 template <class T>
     61 struct BTNode
     62 {
     63     T element;
     64     BTNode<T> *lChild, *rChild;
     65     BTNode()
     66     {
     67         lChild = rChild = NULL;
     68     }
     69     BTNode(const T& x)
     70     {
     71         element = x;
     72         lChild = rChild = NULL;
     73     }
     74     BTNode(const T& x, BTNode<T> *l, BTNode<T> *r)
     75     {
     76         element = x;
     77         lChild = l;
     78         rChild = r;
     79     }
     80 };
     81 
     82 template <class T>
     83 class BinaryTree
     84 {
     85 public:
     86     BinaryTree() { root = NULL; }
     87     ~BinaryTree() { clear(root); }
     88     bool IsEmpty()const;
     89     void clear() { clear(root); }
     90     bool Root(T &x)const;
     91     int size();
     92     void MakeTree(const T &e, BinaryTree<T>& left, BinaryTree<T>& right);
     93     void BreakTree(T &e, BinaryTree<T>& left, BinaryTree<T>& right);
     94     void levelorder(void(*Visit)(T& x));
     95     void PreOrder(void(*Visit)(T& x));
     96     void InOrder(void(*Visit)(T& x));
     97     void PostOrder(void(*Visit)(T& x));
     98     void change();
     99     int height();
    100 protected:
    101     BTNode<T>* root;
    102 private:
    103     void clear(BTNode<T>* t);
    104     int size(BTNode<T>* t);
    105     void levelorder(void(*Visit)(T& x), BTNode<T>* t);
    106     void PreOrder(void(*Visit)(T& x), BTNode<T>* t);
    107     void InOrder(void(*Visit)(T& x), BTNode<T>* t);
    108     void PostOrder(void(*Visit)(T& x), BTNode<T>* t);
    109     void change(BTNode<T>* t);
    110     int height(BTNode<T>* t);
    111 };
    112 
    113 template <class T>
    114 void BinaryTree<T>::clear(BTNode<T>* t)
    115 {
    116     if (!t)
    117         return;
    118     clear(t->lChild);
    119     clear(t->rChild);
    120     delete t;
    121 }
    122 
    123 template <class T>
    124 bool BinaryTree<T>::Root(T &x)const
    125 {
    126     if (root)
    127     {
    128         x = root->element;
    129         return true;
    130     }
    131     else
    132         return false;
    133 }
    134 
    135 template <class T>
    136 void BinaryTree<T>::MakeTree(const T &e, BinaryTree<T>& left, BinaryTree<T>& right)
    137 {
    138     if (root || &left == &right)
    139         return;
    140     root = new BTNode<T>(e, left.root, right.root);
    141     left.root = right.root = NULL;
    142 }
    143 
    144 template <class T>
    145 void BinaryTree<T>::BreakTree(T &x, BinaryTree<T>& left, BinaryTree<T>& right)
    146 {
    147     if (!root || &left == &right || left.root || right.root)
    148         return;
    149     x = root->element;
    150     left.root = root->lChild;
    151     right.root = root->rChild;
    152     delete root;
    153     root = NULL;
    154 }
    155 
    156 template <class T>
    157 void Visit(T& x)
    158 {
    159     cout << x << " ";
    160 }
    161 
    162 template <class T>
    163 void BinaryTree<T>::PreOrder(void(*Visit)(T& x))
    164 {
    165     PreOrder(Visit, root);
    166 }
    167 
    168 template<class T>
    169 void BinaryTree<T>::PreOrder(void(*Visit)(T& x), BTNode<T>* t)
    170 {
    171     if (t)
    172     {
    173         Visit(t->element);
    174         PreOrder(Visit, t->lChild);
    175         PreOrder(Visit, t->rChild);
    176     }
    177     
    178 }
    179 
    180 template <class T>
    181 void BinaryTree<T>::InOrder(void(*Visit)(T& x))
    182 {
    183     InOrder(Visit, root);
    184 }
    185 
    186 template<class T>
    187 void BinaryTree<T>::InOrder(void(*Visit)(T& x), BTNode<T>* t)
    188 {
    189     if (t)
    190     {
    191         InOrder(Visit, t->lChild);
    192         Visit(t->element);
    193         InOrder(Visit, t->rChild);
    194     }
    195 }
    196 
    197 template <class T>
    198 void BinaryTree<T>::PostOrder(void(*Visit)(T& x))
    199 {
    200     PostOrder(Visit, root);
    201 }
    202 
    203 template <class T>
    204 void BinaryTree<T>::PostOrder(void(*Visit)(T& x), BTNode<T>* t)
    205 {
    206     if (t)
    207     {
    208         PostOrder(Visit, t->lChild);
    209         PostOrder(Visit, t->rChild);
    210         Visit(t->element);
    211     }
    212 }
    213 template <class T>
    214 void BinaryTree<T>::levelorder(void(*Visit)(T& x))
    215 {
    216     levelorder(Visit, root);
    217 }
    218 template <class T>
    219 void BinaryTree<T>::levelorder(void(*Visit)(T& x), BTNode<T>* t)
    220 {
    221     SeqQueue<BTNode<T>*>se(100);
    222     se.EnQueue(t);
    223     BTNode<T> *tmp;
    224     while (!se.IsEmpty())
    225     {
    226         se.Front(tmp);
    227         Visit(tmp->element);
    228         se.DeQueue();
    229         if (tmp->lChild)
    230             se.EnQueue(tmp->lChild);
    231         if (tmp->rChild)
    232             se.EnQueue(tmp->rChild);
    233     }
    234 
    235 }
    236 
    237 template <class T>
    238 int BinaryTree<T>::size()
    239 {
    240     return size(root);
    241 }
    242 
    243 template <class T>
    244 int BinaryTree<T>::size(BTNode<T>* t)
    245 {
    246     if (!t)
    247         return 0;
    248     else
    249         return size(t->lChild) + size(t->rChild) + 1;
    250 }
    251 
    252 template <class T>
    253 void BinaryTree<T>::change()
    254 {
    255     change(root);
    256 }
    257 
    258 template <class T>
    259 void BinaryTree<T>::change(BTNode<T>* t)
    260 {
    261     if (!t)
    262         return;
    263     BTNode<T>* temp;
    264     temp = t->lChild;
    265     t->lChild = t->rChild;
    266     t->rChild = temp;
    267     change(t->lChild);
    268     change(t->rChild);
    269 }
    270 
    271 template <class T>
    272 int BinaryTree<T>::height()
    273 {
    274     return height(root);
    275 }
    276 
    277 template <class T>
    278 int BinaryTree<T>::height(BTNode<T>* t)
    279 {
    280     int templ;
    281     int tempr;
    282     if (!t)
    283         return 0;
    284     templ = height(t->lChild);
    285     tempr = height(t->rChild);
    286     if (templ++>tempr++)
    287         return templ;
    288     else
    289         return tempr;
    290 }

    Test.cpp

    #include "BTree.h"
    
    int main()
    {
        BinaryTree<char> a, b, x, y, z;
        y.MakeTree('E', a, b);
        z.MakeTree('F', a, b);
        x.MakeTree('C', y, z);
        y.MakeTree('D', a, b);
        z.MakeTree('B', y, x);
        cout << "Preorder" << endl;
        z.PreOrder(Visit);
        cout << endl;
        cout << "Inorder" << endl;
        z.InOrder(Visit);
        cout << endl;
        cout << "Postorder:" << endl;
        z.PostOrder(Visit);
        cout << endl;
        cout << "Levelorder:" << endl;
        z.levelorder(Visit);
        cout << endl;
        cout << "SizeOfNode:";
        cout << z.size() << endl;
        z.change();
        cout << "TheChanged:" << endl;
        z.PreOrder(Visit);
        cout <<endl<< "TheHeight:";
        cout << z.height() << endl;
        cout << endl;
        cout << "Cleared" << endl;
        z.clear();
        cout << "Tree is Null" << endl;
        system("pause");
        return 0;
        
    }

    实现了二叉树的插入删除等操作

  • 相关阅读:
    C语言I博客作业05
    C语言I博客作业04
    C语言II博客作业04
    C语言II博客作业03
    C语言II—作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/zlgxzswjy/p/4937292.html
Copyright © 2020-2023  润新知