数据结构C++(10)二叉树——链表实现(linkBinaryTree)

抽象基类 queue ,类vectorQueue 的定义同 数据结构C++(6)队列——vector实现(vectorQueue)

异常类 同 数据结构C++(1)线性表——数组实现(arrayList) 。

二叉树的抽象基类 binaryTree 的定义在 binaryTree.h 中:

 1 #pragma once
 2 
 3 #include <functional>
 4 
 5 
 6 template<typename T>
 7 class binaryTree 
 8 {
 9    public:
10       virtual ~binaryTree() {}
11       virtual bool empty() const = 0;
12       virtual int size() const = 0;
13       virtual void outputPreOrder(std::ostream &out) = 0;
14       virtual void outputInOrder(std::ostream &out) = 0;
15       virtual void outputPostOrder(std::ostream &out) = 0;
16       virtual void outputLevelOrder(std::ostream &out) = 0;
17 };

二叉树的节点类型 binaryTreeNode 定义在 binaryTreeNode.h 中:

 1 #pragma once
 2 
 3 template <typename T>
 4 struct binaryTreeNode
 5 {
 6    T element;
 7    binaryTreeNode<T> *leftChild,  *rightChild; 
 8 
 9    binaryTreeNode() {leftChild = rightChild = nullptr;}
10    binaryTreeNode(const T& theElement):element(theElement)
11    {
12       leftChild = rightChild = nullptr;
13    }
14    binaryTreeNode(const T& theElement,
15                   binaryTreeNode *theLeftChild,
16                   binaryTreeNode *theRightChild)
17                   :element(theElement)
18    {
19       leftChild = theLeftChild;
20       rightChild = theRightChild;
21    }
22 };

二叉树的实现类 linkBinaryTree 定义在 linkBinaryTree.h 中:

  1 #pragma once
  2 #include "binaryTree.h"
  3 #include "binaryTreeNode.h"
  4 #include "queue.h"
  5 #include "vectorQueue.h"
  6 #include "myExceptions.h"
  7 
  8 template<typename T>
  9 class linkBinaryTree : public binaryTree<binaryTreeNode<T> >
 10 {
 11 public:
 12     linkBinaryTree() { Root = nullptr; treeSize = 0; }
 13     ~linkBinaryTree();
 14     void makeTree(const T &element, linkBinaryTree<T> &left, linkBinaryTree<T> &right);        //生成树
 15     bool empty() const { return treeSize == 0; }
 16     int size() const { return treeSize; }
 17 
 18     linkBinaryTree<T> &removeLeftSubtree();    //删除左子树
 19     linkBinaryTree<T> &removeRightSubtree();    //删除右子树
 20     void outputPreOrder(std::ostream &out) { preOrder(out, Root); }
 21     void outputInOrder(std::ostream &out) { inOrder(out, Root); }
 22     void outputPostOrder(std::ostream &out) { postOrder(out, Root); }
 23     void outputLevelOrder(std::ostream &out) { levelOrder(out, Root); }
 24 
 25 protected:
 26     binaryTreeNode<T> *Root;
 27     int treeSize;                //子节点数
 28     static int count;
 29 
 30     static int height(binaryTreeNode<T> *tRoot);    //返回树的高度
 31     void preOrder(std::ostream &out, binaryTreeNode<T> *tRoot);        //先跟序遍历
 32     void inOrder(std::ostream &out, binaryTreeNode<T> *tRoot);        //中跟序遍历
 33     void postOrder(std::ostream &out, binaryTreeNode<T> *tRoot);    //后跟序遍历
 34     void levelOrder(std::ostream &out, binaryTreeNode<T> *tRoot);    //按层次遍历
 35     static int countNodes(binaryTreeNode<T> *tRoot);
 36 
 37     void erase(binaryTreeNode<T> *tRoot);        //删除树
 38 };
 39 
 40 template<typename T>
 41 linkBinaryTree<T>::~linkBinaryTree()
 42 {
 43     std::cout << "~linkBinaryTree(), Root = " << Root << endl;
 44     erase(Root);
 45     Root = nullptr;
 46 }
 47 
 48 template<typename T>
 49 int linkBinaryTree<T>::height(binaryTreeNode<T> *tRoot)
 50 {
 51     if (nullptr == tRoot)
 52         return 0;
 53     int hL = height(tRoot->leftChild);
 54     int hR = height(tRoot->rightChild);
 55     if (hL > hR)
 56         return ++hL;
 57     else
 58         return ++hR;
 59 }
 60 
 61 template<typename T>
 62 int linkBinaryTree<T>::countNodes(binaryTreeNode<T> *tRoot)
 63 {
 64     if (nullptr == tRoot)
 65         return 0;
 66     int hL = countNodes(tRoot->leftChild);
 67     int hR = countNodes(tRoot->rightChild);
 68     return hL + hR + 1;
 69 }
 70 
 71 template<typename T>
 72 void linkBinaryTree<T>::preOrder(std::ostream &out, binaryTreeNode<T> *tRoot)
 73 {
 74     if (nullptr == tRoot)
 75         return;
 76     out << tRoot->element << " ";
 77     preOrder(out, tRoot->leftChild);
 78     preOrder(out, tRoot->rightChild);
 79 }
 80 
 81 template<typename T>
 82 void linkBinaryTree<T>::inOrder(std::ostream &out, binaryTreeNode<T> *tRoot)
 83 {
 84     if (nullptr == tRoot)
 85         return;
 86 
 87     inOrder(out, tRoot->leftChild);
 88     out << tRoot->element << " ";
 89     inOrder(out, tRoot->rightChild);
 90 }
 91 
 92 template<typename T>
 93 void linkBinaryTree<T>::postOrder(std::ostream &out, binaryTreeNode<T> *tRoot)
 94 {
 95     if (nullptr == tRoot)
 96         return;
 97     postOrder(out, tRoot->leftChild);
 98     postOrder(out, tRoot->rightChild);
 99     out << tRoot->element << " ";
100 }
101 
102 template<typename T>
103 void linkBinaryTree<T>::levelOrder(std::ostream &out, binaryTreeNode<T> *tRoot)
104 {
105     int count = 0;
106     vectorQueue<binaryTreeNode<T> *> treeQueue;
107     binaryTreeNode<T> *Tmp = tRoot;
108     while (nullptr != Tmp)
109     {
110         count++;
111         out << Tmp->element << " ";
112         if (nullptr != Tmp->leftChild)
113             treeQueue.push(Tmp->leftChild);
114         if (nullptr != Tmp->rightChild)
115             treeQueue.push(Tmp->rightChild);
116         try
117         {
118             Tmp = treeQueue.front();
119         }
120         catch (queueEmpty)
121         {
122             return;
123         }
124         treeQueue.pop();
125     }
126 }
127 
128 template<typename T>
129 void linkBinaryTree<T>::erase(binaryTreeNode<T> *tRoot)
130 {
131     if (nullptr == tRoot)
132         return;
133     erase(tRoot->leftChild);
134     erase(tRoot->rightChild);
135     delete tRoot;
136 }
137 
138 template<typename T>
139 void linkBinaryTree<T>::makeTree(const T& element, linkBinaryTree<T> &left, linkBinaryTree<T> &right)
140 {
141     binaryTreeNode<T> *New = new binaryTreeNode<T>(element, left.Root, right.Root);
142     Root = New;
143     treeSize = left.treeSize + right.treeSize + 1;
144 }
145 
146 template<typename T>
147 linkBinaryTree<T> &linkBinaryTree<T>::removeLeftSubtree()        //删除左子树
148 {
149     treeSize -= countNodes(Root->leftChild);
150 //    erase(Root->leftChild);
151     linkBinaryTree<T> *Tmp = new linkBinaryTree<T>;
152     Tmp->Root = Root->leftChild;
153     Root->leftChild = nullptr;
154     return *Tmp;
155 }
156 
157 template<typename T>
158 linkBinaryTree<T> &linkBinaryTree<T>::removeRightSubtree()    //删除右子树
159 {
160     treeSize -= countNodes(Root->rightChild);
161 //    erase(Root->leftChild);
162     linkBinaryTree<T> *Tmp = new linkBinaryTree<T>;
163     Tmp->Root = Root->rightChild;
164     Root->rightChild = nullptr;
165     return *Tmp;
166 }

参考文献:

[1].Sartaj Sahni. 数据结构、算法与应用[M]. 机械工业出版社, 2000.

转载于:https://www.cnblogs.com/peformer/p/8057665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值