当前位置:   article > 正文

数据结构实验7:实现二分查找、二叉排序(查找)树和AVL树_头歌数据结构查找实验构造二插排序树

头歌数据结构查找实验构造二插排序树

                                         实验7

                                                           学号:      姓名:      专业:

 

7.1实验目的

(1) 掌握顺序表的查找方法,尤其是二分查找方法。

(2) 掌握二叉排序树的建立及查找。

查找是软件设计中的最常用的运算,查找所涉及到的表结构的不同决定了查找的方法及其性能。二分查找是顺序表的查找中的最重要的方法,应能充分理解其实现方法和有关性能,并能借助其判定树结构来加深理解。二叉排序树结构在实验时具有一定的难度,可结合二叉树的有关内容和方法来实现。

7.2 实验任务

编写算法实现下列问题的求解。

(1) 对下列数据表,分别采用二分查找算法实现查找,给出查找过程依次所比较的元素(的下标),并以二分查找的判定树来解释。

第一组测试数据:

数据表为 (1,2,3,4,6,7,8,9,10,11,12,13,17,18,19,20,24,25,26,30,35,40,45,50,100)

查找的元素分别为: 2,8,20,  30,50,5,15,33,110   

第二组数据:

数据表为 (2,3,5,7,8,10,12,15,18,20,22,25,30,35,40,45,50,55,60, 80,100)

查找的元素分别为: 22,8,80,3,100,1,13,120

(2) 设计出在二叉排序树中插入结点的算法,在此基础上实现构建二叉排序树的算法。     

测试数据:构建二叉排序树的输入序列如下:

第一组数据:

100,150,120,50,70,60,80,170,180,160,110,30,40,35,175   

第二组数据:

100,70,60,80,150,120,50,160,30,40,170,180,175,35

(3) 设计算法在二叉排序树中查找指定值的结点。    

测试数据:在任务<1>中第一组测试数据所构造的二叉排序树中,分别查找下列元素:    150,70,160,190,10,55,175

(4) 设计算法在二叉排序树中删除特定值的结点。    

测试数据:在任务(1)中第一组测试数据所构造的二叉排序树中,分别删除下列元素:30,150,100

(5) 已知整型数组A[1..26]递增有序,设计算法以构造一棵平衡的二叉排序树来存放该数组中的所有元素。    

测试数据:数组元素分别为:

第一组数据:

(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26)

第二组数据:

(1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190,210,231,253,277,302,328)

7.3实验数据要求

自我编写测试样例,要求每个功能函数的测试样例不少于两组

7.4 运行结果截图及说明

图1 测试(1)①

 

图2 测试(1)②

 

图3 测试(2)①

 

图4 测试(2)②

 

图5 测试(3)

 

 

图6 测试(4)

 

 

 

图7 测试(5)①

 

图8 测试(5)①

 

 

 

图9 测试(5)①

 

 

 

图10 测试(5)①

 

 

图11 测试(5)②

 

 

图12 测试(5)②

 

 

图13 测试(5)②

 

图14 测试(5)①(全)

 

 

 

图15 测试(5)②(全)

 

 

7.5 附源代码

二分查找:

  1. // stdafx.h : include file for standard system include files,
  2. // or project specific include files that are used frequently, but
  3. // are changed infrequently
  4. //
  5. #if !defined(AFX_STDAFX_H__940FF418_5647_412A_8B4F_3C89C07F8CA5__INCLUDED_)
  6. #define AFX_STDAFX_H__940FF418_5647_412A_8B4F_3C89C07F8CA5__INCLUDED_
  7. #if _MSC_VER > 1000
  8. #pragma once
  9. #endif // _MSC_VER > 1000
  10. #include <stdc++.h>
  11. using namespace std;
  12. typedef long elementType;
  13. const long maxn = 10000 + 3;
  14. // TODO: reference additional headers your program requires here
  15. //{{AFX_INSERT_LOCATION}}
  16. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  17. #endif // !defined(AFX_STDAFX_H__940FF418_5647_412A_8B4F_3C89C07F8CA5__INCLUDED_)

 

  1. // SeqList.h: interface for the SeqList class.
  2. //
  3. //
  4. #if !defined(AFX_SEQLIST_H__58D90762_85EC_4BBB_94EA_068A582CCD81__INCLUDED_)
  5. #define AFX_SEQLIST_H__58D90762_85EC_4BBB_94EA_068A582CCD81__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. class SeqList
  10. {
  11. public:
  12. SeqList();
  13. virtual ~SeqList();
  14. bool seqListFull();
  15. bool seqListEmpty();
  16. void randomInsert( elementType number );
  17. void insert( elementType value );
  18. void showLength();
  19. elementType binarySearch( elementType value );
  20. friend ostream &operator<<( ostream &os, SeqList &SL )
  21. {
  22. if( SL.length == -1 )
  23. {
  24. return os;
  25. }
  26. int column = 0;
  27. for( int i = 1; i <= SL.length; i ++ )
  28. {
  29. os << setw(6) << setiosflags( ios::left ) << SL.Arr[i] << " ";
  30. column ++;
  31. if( column % 10 == 0 )
  32. os << endl;
  33. }
  34. os << endl;
  35. }
  36. private:
  37. elementType Arr[maxn];
  38. int length;
  39. };
  40. #endif // !defined(AFX_SEQLIST_H__58D90762_85EC_4BBB_94EA_068A582CCD81__INCLUDED_)

 

  1. // SeqList.cpp: implementation of the SeqList class.
  2. //
  3. //
  4. #include "stdafx.h"
  5. #include "SeqList.h"
  6. //
  7. // Construction/Destruction
  8. //
  9. SeqList::SeqList()
  10. {
  11. length = 0;
  12. }
  13. SeqList::~SeqList()
  14. {
  15. ios::sync_with_stdio(false);
  16. cout << "The SeqList destruction has been called!" << endl;
  17. }
  18. bool SeqList::seqListFull()
  19. {
  20. return length == maxn - 1;
  21. }
  22. bool SeqList::seqListEmpty()
  23. {
  24. return length == 0;
  25. }
  26. void SeqList::randomInsert( elementType number )
  27. {
  28. ios::sync_with_stdio(false);
  29. if( seqListFull() )
  30. {
  31. cerr << "Inserting failed!The sequence list has been full.Error in void SeqList::randomInsert( int number )" << endl;
  32. return;
  33. }
  34. srand( time(NULL) );
  35. elementType last = -999;
  36. for( int i = 0; i < number; i ++ )
  37. {
  38. elementType key = rand() % ( 10000 - 100 + 1 ) + 100;
  39. if( key >= last )
  40. {
  41. length ++;
  42. Arr[length] = key;
  43. last = key;
  44. }
  45. else
  46. {
  47. i --;
  48. }
  49. }
  50. }
  51. void SeqList::insert( elementType value )
  52. {
  53. ios::sync_with_stdio(false);
  54. if( seqListFull() )
  55. {
  56. cerr << "Inerting failed!The sequence list has been full.Error in void SeqList::insert( elementType value )" << endl;
  57. return;
  58. }
  59. length ++;
  60. Arr[length] = value;
  61. }
  62. elementType SeqList::binarySearch( elementType value )
  63. {
  64. ios::sync_with_stdio(false);
  65. if( seqListEmpty() )
  66. {
  67. cerr << "Searching failed!The sequence list is empty.Error in elementType SeqList::binarySearch( elementType value )" << endl;
  68. return -1;
  69. }
  70. elementType lower = 0, upper = length;
  71. while( lower <= upper )
  72. {
  73. elementType mid = ( lower + upper ) >> 1; //+ 1;
  74. if( Arr[mid] == value )
  75. {
  76. return mid;
  77. }
  78. if( Arr[mid] >= value )
  79. {
  80. upper = mid - 1;
  81. }
  82. else
  83. {
  84. lower = mid + 1;
  85. }
  86. }
  87. return -1;
  88. }
  89. void SeqList::showLength()
  90. {
  91. ios::sync_with_stdio(false);
  92. cout << length << endl;
  93. }

 

  1. // BinarySearch.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "SeqList.h"
  5. void test1()
  6. {
  7. ios::sync_with_stdio(false);
  8. SeqList SL1;
  9. elementType number;
  10. cin >> number;
  11. SL1.randomInsert( number );
  12. cout << SL1;
  13. elementType value;
  14. for( int i = 0; i < number; i ++ )
  15. {
  16. cin >> value;
  17. if( SL1.binarySearch(value) != -1 )
  18. {
  19. cout << value << " is in the sequence list." << endl;
  20. }
  21. else
  22. {
  23. cout << value << " is not in the sequence list." << endl;
  24. }
  25. }
  26. }
  27. void test2()
  28. {
  29. ios::sync_with_stdio(false);
  30. SeqList SL1;
  31. elementType value;
  32. while( cin >> value )
  33. {
  34. if( value == -999 )
  35. {
  36. break;
  37. }
  38. SL1.insert(value);
  39. }
  40. SL1.showLength();
  41. cout << SL1;
  42. elementType key;
  43. while( cin >> key )
  44. {
  45. //cin >> key;
  46. if( key == -99 )
  47. {
  48. //break;
  49. return;
  50. }
  51. if( SL1.binarySearch(key) != -1 )
  52. {
  53. cout << key << " is in the sequence list." << endl;
  54. }
  55. else
  56. {
  57. cout << key << " is not in the sequence list." << endl;
  58. }
  59. }
  60. }
  61. int main(int argc, char* argv[])
  62. {
  63. test2();
  64. return 0;
  65. }

 二叉查找(排序)树:

  1. // stdafx.h : include file for standard system include files,
  2. // or project specific include files that are used frequently, but
  3. // are changed infrequently
  4. //
  5. #if !defined(AFX_STDAFX_H__239FA301_F6C5_4AE4_BD82_5EB3365C7ECB__INCLUDED_)
  6. #define AFX_STDAFX_H__239FA301_F6C5_4AE4_BD82_5EB3365C7ECB__INCLUDED_
  7. #if _MSC_VER > 1000
  8. #pragma once
  9. #endif // _MSC_VER > 1000
  10. #include <stdc++.h>
  11. #include <graphics.h>
  12. using namespace std;
  13. //将 elementType 设为 int 可以顺利完成实验要求;而将其设为 string 则可以输入字符串等,
  14. //此时的大小关系默认为为字典序
  15. //typedef string elementType;
  16. typedef int elementType;
  17. //为了图好玩,调用EasyX库把字体颜色改了一下
  18. //这是EasyX的官方网站: https://www.easyx.cn/
  19. typedef struct node
  20. {
  21. elementType data;
  22. struct node *leftChidld, *rightChild;
  23. }BSTNode, *_BSTree;
  24. // TODO: reference additional headers your program requires here
  25. //{{AFX_INSERT_LOCATION}}
  26. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  27. #endif // !defined(AFX_STDAFX_H__239FA301_F6C5_4AE4_BD82_5EB3365C7ECB__INCLUDED_)
  1. // BSTree.h: interface for the BSTree class.
  2. //
  3. //
  4. #if !defined(AFX_BSTREE_H__37E371A7_E165_4AC3_898B_DDF38B0F87D8__INCLUDED_)
  5. #define AFX_BSTREE_H__37E371A7_E165_4AC3_898B_DDF38B0F87D8__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. class BSTree
  10. {
  11. public:
  12. BSTree();
  13. virtual ~BSTree();
  14. BSTNode *search( _BSTree BST, elementType value );//递归查找
  15. BSTNode *search( _BSTree BST, elementType value, _BSTree &father );//迭代查找
  16. BSTNode *getRootNode();
  17. bool insert( _BSTree BST, elementType value );
  18. bool deleteNode1( _BSTree &BST, elementType value ); //删除指定结点,failed
  19. void deleteNode2( _BSTree &BST, elementType value ); //递归删除指定结点
  20. void deleteNode2_1( _BSTree &BST, elementType value ); //迭代删除指定结点,待调试!
  21. void deleteNode3( _BSTree &BST, elementType value );
  22. void removeNode1( _BSTree &BST );
  23. void removeNode2( _BSTree &BST );
  24. void removeNode3( _BSTree &BST );
  25. void createBinarySearchTree( _BSTree BST, vector<elementType>VI/*elementType value*/ );
  26. void destroy( _BSTree BST );
  27. void preOrderTraversal( _BSTree BST/*, int space*/ );
  28. void inOrderTraversal( _BSTree BST/*, int space*/ );
  29. void postOrderTraversal( _BSTree BST/*, int space*/ );
  30. private:
  31. BSTNode *head;
  32. };
  33. #endif // !defined(AFX_BSTREE_H__37E371A7_E165_4AC3_898B_DDF38B0F87D8__INCLUDED_)

 

  1. // BSTree.cpp: implementation of the BSTree class.
  2. //
  3. //
  4. #include "stdafx.h"
  5. #include "BSTree.h"
  6. //
  7. // Construction/Destruction
  8. //
  9. BSTree::BSTree()
  10. {
  11. //head = NULL;
  12. //head = new BSTNode;
  13. //head->leftChidld = head->rightChild = NULL;
  14. }
  15. BSTree::~BSTree()
  16. {
  17. ios::sync_with_stdio(false);
  18. destroy(head);
  19. cout << "The binary search tree has been destroyed!" << endl;
  20. }
  21. void BSTree::destroy( _BSTree BST )
  22. {
  23. if(BST)
  24. {
  25. destroy( BST->leftChidld );
  26. destroy( BST->rightChild );
  27. delete BST;
  28. }
  29. }
  30. void BSTree::preOrderTraversal( _BSTree BST/*, int space*/ )
  31. {
  32. ios::sync_with_stdio(false);
  33. /*
  34. if(!BST)
  35. {
  36. cerr << "The binary search tree is empty.Error in void BSTree::preOrderTraversal( _BSTree BST )." << endl;
  37. return;
  38. }
  39. */
  40. if(BST)
  41. {
  42. cout << BST->data << " ";
  43. preOrderTraversal( BST->leftChidld );
  44. preOrderTraversal( BST->rightChild );
  45. //for( int i = 0; i < space; i ++ )
  46. // cout << " ";
  47. //cout << BST->data << endl;
  48. //preOrderTraversal( BST->leftChidld, space + 5 );
  49. //preOrderTraversal( BST->rightChild, space + 5 );
  50. }
  51. }
  52. void BSTree::inOrderTraversal( _BSTree BST/*, int space*/ )
  53. {
  54. ios::sync_with_stdio(false);
  55. if(BST)
  56. {
  57. inOrderTraversal( BST->leftChidld );
  58. cout << BST->data << " ";
  59. inOrderTraversal( BST->rightChild );
  60. //inOrderTraversal( BST->leftChidld, space + 5 );
  61. //for( int i = 0; i < space; i ++ )
  62. // cout << " ";
  63. //cout << BST->data << endl;
  64. //inOrderTraversal( BST->rightChild, space + 5 );
  65. }
  66. }
  67. void BSTree::postOrderTraversal( _BSTree BST/*, int space*/ )
  68. {
  69. ios::sync_with_stdio(false);
  70. if(BST)
  71. {
  72. postOrderTraversal( BST->leftChidld );
  73. postOrderTraversal( BST->rightChild );
  74. cout << BST->data << " ";
  75. /*
  76. postOrderTraversal( BST->leftChidld, space + 5 );
  77. postOrderTraversal( BST->rightChild, space + 5 );
  78. for( int i = 0; i < space; i ++ )
  79. cout << " ";
  80. cout << BST->data << endl;
  81. */
  82. }
  83. }
  84. void BSTree::createBinarySearchTree( _BSTree BST, /*elementType value*/vector<elementType>VI )
  85. {
  86. //BST = NULL;
  87. head = NULL;
  88. for( int i = 0; i < VI.size(); i ++ )
  89. {
  90. insert( head, VI[i] );
  91. }
  92. return;
  93. /*
  94. while( cin >> value )
  95. {
  96. if( value == "#" )
  97. {
  98. return;
  99. }
  100. else
  101. insert( head, value );
  102. }
  103. for( int i = 0; i < value; i ++ )
  104. {
  105. elementType key;
  106. cout << "input: ";
  107. cin >> key;
  108. insert( head, key );
  109. }
  110. */
  111. }
  112. BSTNode *BSTree::getRootNode()
  113. {
  114. return head;
  115. }
  116. BSTNode *BSTree::search( _BSTree BST, elementType value )//递归查找
  117. {
  118. ios::sync_with_stdio(false);
  119. if(!head)
  120. {
  121. cerr << "The binary search tree is empty.Error in BSTNode *BSTree::search( _BSTree BST, elementType value )." << endl;
  122. return NULL;
  123. }
  124. else if( BST->data == value )
  125. {
  126. return BST;
  127. }
  128. else if( BST->data > value )
  129. {
  130. return search( BST->leftChidld, value );
  131. }
  132. else
  133. {
  134. return search( BST->rightChild, value );
  135. }
  136. }
  137. BSTNode *BSTree::search( _BSTree BST, elementType value, _BSTree &father )//迭代查找
  138. {
  139. ios::sync_with_stdio(false);
  140. /*
  141. if(!head)
  142. {
  143. cerr << "The binary search tree empty.Error in BSTNode *BSTree::search( _BSTree BST, elementType value, _BSTree &father )." << endl;
  144. return NULL;
  145. }
  146. */
  147. BSTNode *tmp = head;
  148. father = NULL;
  149. while( tmp && tmp->data != value )
  150. {
  151. father = tmp;
  152. if( value < tmp->data )
  153. {
  154. tmp = tmp->leftChidld;
  155. }
  156. else
  157. {
  158. tmp = tmp->rightChild;
  159. }
  160. }
  161. return tmp;
  162. }
  163. bool BSTree::insert( _BSTree BST, elementType value )
  164. {
  165. //if(!head)
  166. //{
  167. // cerr << "The binary search tree does not exit.Error in bool BSTree::insert( _BSTree BST, elementType value )" << endl;
  168. // return false;
  169. //}
  170. BSTNode *newNode, *target, *father;
  171. target = search( head, value, father );
  172. if(target)
  173. {
  174. cerr << "Inserting failed!" << value << " has been exited in the binary search tree.\nError in bool BSTree::insert( _BSTree BST, elementType value )" << endl;
  175. return false;
  176. }
  177. newNode = new BSTNode;
  178. newNode->data = value;
  179. newNode->leftChidld = newNode->rightChild = NULL;
  180. if(!head)
  181. {
  182. head = newNode;
  183. }
  184. else if( value < father->data )
  185. {
  186. father->leftChidld = newNode;
  187. }
  188. else
  189. {
  190. father->rightChild = newNode;
  191. }
  192. return true;
  193. }
  194. bool BSTree::deleteNode1( _BSTree &BST, elementType value )
  195. {
  196. ios::sync_with_stdio(false);
  197. //if(!head)
  198. if(!BST)
  199. {
  200. cerr << "The binary search tree does not exit.Error in bool BSTree::deleteNode( _BSTree BST, elementType value )" << endl;
  201. return false;
  202. }
  203. BSTNode *newNode, *target, *father;
  204. //target = search( head, value, father );
  205. target = search( BST, value, father );
  206. if( !target )//查找失败,不删除
  207. {
  208. cerr << "Node-deleting failed!\n" << value << " is not in the binary search tree.\n" << "Error in bool BSTree::deleteNode( _BSTree BST, elementType value )." << endl;
  209. return false;
  210. }
  211. if( target->leftChidld && target->rightChild )//被删结点有两个 *target 孩子节点
  212. {
  213. newNode = target->rightChild; //找 target 的中序后继 newNode
  214. father = target;
  215. while( newNode->leftChidld )
  216. {
  217. father = newNode;
  218. newNode = newNode->leftChidld;
  219. }
  220. target->data = newNode->data; //将 *newNode 的数据传給 *target
  221. target = newNode; //找到的这个结点成为被删除结点
  222. }
  223. if( target->leftChidld ) //单孩子,记录非空孩子结点
  224. {
  225. newNode = target->leftChidld;
  226. }
  227. else
  228. {
  229. newNode = target->rightChild;
  230. }
  231. //if( target == head ) //被删结点是根结点
  232. if( target == BST )
  233. {
  234. //head = newNode;
  235. BST = newNode;
  236. }
  237. else if( newNode && ( newNode->data < father->data ) ) //重新链接,保持二叉排序树
  238. {
  239. father->leftChidld = newNode;
  240. }
  241. else
  242. {
  243. father->rightChild = newNode;
  244. }
  245. delete target;
  246. return true;
  247. }
  248. void BSTree::deleteNode2( _BSTree &BST, elementType value )
  249. {
  250. if(BST)
  251. {
  252. if( value < BST->data )
  253. {
  254. deleteNode2( BST->leftChidld, value );
  255. }
  256. else if( value > BST->data )
  257. {
  258. deleteNode2( BST->rightChild, value );
  259. }
  260. else
  261. {
  262. removeNode1(BST);
  263. }
  264. }
  265. }
  266. void BSTree::deleteNode2_1( _BSTree &BST, elementType value ) //迭代删除指定结点,待调试!
  267. {
  268. BSTNode *target = NULL;
  269. while( BST || BST->data != value )
  270. {
  271. target = BST;
  272. if( value < target->data )
  273. //if( value < BST->data )
  274. BST = BST->leftChidld;
  275. else
  276. BST = BST->rightChild;
  277. }
  278. removeNode1(target);
  279. //removeNode1(BST);
  280. }
  281. void BSTree::deleteNode3( _BSTree &BST, elementType value )
  282. {
  283. if(BST)
  284. {
  285. if( value < BST->data )
  286. {
  287. deleteNode2( BST->leftChidld, value );
  288. }
  289. else if( value > BST->data )
  290. {
  291. deleteNode2( BST->rightChild, value );
  292. }
  293. else
  294. {
  295. removeNode2(BST);
  296. }
  297. }
  298. }
  299. /*
  300. 在二叉查找树中删除一个给定的结点p有三种情况
  301. (1)结点p无左右子树,则直接删除该结点,修改父节点相应指针
  302. (2)结点p有左子树(右子树),则把p的左子树(右子树)接到p的父节点上
  303. (3)左右子树同时存在,则有三种处理方式
  304. a.找到结点p的中序直接前驱结点s,把结点s的数据转移到结点p,然后删除结点s,
  305. 由于结点s为p的左子树中最右的结点,因而s无右子树,删除结点s可以归结到情况(2)。
  306. 严蔚敏数据结构P230-231就是该处理方式。
  307. b.找到结点p的中序直接后继结点s,把结点s的数据转移到结点p,然后删除结点s,
  308. 由于结点s为p的右子树总最左的结点,因而s无左子树,删除结点s可以归结到情况(2)。
  309. 算法导论第2版P156-157该是该处理方式。
  310. c.到p的中序直接前驱s,将p的左子树接到父节点上,将p的右子树接到s的右子树上,然后删除结点p。
  311. */
  312. void BSTree::removeNode1( _BSTree &BST )
  313. {
  314. BSTNode *target = NULL;
  315. if( !BST->leftChidld )
  316. {
  317. target = BST;
  318. BST = BST->rightChild;
  319. delete target;
  320. }
  321. else if( !BST->rightChild )
  322. {
  323. target = BST;
  324. BST = BST->leftChidld;
  325. delete target;
  326. }
  327. else
  328. {
  329. BSTNode *newNode = NULL;
  330. target = BST;
  331. newNode = BST->leftChidld; //左子树根结点
  332. while( newNode->rightChild ) //寻找 BST 结点的中序前驱结点,即以 BST->leftChild为根结点的子树中的最右结点
  333. {
  334. target = newNode; //*target 指向 *BST 的父结点
  335. newNode = newNode->rightChild; //*newNode 指向 *BST 的中序前驱结点
  336. }
  337. BST->data = newNode->data; //*newNode 的数据传給 *BST 的数据,然后删除结点 *newNode
  338. if( target != BST ) //BST->leftChidld 的右子树非空,这句话等价于 if( !( target == BST ) )
  339. {
  340. target->rightChild = newNode->leftChidld; //*newNode 的左子树接到 *target 的右子树上
  341. }
  342. else
  343. {
  344. target->leftChidld = newNode->leftChidld; //*newNode 的左子树接到 *target 的左子树上
  345. }
  346. delete newNode; //删除结点 *newNode
  347. }
  348. }
  349. //注意 while 循环体:
  350. //如果 BST 左子树为空,则 while 循环体不执行,那么 target 就不会发生改变。
  351. //然而一开始 target == BST。
  352. //反过来说,如果 BST 左子树不为空,则 while 执行,那么 target 就会发生改变。
  353. //target 改变了,就和 BST 不一样。
  354. //所以就可以表明 BST 左子树非空。
  355. void BSTree::removeNode2( _BSTree &BST )
  356. {
  357. BSTNode *target = NULL;
  358. if( !BST->leftChidld )
  359. {
  360. target = BST;
  361. BST = BST->rightChild;
  362. delete target;
  363. }
  364. else if( !BST->rightChild )
  365. {
  366. target = BST;
  367. BST = BST->leftChidld;
  368. delete target;
  369. }
  370. else
  371. {
  372. BSTNode *newNode = NULL;
  373. target = BST;
  374. newNode = BST->rightChild; //右子树根结点
  375. while( newNode->leftChidld ) //寻找 BST 结点的中序前驱结点,即以 BST->leftChild为根结点的子树中的最左结点
  376. {
  377. target = newNode; //*target 指向 *BST 的父结点
  378. newNode = newNode->leftChidld; //*newNode 指向 *BST 的中序后继结点
  379. }
  380. BST->data = newNode->data; //*newNode 的数据传給 *BST 的数据,然后删除结点 *newNode
  381. if( target != BST ) //BST->leftChidld 的左子树非空,这句话等价于 if( !( target == BST ) )
  382. {
  383. target->leftChidld = newNode->rightChild; //*newNode 的右子树接到 *target 的左子树上
  384. }
  385. else
  386. {
  387. target->rightChild = newNode->rightChild; //*newNode 的右子树接到 *target 的右子树上
  388. }
  389. delete newNode; //删除结点 *newNode
  390. }
  391. }
  392. //注意 while 循环体:
  393. //如果 BST 右子树为空,则 while 循环体不执行,那么 target 就不会发生改变。
  394. //然而一开始 target == BST。
  395. //反过来说,如果 BST 右子树不为空,则 while 执行,那么 target 就会发生改变。
  396. //target 改变了,就和 BST 不一样
  397. //所以就可以表明 BST 右子树非空。
  398. void BSTree::removeNode3( _BSTree &BST )
  399. {
  400. BSTNode *target = NULL;
  401. if( !BST->leftChidld )
  402. {
  403. target = BST;
  404. BST = BST->rightChild;
  405. delete target;
  406. }
  407. else if( !BST->rightChild )
  408. {
  409. target = BST;
  410. BST = BST->leftChidld;
  411. delete target;
  412. }
  413. else
  414. {
  415. BSTNode *newNode = NULL;
  416. target = BST;
  417. newNode = BST->leftChidld; //左子树根结点
  418. while( newNode->rightChild ) //寻找 BST 结点的中序前驱结点,即以 BST->leftChild为根结点的子树中的最右结点
  419. {
  420. //target = newNode;
  421. newNode = newNode->rightChild;
  422. }
  423. newNode->rightChild = target->leftChidld; //*target 的左子树接到 *newNode 的左子树上
  424. target = target->leftChidld; //*target 的左子树接到父结点上
  425. delete target; //删除结点 *target
  426. }
  427. }
  1. // BinarySearchTree.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "BSTree.h"
  5. //这是EasyX的官方网站: https://www.easyx.cn/
  6. void test1()
  7. {
  8. HANDLE hOut;
  9. // 获取输出流的句柄
  10. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  11. BSTree BST1;
  12. elementType value;
  13. vector<elementType>VI;
  14. while( cin >> value )
  15. {
  16. if( (char)value == '#' && value != 35/*-999*/ ) //细节处理:一定要加 && value != 35,因为 # 的ASCII码是35,
  17. { //不加的话在输入数字“35”而不是“#”时循环也会终止
  18. break;
  19. }
  20. else
  21. {
  22. VI.push_back(value);
  23. }
  24. }
  25. BST1.createBinarySearchTree( BST1.getRootNode(), VI );
  26. SetConsoleTextAttribute(hOut,
  27. FOREGROUND_RED | // 前景色_红色
  28. FOREGROUND_BLUE |// 前景色_蓝色
  29. FOREGROUND_INTENSITY);// 加强
  30. cout << "PreOrder:" << endl;
  31. BST1.preOrderTraversal( BST1.getRootNode() );
  32. cout << endl;
  33. cout << "InOrder:" << endl;
  34. BST1.inOrderTraversal( BST1.getRootNode() );
  35. cout << endl;
  36. cout << "PostOrder:" << endl;
  37. BST1.postOrderTraversal( BST1.getRootNode() );
  38. cout << endl;
  39. SetConsoleTextAttribute(hOut,
  40. FOREGROUND_RED | // 前景色_红色
  41. FOREGROUND_GREEN | // 前景色_绿色
  42. FOREGROUND_BLUE ); // 前景色_蓝色
  43. return;
  44. }
  45. void test2()
  46. {
  47. HANDLE hOut;
  48. // 获取输出流的句柄
  49. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  50. BSTree BST1;
  51. elementType value;
  52. vector<elementType>VI;
  53. while( cin >> value )
  54. {
  55. if( (char)value == '#' && value != 35/*-999*/ ) //细节处理:一定要加 && value != 35,因为 # 的ASCII码是35,
  56. { //不加的话在输入数字“35”而不是“#”时循环也会终止
  57. break;
  58. }
  59. else
  60. {
  61. VI.push_back(value);
  62. }
  63. }
  64. BST1.createBinarySearchTree( BST1.getRootNode(), VI );
  65. _BSTree index = NULL;
  66. SetConsoleTextAttribute(hOut,
  67. FOREGROUND_RED | // 前景色_红色
  68. FOREGROUND_BLUE |// 前景色_蓝色
  69. FOREGROUND_INTENSITY);// 加强
  70. cout << "PreOrder:" << endl;
  71. BST1.preOrderTraversal( BST1.getRootNode() );
  72. cout << endl;
  73. cout << "InOrder:" << endl;
  74. BST1.inOrderTraversal( BST1.getRootNode() );
  75. cout << endl;
  76. cout << "PostOrder:" << endl;
  77. BST1.postOrderTraversal( BST1.getRootNode() );
  78. cout << endl;
  79. //elementType key = ;// = 545;
  80. //下面这句话不会运行
  81. //cin >> key;
  82. elementType Arr[] = { 150, 70, 160, 190, 10, 55, 175 };
  83. for( int j = 0; j < sizeof(Arr) / sizeof(elementType); j ++ )
  84. {
  85. if( BST1.search( BST1.getRootNode(), Arr[j], index ) )
  86. {
  87. SetConsoleTextAttribute(hOut,
  88. FOREGROUND_BLUE | // 前景色_蓝色
  89. FOREGROUND_INTENSITY ); // 前景色_加强
  90. cout << Arr[j] << " is in the binary search tree." << endl;
  91. SetConsoleTextAttribute(hOut,
  92. FOREGROUND_RED | // 前景色_红色
  93. FOREGROUND_GREEN | // 前景色_绿色
  94. FOREGROUND_BLUE ); // 前景色_蓝色
  95. }
  96. else
  97. {
  98. SetConsoleTextAttribute(hOut,
  99. FOREGROUND_RED | // 前景色_红色
  100. FOREGROUND_INTENSITY ); // 前景色_加强
  101. cout << Arr[j] << " is not in the binary search tree." << endl;
  102. SetConsoleTextAttribute(hOut,
  103. FOREGROUND_RED | // 前景色_红色
  104. FOREGROUND_GREEN | // 前景色_绿色
  105. FOREGROUND_BLUE ); // 前景色_蓝色
  106. }
  107. }
  108. //无法实现下面这样输入数值判断其是否存在
  109. /*
  110. BSTNode *father = NULL, *target = NULL;
  111. elementType key;
  112. while( cin >> key )
  113. {
  114. //target = NULL;
  115. if( (char)key == '#' && key != 35 )
  116. {
  117. break;
  118. }
  119. else
  120. target = BST1.search( BST1.getRootNode(), key, father );
  121. if(!target)
  122. {
  123. cout << "No!" << endl;
  124. }
  125. else
  126. {
  127. cout << "Yes!" << endl;
  128. }
  129. }
  130. */
  131. return;
  132. }
  133. void test3()
  134. {
  135. HANDLE hOut;
  136. // 获取输出流的句柄
  137. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  138. BSTree BST1;
  139. elementType value;
  140. vector<elementType>VI;
  141. while( cin >> value )
  142. {
  143. if( (char)value == '#' && value != 35/*-999*/ ) //细节处理:一定要加 && value != 35,因为 # 的ASCII码是35,
  144. { //不加的话在输入数字“35”而不是“#”时循环也会终止
  145. break;
  146. }
  147. else
  148. {
  149. VI.push_back(value);
  150. }
  151. }
  152. BST1.createBinarySearchTree( BST1.getRootNode(), VI );
  153. _BSTree index = NULL;
  154. SetConsoleTextAttribute(hOut,
  155. FOREGROUND_BLUE | // 前景色_蓝色
  156. FOREGROUND_INTENSITY ); // 前景色_加强
  157. cout << "The origin binary search tree is as follow:" << endl;
  158. SetConsoleTextAttribute(hOut,
  159. FOREGROUND_RED | // 前景色_红色
  160. FOREGROUND_BLUE |// 前景色_蓝色
  161. FOREGROUND_INTENSITY);// 加强
  162. cout << "PreOrder:" << endl;
  163. BST1.preOrderTraversal( BST1.getRootNode() );
  164. cout << endl;
  165. cout << "InOrder:" << endl;
  166. BST1.inOrderTraversal( BST1.getRootNode() );
  167. cout << endl;
  168. cout << "PostOrder:" << endl;
  169. BST1.postOrderTraversal( BST1.getRootNode() );
  170. cout << endl;
  171. elementType Arr[] = { 30, 150, 100 };
  172. for( int i = 0; i < sizeof(Arr) / sizeof(elementType); i ++ )
  173. {
  174. _BSTree index = BST1.getRootNode();
  175. //BST1.deleteNode1( index, Arr[i] );
  176. BST1.deleteNode2( index, Arr[i] );
  177. SetConsoleTextAttribute(hOut,
  178. FOREGROUND_BLUE | // 前景色_蓝色
  179. FOREGROUND_INTENSITY ); // 前景色_加强
  180. cout << "After deleting node " << Arr[i] << ", the current binary search tree is as follow:"<< endl;
  181. SetConsoleTextAttribute(hOut,
  182. FOREGROUND_RED | // 前景色_红色
  183. FOREGROUND_BLUE |// 前景色_蓝色
  184. FOREGROUND_INTENSITY);// 加强
  185. cout << "PreOrder:" << endl;
  186. BST1.preOrderTraversal( BST1.getRootNode() );
  187. cout << endl;
  188. cout << "InOrder:" << endl;
  189. BST1.inOrderTraversal( BST1.getRootNode() );
  190. cout << endl;
  191. cout << "PostOrder:" << endl;
  192. BST1.postOrderTraversal( BST1.getRootNode() );
  193. cout << endl;
  194. }
  195. SetConsoleTextAttribute(hOut,
  196. FOREGROUND_RED | // 前景色_红色
  197. FOREGROUND_GREEN | // 前景色_绿色
  198. FOREGROUND_BLUE ); // 前景色_蓝色
  199. return;
  200. }
  201. int main(int argc, char* argv[])
  202. {
  203. //test1();
  204. //test2();
  205. test3();
  206. return 0;
  207. }

AVL树:

  1. // Tips for Getting Started:
  2. // 1. Use the Solution Explorer window to add/manage files
  3. // 2. Use the Team Explorer window to connect to source control
  4. // 3. Use the Output window to see build output and other messages
  5. // 4. Use the Error List window to view errors
  6. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  7. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  8. #ifndef PCH_H
  9. #define PCH_H
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <graphics.h>
  13. #include <windows.h>
  14. using namespace std;
  15. // TODO: add headers that you want to pre-compile here
  16. #endif //PCH_H

 

  1. #pragma once
  2. /* AVL node */
  3. template <class T>
  4. class AVLNode
  5. {
  6. public:
  7. T key;
  8. int balance;
  9. AVLNode *leftChild, *rightChild, *parent;
  10. AVLNode(T k, AVLNode *p) : key(k), balance(0), parent(p),leftChild(NULL), rightChild(NULL) {}
  11. ~AVLNode();
  12. };

 

  1. #pragma once
  2. /* AVL tree */
  3. #include "AVLnode.h"
  4. template <class T>
  5. class AVLTree
  6. {
  7. public:
  8. AVLTree(void);
  9. ~AVLTree(void);
  10. bool insert(T key);
  11. void deleteKey(const T key);
  12. void printBalance();
  13. void inOrderTraverse();
  14. void preOrderTraverse();
  15. void postOrderTraverse();
  16. void display();
  17. AVLNode<T>* RR_Rotate(AVLNode<T> *AVLB); //rotate left
  18. //当在RR发生不平衡时需要进行左旋转
  19. AVLNode<T>* LL_Rotate(AVLNode<T> *AVLB); //rotate right
  20. //当在LL发生不平衡时需要进行右旋转
  21. AVLNode<T>* LR_Rotate(AVLNode<T> *AVLB); //rotate left then right
  22. AVLNode<T>* RL_Rotate(AVLNode<T> *AVLB); //rotate right then left
  23. AVLNode<T>* getRootNode();
  24. void reBalance(AVLNode<T> *AVLB);
  25. int height(AVLNode<T> *AVLB);
  26. void setBalance(AVLNode<T> *AVLB);
  27. void printBalance(AVLNode<T> *AVLB);
  28. void clearNode(AVLNode<T> *AVLB);
  29. void inOrderTraverse(AVLNode<T> *AVLB);
  30. void preOrderTraverse(AVLNode<T> *AVLB);
  31. void postOrderTraverse(AVLNode<T> *AVLB);
  32. void display(AVLNode <T>*AVLB, int space, int colour);
  33. private:
  34. AVLNode<T> *root;
  35. };

 

  1. #include "pch.h"
  2. #include "AVLnode.h"
  3. template <class T>
  4. AVLNode<T>::~AVLNode()
  5. {
  6. delete leftChild;
  7. delete rightChild;
  8. }
  1. #include "pch.h"
  2. #include "AVLtree.h"
  3. template <class T>
  4. void AVLTree<T>::reBalance(AVLNode<T> *AVLB)
  5. {
  6. setBalance(AVLB);
  7. if (AVLB->balance == -2)
  8. {
  9. if (height(AVLB->leftChild->leftChild) >= height(AVLB->leftChild->rightChild))
  10. AVLB = LL_Rotate(AVLB);
  11. else
  12. AVLB = LR_Rotate(AVLB);
  13. }
  14. else if (AVLB->balance == 2)
  15. {
  16. if (height(AVLB->rightChild->rightChild) >= height(AVLB->rightChild->leftChild))
  17. AVLB = RR_Rotate(AVLB);
  18. else
  19. AVLB = RL_Rotate(AVLB);
  20. }
  21. if (AVLB->parent != NULL)
  22. {
  23. reBalance(AVLB->parent);
  24. }
  25. else
  26. {
  27. root = AVLB;
  28. }
  29. }
  30. template <class T>
  31. AVLNode<T>* AVLTree<T>::RR_Rotate(AVLNode<T> *AVLB)
  32. {
  33. AVLNode<T> *tmp = AVLB->rightChild;
  34. tmp->parent = AVLB->parent;
  35. AVLB->rightChild = tmp->leftChild;
  36. if (AVLB->rightChild != NULL)
  37. AVLB->rightChild->parent = AVLB;
  38. tmp->leftChild = AVLB;
  39. AVLB->parent = tmp;
  40. if (tmp->parent != NULL)
  41. {
  42. if (tmp->parent->rightChild == AVLB)
  43. {
  44. tmp->parent->rightChild = tmp;
  45. }
  46. else
  47. {
  48. tmp->parent->leftChild = tmp;
  49. }
  50. }
  51. setBalance(AVLB);
  52. setBalance(tmp);
  53. return tmp;
  54. }
  55. template <class T>
  56. AVLNode<T>* AVLTree<T>::LL_Rotate(AVLNode<T> *AVLB)
  57. {
  58. AVLNode<T> *tmp = AVLB->leftChild;
  59. tmp->parent = AVLB->parent;
  60. AVLB->leftChild = tmp->rightChild;
  61. if (AVLB->leftChild != NULL)
  62. AVLB->leftChild->parent = AVLB;
  63. tmp->rightChild = AVLB;
  64. AVLB->parent = tmp;
  65. if (tmp->parent != NULL)
  66. {
  67. if (tmp->parent->rightChild == AVLB)
  68. {
  69. tmp->parent->rightChild = tmp;
  70. }
  71. else
  72. {
  73. tmp->parent->leftChild = tmp;
  74. }
  75. }
  76. setBalance(AVLB);
  77. setBalance(tmp);
  78. return tmp;
  79. }
  80. template <class T>
  81. AVLNode<T>* AVLTree<T>::LR_Rotate(AVLNode<T> *AVLB)
  82. {
  83. AVLB->leftChild = RR_Rotate(AVLB->leftChild);
  84. return LL_Rotate(AVLB);
  85. }
  86. template <class T>
  87. AVLNode<T>* AVLTree<T>::RL_Rotate(AVLNode<T> *AVLB)
  88. {
  89. AVLB->rightChild = LL_Rotate(AVLB->rightChild);
  90. return RR_Rotate(AVLB);
  91. }
  92. template <class T>
  93. AVLNode<T>* AVLTree<T>::getRootNode()
  94. {
  95. return root;
  96. }
  97. template <class T>
  98. int AVLTree<T>::height(AVLNode<T> *AVLB)
  99. {
  100. if (AVLB == NULL)
  101. return -1;
  102. return 1 + max(height(AVLB->leftChild), height(AVLB->rightChild));
  103. }
  104. template <class T>
  105. void AVLTree<T>::setBalance(AVLNode<T> *AVLB)
  106. {
  107. AVLB->balance = height(AVLB->rightChild) - height(AVLB->leftChild);
  108. }
  109. template <class T>
  110. void AVLTree<T>::printBalance(AVLNode<T> *AVLB)
  111. {
  112. ios::sync_with_stdio(false);
  113. if (AVLB != NULL)
  114. {
  115. printBalance(AVLB->leftChild);
  116. cout << AVLB->balance << " ";
  117. //std::cout << n->key << " ";
  118. printBalance(AVLB->rightChild);
  119. }
  120. }
  121. template <class T>
  122. void AVLTree<T>::inOrderTraverse(AVLNode<T> *AVLB)
  123. {
  124. ios::sync_with_stdio(false);
  125. if (AVLB)
  126. {
  127. inOrderTraverse(AVLB->leftChild);
  128. cout << AVLB->key << " ";
  129. inOrderTraverse(AVLB->rightChild);
  130. }
  131. }
  132. template <class T>
  133. void AVLTree<T>::preOrderTraverse(AVLNode<T> *AVLB)
  134. {
  135. if (AVLB)
  136. {
  137. cout << AVLB->key << " ";
  138. preOrderTraverse(AVLB->leftChild);
  139. preOrderTraverse(AVLB->rightChild);
  140. }
  141. }
  142. template <class T>
  143. void AVLTree<T>::postOrderTraverse(AVLNode<T> *AVLB)
  144. {
  145. ios::sync_with_stdio(false);
  146. if (AVLB)
  147. {
  148. postOrderTraverse(AVLB->leftChild);
  149. postOrderTraverse(AVLB->rightChild);
  150. cout << AVLB->key << " ";
  151. }
  152. }
  153. template <class T>
  154. void AVLTree<T>::display(AVLNode <T>*AVLB, int space, int colour )
  155. {
  156. ios::sync_with_stdio(false);
  157. HANDLE hConsole;
  158. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  159. if (AVLB)
  160. {
  161. display(AVLB->rightChild, space + 1, colour + 1);
  162. SetConsoleTextAttribute(hConsole, 0x0008 | colour);
  163. //colour++;
  164. cout << endl;
  165. if (AVLB == root)
  166. cout << " Root ----> ";
  167. for (int i = 0; i < space && AVLB != root; i++)
  168. cout << " ";
  169. cout << AVLB->key;
  170. display(AVLB->leftChild, space + 1, colour + 1);
  171. }
  172. }
  173. template <class T>
  174. AVLTree<T>::AVLTree(void) : root(NULL) {}
  175. template <class T>
  176. AVLTree<T>::~AVLTree(void)
  177. {
  178. delete root;
  179. }
  180. template <class T>
  181. bool AVLTree<T>::insert(T key)
  182. {
  183. if (root == NULL)
  184. {
  185. root = new AVLNode<T>(key, NULL);
  186. }
  187. else
  188. {
  189. AVLNode<T> //这种风格我觉得不错
  190. //I appreciate this style of code
  191. *target = root,
  192. *parent;
  193. while (1)
  194. {
  195. if (target->key == key)
  196. return false;
  197. parent = target;
  198. bool goLeft = target->key > key;
  199. target = goLeft ? target->leftChild : target->rightChild;
  200. if (target == NULL)
  201. {
  202. if (goLeft)
  203. {
  204. parent->leftChild = new AVLNode<T>(key, parent);
  205. }
  206. else
  207. {
  208. parent->rightChild = new AVLNode<T>(key, parent);
  209. }
  210. reBalance(parent);
  211. break;
  212. }
  213. }
  214. }
  215. return true;
  216. }
  217. template <class T>
  218. void AVLTree<T>::deleteKey(const T delKey)
  219. {
  220. if (root == NULL)
  221. return;
  222. AVLNode<T>
  223. *target = root,
  224. *parent = root,
  225. *delNode = NULL,
  226. *child = root;
  227. while (child != NULL)
  228. {
  229. parent = target;
  230. target = child;
  231. child = delKey >= target->key ? target->rightChild : target->leftChild;
  232. if (delKey == target->key)
  233. delNode = target;
  234. }
  235. if (delNode != NULL)
  236. {
  237. delNode->key = target->key;
  238. child = target->leftChild != NULL ? target->leftChild : target->rightChild;
  239. if (root->key == delKey)
  240. {
  241. root = child;
  242. }
  243. else
  244. {
  245. if (parent->leftChild == target)
  246. {
  247. parent->leftChild = child;
  248. }
  249. else
  250. {
  251. parent->rightChild = child;
  252. }
  253. reBalance(parent);
  254. }
  255. }
  256. }
  257. template <class T>
  258. void AVLTree<T>::printBalance()
  259. {
  260. ios::sync_with_stdio(false);
  261. printBalance(root);
  262. cout << endl;
  263. }
  264. template <class T>
  265. void AVLTree<T>::inOrderTraverse()
  266. {
  267. ios::sync_with_stdio(false);
  268. inOrderTraverse(root);
  269. cout << endl;
  270. }
  271. template <class T>
  272. void AVLTree<T>::preOrderTraverse()
  273. {
  274. ios::sync_with_stdio(false);
  275. preOrderTraverse(root);
  276. cout << endl;
  277. }
  278. template <class T>
  279. void AVLTree<T>::postOrderTraverse()
  280. {
  281. ios::sync_with_stdio(false);
  282. postOrderTraverse(root);
  283. cout << endl;
  284. }
  285. template <class T>
  286. void AVLTree<T>::display()
  287. {
  288. ios::sync_with_stdio(false);
  289. int color = 1;
  290. display(root, 1, color);
  291. cout << endl;
  292. }

 

  1. // AVL_2.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. #include "pch.h"
  4. #include <iostream>
  5. #include "AVLtree.h"
  6. #include "AVLnode.h"
  7. #include "AVLnode.cpp"
  8. #include "AVLtree.cpp"
  9. int main()
  10. {
  11. //std::cout << "Hello World!\n";
  12. ios::sync_with_stdio(false);
  13. AVLTree<int> AVLBT;
  14. HANDLE hConsole;
  15. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  16. cout << "Inserting integer values 1 to 26" << std::endl;
  17. int Arr[] = { 1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190,210,231,
  18. 253,277,302,328 };
  19. for (int i = 0; i < sizeof(Arr) / sizeof(int); i++)
  20. //for( int i = 0; Arr[i] != 0; i ++ )
  21. //AVLBT.insert(i);
  22. AVLBT.insert(Arr[i]);
  23. cout << "Printing the balance factor of each node: " << std::endl;
  24. SetConsoleTextAttribute(hConsole, 12);
  25. AVLBT.printBalance();
  26. SetConsoleTextAttribute(hConsole, 7);
  27. cout << "Printing key: " << std::endl;
  28. SetConsoleTextAttribute(hConsole, 0x0008 | 8);
  29. AVLBT.inOrderTraverse();
  30. AVLBT.display();
  31. //AVLTree<int> *root = avl.getRootNode();
  32. while (1)
  33. {
  34. SetConsoleTextAttribute(hConsole, 7);
  35. cout << "\n---------------------" << endl;
  36. cout << "AVL tree implementation" << endl;
  37. cout << "By Utah Xef developed" << endl;
  38. cout << "\n---------------------" << endl;
  39. cout << "1.insert element into the tree" << endl;
  40. cout << "2.display balanced AVL tree" << endl;
  41. cout << "3.preorder traversal" << endl;
  42. cout << "4.inorder traversal" << endl;
  43. cout << "5.postorder traversal" << endl;
  44. cout << "6.delete key" << endl;
  45. cout << "7.display the balance factor of each node" << endl;
  46. cout << "8.exit" << endl;
  47. cout << "enter your choice: ";
  48. int choice;
  49. cin >> choice;
  50. switch (choice)
  51. {
  52. case 1:
  53. cout << "enter value to be inserted: ";
  54. int item;
  55. cin >> item;
  56. AVLBT.insert(item);
  57. break;
  58. case 2:
  59. if (AVLBT.getRootNode() == nullptr)
  60. {
  61. cout << "tree is empty" << endl;
  62. continue;
  63. }
  64. cout << "balanced avl tree:" << endl;
  65. AVLBT.display();
  66. break;
  67. case 3:
  68. cout << "preorder traversal:" << endl;
  69. SetConsoleTextAttribute(hConsole, 0x0008 | 9);
  70. AVLBT.preOrderTraverse();
  71. cout << endl;
  72. break;
  73. case 4:
  74. cout << "inorder traversal:" << endl;
  75. SetConsoleTextAttribute(hConsole, 0x0008 | 10);
  76. AVLBT.inOrderTraverse();
  77. cout << endl;
  78. break;
  79. case 5:
  80. cout << "postorder traversal:" << endl;
  81. SetConsoleTextAttribute(hConsole, 0x0008 | 11);
  82. AVLBT.postOrderTraverse();
  83. cout << endl;
  84. break;
  85. case 6:
  86. int value;
  87. cout << "Please input the value to delete:" << endl;
  88. cin >> value;
  89. AVLBT.deleteKey(value);
  90. break;
  91. case 7:
  92. cout << "The balance factor of each node:" << endl;
  93. SetConsoleTextAttribute(hConsole, 0x0008 | 14);
  94. AVLBT.printBalance();
  95. break;
  96. case 8:
  97. exit(1);
  98. break;
  99. default:
  100. cout << "Wrong choice" << endl;
  101. break;
  102. }
  103. }
  104. //std::cout << std::endl;
  105. std::cin.get();
  106. }
  107. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  108. // Debug program: F5 or Debug > Start Debugging menu
  109. // Tips for Getting Started:
  110. // 1. Use the Solution Explorer window to add/manage files
  111. // 2. Use the Team Explorer window to connect to source control
  112. // 3. Use the Output window to see build output and other messages
  113. // 4. Use the Error List window to view errors
  114. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  115. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

 

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

闽ICP备14008679号