当前位置:   article > 正文

C++ 二叉树(建立、销毁、前中后序遍历和层次遍历,寻找双亲结点等)_c++ 树 销毁

c++ 树 销毁

(1)结构体和类定义

  1. struct BTreeNode {
  2. T data;
  3. BTreeNode* left, * right;
  4. BTreeNode() :data(0), left(nullptr), right(nullptr) {}
  5. BTreeNode(T val, BTreeNode<T>* leftChild = nullptr, BTreeNode<T>* rightChild = nullptr)
  6. :data(val), left(leftChild), right(rightChild) {}
  7. };
  8. template<class T>
  9. class BTree {
  10. public:
  11. BTree() :root(nullptr) {} // 构造函数
  12. BTree(string str); // 重载
  13. void createTree(BTreeNode<T>*& bt, string str); // 创建二次树
  14. ~BTree(); // 析构函数
  15. bool IsEmpty(); // 判二叉树空否?
  16. int Size(BTreeNode<T>* cur); // 计算结点个数
  17. int getSize(); // 获取结点个数
  18. BTreeNode<T>* getData(T& item, BTreeNode<T>* cur); // 取得结点数据
  19. bool Find(T& item); // 判断item是否在树中
  20. int Height(BTreeNode<T>* bt); // 求树高度
  21. int getHeight(); // 获取树高度
  22. BTreeNode<T>* getRoot(); // 取根
  23. void preOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 前序遍历
  24. void inOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 中序遍历
  25. void postOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 后序遍历
  26. void levelOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 层序遍历
  27. vector<T> preOrder(); // 调用前序遍历,返回vector
  28. vector<T> inOrder(); // 调用中序遍历,返回vector
  29. vector<T> postOrder(); // 调用后序遍历,返回vector
  30. vector<T> levelOrder(); // 调用层序遍历,返回vector
  31. void CopyTree(BTreeNode<T>* root, BTreeNode<T>*& copyRoot); // 二叉树复制
  32. void Copy(BTreeNode<T>*& copyRoot); // 调用二叉树复制
  33. void destroyCopyTree(BTreeNode<T>*& copyRoot); // 销毁复制二叉树
  34. BTreeNode<T>* FindParent(BTreeNode<T>* root, BTreeNode<T>* node); // 寻找双亲
  35. BTreeNode<T>* LeftChild(BTreeNode<T>* node) { //求结点 node 的左孩子
  36. return (node != nullptr) ? node->left : nullptr;
  37. }
  38. BTreeNode<T>* RightChild(BTreeNode<T>* node) { //求结点 node 的右孩子
  39. return (node != nullptr) ? node->right : nullptr;
  40. }
  41. protected:
  42. BTreeNode<T>* root;
  43. void destroyTree(BTreeNode<T>* node); // 销毁二叉树
  44. };

(2)创建二叉树

  1. template<class T>
  2. BTree<T>::BTree(string str) {
  3. createTree(root, str);
  4. cout << "报告:创建一颗二叉树,完成!!!" << endl;
  5. }
  6. template<class T>
  7. void BTree<T>::createTree(BTreeNode<T>*& bt, string str) {
  8. static int i = 0;
  9. char ch = ' ';
  10. ch = str[i++];
  11. if (ch == '#') bt = nullptr;
  12. else {
  13. bt = new BTreeNode<T>(ch);
  14. createTree(bt->left, str);
  15. createTree(bt->right, str);
  16. }
  17. };

(3)前中后序遍历和层序遍历

  1. // 前序遍历
  2. template<class T>
  3. void BTree<T>::preOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  4. if (cur == nullptr)
  5. return;
  6. vec.push_back(cur->data); // 中
  7. preOrderTraversal(cur->left, vec); // 左
  8. preOrderTraversal(cur->right, vec); // 右
  9. }
  10. // 调用前序遍历,返回vector
  11. template<class T>
  12. vector<T> BTree<T>::preOrder() {
  13. cout << "获取前序遍历数组...." << endl;
  14. cout << ">>>>";
  15. vector<T> resVec;
  16. preOrderTraversal(root, resVec);
  17. return resVec;
  18. }
  19. // 中序遍历
  20. template<class T>
  21. void BTree<T>::inOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  22. if (cur == nullptr)
  23. return;
  24. inOrderTraversal(cur->left, vec); // 左
  25. vec.push_back(cur->data); // 中
  26. inOrderTraversal(cur->right, vec); // 右
  27. }
  28. // 调用中序遍历,返回vector
  29. template<class T>
  30. vector<T> BTree<T>::inOrder() {
  31. cout << "获取中序遍历数组...." << endl;
  32. cout << ">>>>";
  33. vector<T> resVec;
  34. inOrderTraversal(root, resVec);
  35. return resVec;
  36. }
  37. // 后序遍历
  38. template<class T>
  39. void BTree<T>::postOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  40. if (cur == nullptr)
  41. return;
  42. postOrderTraversal(cur->left, vec); // 左
  43. postOrderTraversal(cur->right, vec); // 右
  44. vec.push_back(cur->data); // 中
  45. }
  46. // 调用后序遍历,返回vector
  47. template<class T>
  48. vector<T> BTree<T>::postOrder() {
  49. cout << "获取后序遍历数组...." << endl;
  50. cout << ">>>>";
  51. vector<T> resVec;
  52. postOrderTraversal(root, resVec);
  53. return resVec;
  54. }
  55. // 层序遍历
  56. template<class T>
  57. void BTree<T>::levelOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  58. if (cur == nullptr) return;
  59. queue<BTreeNode<T>*> Queue;
  60. BTreeNode<T>* p;
  61. Queue.push(cur); // 根结点入队列
  62. while (!Queue.empty()) {
  63. p = Queue.front();
  64. //cout << p->data << " ";//输出出队结点的数据
  65. vec.push_back(p->data);
  66. Queue.pop();
  67. if (p->left != nullptr) {
  68. Queue.push(p->left);
  69. }
  70. if (p->right != nullptr) {
  71. Queue.push(p->right);
  72. }
  73. }
  74. }
  75. // 调用层序遍历,返回vector
  76. template<class T>
  77. vector<T> BTree<T>::levelOrder() {
  78. cout << "获取层序遍历数组...." << endl;
  79. cout << ">>>>";
  80. vector<T> resVec;
  81. levelOrderTraversal(root, resVec);
  82. return resVec;
  83. }

(4)复制二叉树

  1. template<class T>
  2. void BTree<T>::CopyTree(BTreeNode<T>* root, BTreeNode<T>*& copyRoot) {
  3. if (!root) {
  4. copyRoot = nullptr;
  5. }
  6. else {
  7. copyRoot = new BTreeNode<T>;
  8. copyRoot->data = root->data; //复制根节点
  9. CopyTree(root->left, copyRoot->left); //递归复制左子树
  10. CopyTree(root->right, copyRoot->right);//递归复制左子树
  11. }
  12. }
  13. template<class T>
  14. void BTree<T>::Copy(BTreeNode<T>*& copyRoot) {
  15. CopyTree(root, copyRoot);
  16. }

(5)销毁二叉树

  1. template<class T>
  2. void BTree<T>::destroyCopyTree(BTreeNode<T>*& copyRoot) {
  3. destroyTree(copyRoot);
  4. cout << "报告,复制二叉树已销毁完毕!!!" << endl;
  5. }
  6. // 销毁二叉树
  7. template<class T>
  8. void BTree<T>::destroyTree(BTreeNode<T>* bt) {
  9. // 后序遍历删除根为subTree的子树;
  10. if (bt != nullptr) {
  11. destroyTree(bt->left); //删除左子树
  12. destroyTree(bt->right); //删除右子树
  13. delete bt; //删除根结点
  14. }
  15. }

(6)析构函数

  1. // 析构函数
  2. template<class T>
  3. BTree<T>::~BTree<T>() {
  4. //cout << "调用析构函数" << endl;
  5. destroyTree(root);
  6. cout << "报告,这棵树已经销毁完毕!!!" << endl;
  7. }

(7)求树的高度

  1. // 求树高度
  2. template<class T>
  3. int BTree<T>::Height(BTreeNode<T>* bt) {
  4. if (bt == nullptr) return 0;
  5. else {
  6. int leftH = Height(bt->left);
  7. int rightH = Height(bt->right);
  8. return (leftH > rightH) ? leftH + 1 : rightH + 1;
  9. }
  10. }
  11. // 获取树高度
  12. template<class T>
  13. int BTree<T>::getHeight() {
  14. return Height(root);
  15. }

(8)获取结点,判断其是否在二叉树中

  1. // 取得结点数据
  2. template<class T>
  3. BTreeNode<T>* BTree<T>::getData(T& item, BTreeNode<T>* cur) {
  4. if (cur == nullptr) return nullptr;
  5. if (cur->data == item) return cur;
  6. return getData(item, cur->left) != nullptr ? getData(item, cur->left) : getData(item, cur->right);
  7. }
  8. // 判断item是否在树中
  9. template<class T>
  10. bool BTree<T>::Find(T& item) {
  11. if (this->getData(item, root) == nullptr) return false;
  12. else return true;

(9)计算结点个数和获取结点个数

  1. // 计算结点个数
  2. template<class T>
  3. int BTree<T>::Size(BTreeNode<T>* cur) {
  4. if (cur == nullptr)
  5. return 0;
  6. else
  7. return 1 + Size(cur->left) + Size(cur->right);
  8. }
  9. // 获取结点个数
  10. template<class T>
  11. int BTree<T>::getSize() {
  12. return Size(root);
  13. }

(10)二叉树判空

  1. // 判二叉树空否?
  2. template<class T>
  3. bool BTree<T>::IsEmpty() {
  4. return (root == nullptr) ? true : false;
  5. }

(11)获取根结点

  1. // 获取根
  2. template<class T>
  3. BTreeNode<T>* BTree<T>::getRoot() {
  4. if (!root) return nullptr;
  5. else {
  6. return this->root;
  7. }
  8. }

源代码:

btree.h
  1. #pragma once
  2. #include <iostream>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6. template<class T>
  7. struct BTreeNode {
  8. T data;
  9. BTreeNode* left, * right;
  10. BTreeNode() :data(0), left(nullptr), right(nullptr) {}
  11. BTreeNode(T val, BTreeNode<T>* leftChild = nullptr, BTreeNode<T>* rightChild = nullptr)
  12. :data(val), left(leftChild), right(rightChild) {}
  13. };
  14. template<class T>
  15. class BTree {
  16. public:
  17. BTree() :root(nullptr) {} // 构造函数
  18. BTree(string str); // 重载
  19. void createTree(BTreeNode<T>*& bt, string str); // 创建二次树
  20. ~BTree(); // 析构函数
  21. bool IsEmpty(); // 判二叉树空否?
  22. int Size(BTreeNode<T>* cur); // 计算结点个数
  23. int getSize(); // 获取结点个数
  24. BTreeNode<T>* getData(T& item, BTreeNode<T>* cur); // 取得结点数据
  25. bool Find(T& item); // 判断item是否在树中
  26. int Height(BTreeNode<T>* bt); // 求树高度
  27. int getHeight(); // 获取树高度
  28. BTreeNode<T>* getRoot(); // 取根
  29. void preOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 前序遍历
  30. void inOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 中序遍历
  31. void postOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 后序遍历
  32. void levelOrderTraversal(BTreeNode<T>* cur, vector<int>& vec); // 层序遍历
  33. vector<T> preOrder(); // 调用前序遍历,返回vector
  34. vector<T> inOrder(); // 调用中序遍历,返回vector
  35. vector<T> postOrder(); // 调用后序遍历,返回vector
  36. vector<T> levelOrder(); // 调用层序遍历,返回vector
  37. void CopyTree(BTreeNode<T>* root, BTreeNode<T>*& copyRoot); // 二叉树复制
  38. void Copy(BTreeNode<T>*& copyRoot); // 调用二叉树复制
  39. void destroyCopyTree(BTreeNode<T>*& copyRoot); // 销毁复制二叉树
  40. BTreeNode<T>* FindParent(BTreeNode<T>* root, BTreeNode<T>* node); // 寻找双亲
  41. BTreeNode<T>* LeftChild(BTreeNode<T>* node) { //求结点 node 的左孩子
  42. return (node != nullptr) ? node->left : nullptr;
  43. }
  44. BTreeNode<T>* RightChild(BTreeNode<T>* node) { //求结点 node 的右孩子
  45. return (node != nullptr) ? node->right : nullptr;
  46. }
  47. protected:
  48. BTreeNode<T>* root;
  49. void destroyTree(BTreeNode<T>* node); // 销毁二叉树
  50. };
btree.cpp
  1. // 每次写递归,都按照这三要素来写,可以保证大家写出正确的递归算法!
  2. // 1.确定递归函数的参数的返回值
  3. // 2.确定终止条件
  4. // 3.确定单层递归的逻辑
  5. #include "btree.h"
  6. template<class T>
  7. BTree<T>::BTree(string str) {
  8. createTree(root, str);
  9. cout << "报告:创建一颗二叉树,完成!!!" << endl;
  10. }
  11. template<class T>
  12. void BTree<T>::createTree(BTreeNode<T>*& bt, string str) {
  13. static int i = 0;
  14. char ch = ' ';
  15. ch = str[i++];
  16. if (ch == '#') bt = nullptr;
  17. else {
  18. bt = new BTreeNode<T>(ch);
  19. createTree(bt->left, str);
  20. createTree(bt->right, str);
  21. }
  22. };
  23. // 判二叉树空否?
  24. template<class T>
  25. bool BTree<T>::IsEmpty() {
  26. return (root == nullptr) ? true : false;
  27. }
  28. // 计算结点个数
  29. template<class T>
  30. int BTree<T>::Size(BTreeNode<T>* cur) {
  31. if (cur == nullptr)
  32. return 0;
  33. else
  34. return 1 + Size(cur->left) + Size(cur->right);
  35. }
  36. // 获取结点个数
  37. template<class T>
  38. int BTree<T>::getSize() {
  39. return Size(root);
  40. }
  41. // 取得结点数据
  42. template<class T>
  43. BTreeNode<T>* BTree<T>::getData(T& item, BTreeNode<T>* cur) {
  44. if (cur == nullptr) return nullptr;
  45. if (cur->data == item) return cur;
  46. return getData(item, cur->left) != nullptr ? getData(item, cur->left) : getData(item, cur->right);
  47. }
  48. // 判断item是否在树中
  49. template<class T>
  50. bool BTree<T>::Find(T& item) {
  51. if (this->getData(item, root) == nullptr) return false;
  52. else return true;
  53. }
  54. // 求树高度
  55. template<class T>
  56. int BTree<T>::Height(BTreeNode<T>* bt) {
  57. if (bt == nullptr) return 0;
  58. else {
  59. int leftH = Height(bt->left);
  60. int rightH = Height(bt->right);
  61. return (leftH > rightH) ? leftH + 1 : rightH + 1;
  62. }
  63. }
  64. // 获取树高度
  65. template<class T>
  66. int BTree<T>::getHeight() {
  67. return Height(root);
  68. }
  69. // 获取根
  70. template<class T>
  71. BTreeNode<T>* BTree<T>::getRoot() {
  72. if (!root) return nullptr;
  73. else {
  74. return this->root;
  75. }
  76. }
  77. // 析构函数
  78. template<class T>
  79. BTree<T>::~BTree<T>() {
  80. //cout << "调用析构函数" << endl;
  81. destroyTree(root);
  82. cout << "报告,这棵树已经销毁完毕!!!" << endl;
  83. }
  84. // 销毁二叉树
  85. template<class T>
  86. void BTree<T>::destroyTree(BTreeNode<T>* bt) {
  87. // 后序遍历删除根为subTree的子树;
  88. if (bt != nullptr) {
  89. destroyTree(bt->left); //删除左子树
  90. destroyTree(bt->right); //删除右子树
  91. delete bt; //删除根结点
  92. }
  93. }
  94. // 前序遍历
  95. template<class T>
  96. void BTree<T>::preOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  97. if (cur == nullptr)
  98. return;
  99. vec.push_back(cur->data); // 中
  100. preOrderTraversal(cur->left, vec); // 左
  101. preOrderTraversal(cur->right, vec); // 右
  102. }
  103. // 调用前序遍历,返回vector
  104. template<class T>
  105. vector<T> BTree<T>::preOrder() {
  106. cout << "获取前序遍历数组...." << endl;
  107. cout << ">>>>";
  108. vector<T> resVec;
  109. preOrderTraversal(root, resVec);
  110. return resVec;
  111. }
  112. // 中序遍历
  113. template<class T>
  114. void BTree<T>::inOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  115. if (cur == nullptr)
  116. return;
  117. inOrderTraversal(cur->left, vec); // 左
  118. vec.push_back(cur->data); // 中
  119. inOrderTraversal(cur->right, vec); // 右
  120. }
  121. // 调用中序遍历,返回vector
  122. template<class T>
  123. vector<T> BTree<T>::inOrder() {
  124. cout << "获取中序遍历数组...." << endl;
  125. cout << ">>>>";
  126. vector<T> resVec;
  127. inOrderTraversal(root, resVec);
  128. return resVec;
  129. }
  130. // 后序遍历
  131. template<class T>
  132. void BTree<T>::postOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  133. if (cur == nullptr)
  134. return;
  135. postOrderTraversal(cur->left, vec); // 左
  136. postOrderTraversal(cur->right, vec); // 右
  137. vec.push_back(cur->data); // 中
  138. }
  139. // 调用后序遍历,返回vector
  140. template<class T>
  141. vector<T> BTree<T>::postOrder() {
  142. cout << "获取后序遍历数组...." << endl;
  143. cout << ">>>>";
  144. vector<T> resVec;
  145. postOrderTraversal(root, resVec);
  146. return resVec;
  147. }
  148. // 层序遍历
  149. template<class T>
  150. void BTree<T>::levelOrderTraversal(BTreeNode<T>* cur, vector<int>& vec) {
  151. if (cur == nullptr) return;
  152. queue<BTreeNode<T>*> Queue;
  153. BTreeNode<T>* p;
  154. Queue.push(cur); // 根结点入队列
  155. while (!Queue.empty()) {
  156. p = Queue.front();
  157. //cout << p->data << " ";//输出出队结点的数据
  158. vec.push_back(p->data);
  159. Queue.pop();
  160. if (p->left != nullptr) {
  161. Queue.push(p->left);
  162. }
  163. if (p->right != nullptr) {
  164. Queue.push(p->right);
  165. }
  166. }
  167. }
  168. // 调用层序遍历,返回vector
  169. template<class T>
  170. vector<T> BTree<T>::levelOrder() {
  171. cout << "获取层序遍历数组...." << endl;
  172. cout << ">>>>";
  173. vector<T> resVec;
  174. levelOrderTraversal(root, resVec);
  175. return resVec;
  176. }
  177. template<class T>
  178. void BTree<T>::CopyTree(BTreeNode<T>* root, BTreeNode<T>*& copyRoot) {
  179. if (!root) {
  180. copyRoot = nullptr;
  181. }
  182. else {
  183. copyRoot = new BTreeNode<T>;
  184. copyRoot->data = root->data; //复制根节点
  185. CopyTree(root->left, copyRoot->left); //递归复制左子树
  186. CopyTree(root->right, copyRoot->right);//递归复制左子树
  187. }
  188. }
  189. template<class T>
  190. void BTree<T>::Copy(BTreeNode<T>*& copyRoot) {
  191. CopyTree(root, copyRoot);
  192. }
  193. template<class T>
  194. void BTree<T>::destroyCopyTree(BTreeNode<T>*& copyRoot) {
  195. destroyTree(copyRoot);
  196. cout << "报告,复制二叉树已销毁完毕!!!" << endl;
  197. }
  198. template<class T>
  199. BTreeNode<T>* BTree<T>::FindParent(BTreeNode<T>* root, BTreeNode<T>* node) {
  200. if (root == nullptr) return nullptr;
  201. if (root->left == node || root->right == node)
  202. return root; //找到, 返回父结点地址
  203. BTreeNode <T>* p;
  204. if ((p = FindParent(root->left, node)) != nullptr)
  205. return p; //递归在左子树中搜索
  206. else return FindParent(root->right, node);
  207. }
test.cpp
  1. #include "btree.h"
  2. #include "btree.cpp"
  3. //#include <iostream>
  4. //using namespace std;
  5. int main() {
  6. cout << "-------------------------Start--------------------------" << endl;
  7. cout << "---------------------创建原始二叉树---------------------" << endl;
  8. string str = "ABD#G##E##CF###";
  9. BTree<int>* T = new BTree<int>(str);
  10. BTreeNode<int>* root = T->getRoot();
  11. cout << "这棵树有 " << T->getSize() << " 个结点" << endl;
  12. int zifu = 'G';
  13. if (T->Find(zifu)) {
  14. cout << "这棵树有 " << (char)zifu << " 结点" << endl;
  15. }
  16. else {
  17. cout << "这棵树无 " << (char)zifu << " 结点" << endl;
  18. }
  19. BTreeNode<int>* node = T->getData(zifu, root);
  20. if (node) {
  21. cout << (char)node->data << endl;
  22. BTreeNode<int>* nodeParent = T->FindParent(root, node);
  23. if (!nodeParent) {
  24. cout << "找不到父亲结点" << endl;
  25. }
  26. else {
  27. cout << "结点 " << (char)zifu << " 的父亲结点是: " << (char)nodeParent->data << " 结点" << endl;
  28. if (nodeParent->left) cout << "我的左孩子是: " << (char)nodeParent->left->data << endl;
  29. else cout << "我没有左孩子..." << endl;
  30. if (nodeParent->right) cout << "我的右孩子是: " << (char)nodeParent->right->data << endl;
  31. else cout << "我没有右孩子..." << endl;
  32. }
  33. }
  34. cout << "这棵树的高度为: " << T->getHeight() << endl;
  35. vector<int> vec = T->preOrder();
  36. for (auto i : vec) {
  37. cout << (char)i;
  38. }
  39. cout << endl;
  40. vec.clear();
  41. vec = T->inOrder();
  42. for (auto i : vec) {
  43. cout << (char)i;
  44. }
  45. cout << endl;
  46. vec.clear();
  47. vec = T->postOrder();
  48. for (auto i : vec) {
  49. cout << (char)i;
  50. }
  51. cout << endl;
  52. vec.clear();
  53. vec = T->levelOrder();
  54. for (auto i : vec) {
  55. cout << (char)i;
  56. }
  57. cout << endl;
  58. cout << "-----------------------复制二叉树-----------------------" << endl;
  59. // 复制二叉树
  60. //vector<int> vec;
  61. //BTreeNode<int>* root = T->getRoot();
  62. BTreeNode<int>* copyRoot = new BTreeNode<int>;
  63. //T->Copy(copyRoot); // 方法一
  64. T->CopyTree(root, copyRoot); // 方法二
  65. vec.clear();
  66. cout << "获取前序遍历数组...." << endl;
  67. cout << ">>>>";
  68. T->preOrderTraversal(copyRoot, vec);
  69. for (auto i : vec) {
  70. cout << (char)i;
  71. }
  72. cout << endl;
  73. vec.clear();
  74. cout << "获取中序遍历数组...." << endl;
  75. cout << ">>>>";
  76. T->inOrderTraversal(copyRoot, vec);
  77. for (auto i : vec) {
  78. cout << (char)i;
  79. }
  80. cout << endl;
  81. vec.clear();
  82. cout << "获取后序遍历数组...." << endl;
  83. cout << ">>>>";
  84. T->postOrderTraversal(copyRoot, vec);
  85. for (auto i : vec) {
  86. cout << (char)i;
  87. }
  88. cout << endl;
  89. vec.clear();
  90. cout << "获取层序遍历数组...." << endl;
  91. cout << ">>>>";
  92. T->levelOrderTraversal(copyRoot, vec);
  93. for (auto i : vec) {
  94. cout << (char)i;
  95. }
  96. cout << endl;
  97. cout << "---------------------销毁复制二叉树---------------------" << endl;
  98. T->destroyCopyTree(copyRoot);
  99. cout << "---------------------销毁原始二叉树---------------------" << endl;
  100. T->~BTree();
  101. cout << "--------------------------End---------------------------" << endl;
  102. return 0;
  103. }
>>测试结果 
  1. -------------------------Start--------------------------
  2. ---------------------创建原始二叉树---------------------
  3. 报告:创建一颗二叉树,完成!!!
  4. 这棵树有 7 个结点
  5. 这棵树有 G 结点
  6. G
  7. 结点 G 的父亲结点是: D 结点
  8. 我没有左孩子...
  9. 我的右孩子是: G
  10. 这棵树的高度为: 4
  11. 获取前序遍历数组....
  12. >>>>ABDGECF
  13. 获取中序遍历数组....
  14. >>>>DGBEAFC
  15. 获取后序遍历数组....
  16. >>>>GDEBFCA
  17. 获取层序遍历数组....
  18. >>>>ABCDEFG
  19. -----------------------复制二叉树-----------------------
  20. 获取前序遍历数组....
  21. >>>>ABDGECF
  22. 获取中序遍历数组....
  23. >>>>DGBEAFC
  24. 获取后序遍历数组....
  25. >>>>GDEBFCA
  26. 获取层序遍历数组....
  27. >>>>ABCDEFG
  28. ---------------------销毁复制二叉树---------------------
  29. 报告,复制二叉树已销毁完毕!!!
  30. ---------------------销毁原始二叉树---------------------
  31. 报告,这棵树已经销毁完毕!!!
  32. --------------------------End---------------------------

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/621961
推荐阅读
相关标签
  

闽ICP备14008679号