实验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.  
  6. #if !defined(AFX_STDAFX_H__940FF418_5647_412A_8B4F_3C89C07F8CA5__INCLUDED_)
  7. #define AFX_STDAFX_H__940FF418_5647_412A_8B4F_3C89C07F8CA5__INCLUDED_
  8.  
  9. #if _MSC_VER > 1000
  10. #pragma once
  11. #endif // _MSC_VER > 1000
  12.  
  13. #include <stdc++.h>
  14.  
  15. using namespace std;
  16.  
  17. typedef long elementType;
  18.  
  19. const long maxn = + ;
  20.  
  21. // TODO: reference additional headers your program requires here
  22.  
  23. //{{AFX_INSERT_LOCATION}}
  24. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  25.  
  26. #endif // !defined(AFX_STDAFX_H__940FF418_5647_412A_8B4F_3C89C07F8CA5__INCLUDED_)
  1. // SeqList.h: interface for the SeqList class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #if !defined(AFX_SEQLIST_H__58D90762_85EC_4BBB_94EA_068A582CCD81__INCLUDED_)
  6. #define AFX_SEQLIST_H__58D90762_85EC_4BBB_94EA_068A582CCD81__INCLUDED_
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11.  
  12. class SeqList
  13. {
  14. public:
  15. SeqList();
  16. virtual ~SeqList();
  17. bool seqListFull();
  18. bool seqListEmpty();
  19. void randomInsert( elementType number );
  20. void insert( elementType value );
  21. void showLength();
  22. elementType binarySearch( elementType value );
  23. friend ostream &operator<<( ostream &os, SeqList &SL )
  24. {
  25. if( SL.length == - )
  26. {
  27. return os;
  28. }
  29. int column = ;
  30. for( int i = ; i <= SL.length; i ++ )
  31. {
  32. os << setw() << setiosflags( ios::left ) << SL.Arr[i] << " ";
  33. column ++;
  34. if( column % == )
  35. os << endl;
  36. }
  37. os << endl;
  38. }
  39.  
  40. private:
  41. elementType Arr[maxn];
  42. int length;
  43.  
  44. };
  45.  
  46. #endif // !defined(AFX_SEQLIST_H__58D90762_85EC_4BBB_94EA_068A582CCD81__INCLUDED_)
  1. // SeqList.cpp: implementation of the SeqList class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "stdafx.h"
  6. #include "SeqList.h"
  7.  
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11.  
  12. SeqList::SeqList()
  13. {
  14. length = ;
  15. }
  16.  
  17. SeqList::~SeqList()
  18. {
  19. ios::sync_with_stdio(false);
  20. cout << "The SeqList destruction has been called!" << endl;
  21. }
  22.  
  23. bool SeqList::seqListFull()
  24. {
  25. return length == maxn - ;
  26. }
  27.  
  28. bool SeqList::seqListEmpty()
  29. {
  30. return length == ;
  31. }
  32.  
  33. void SeqList::randomInsert( elementType number )
  34. {
  35. ios::sync_with_stdio(false);
  36. if( seqListFull() )
  37. {
  38. cerr << "Inserting failed!The sequence list has been full.Error in void SeqList::randomInsert( int number )" << endl;
  39. return;
  40. }
  41.  
  42. srand( time(NULL) );
  43. elementType last = -;
  44. for( int i = ; i < number; i ++ )
  45. {
  46. elementType key = rand() % ( - + ) + ;
  47. if( key >= last )
  48. {
  49. length ++;
  50. Arr[length] = key;
  51. last = key;
  52. }
  53. else
  54. {
  55. i --;
  56. }
  57. }
  58. }
  59.  
  60. void SeqList::insert( elementType value )
  61. {
  62. ios::sync_with_stdio(false);
  63. if( seqListFull() )
  64. {
  65. cerr << "Inerting failed!The sequence list has been full.Error in void SeqList::insert( elementType value )" << endl;
  66. return;
  67. }
  68.  
  69. length ++;
  70. Arr[length] = value;
  71. }
  72.  
  73. elementType SeqList::binarySearch( elementType value )
  74. {
  75. ios::sync_with_stdio(false);
  76. if( seqListEmpty() )
  77. {
  78. cerr << "Searching failed!The sequence list is empty.Error in elementType SeqList::binarySearch( elementType value )" << endl;
  79. return -;
  80. }
  81. elementType lower = , upper = length;
  82. while( lower <= upper )
  83. {
  84. elementType mid = ( lower + upper ) >> ; //+ 1;
  85. if( Arr[mid] == value )
  86. {
  87. return mid;
  88. }
  89. if( Arr[mid] >= value )
  90. {
  91. upper = mid - ;
  92. }
  93. else
  94. {
  95. lower = mid + ;
  96. }
  97. }
  98. return -;
  99. }
  100.  
  101. void SeqList::showLength()
  102. {
  103. ios::sync_with_stdio(false);
  104. cout << length << endl;
  105. }
  1. // BinarySearch.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "SeqList.h"
  6.  
  7. void test1()
  8. {
  9. ios::sync_with_stdio(false);
  10. SeqList SL1;
  11. elementType number;
  12. cin >> number;
  13. SL1.randomInsert( number );
  14. cout << SL1;
  15. elementType value;
  16. for( int i = ; i < number; i ++ )
  17. {
  18. cin >> value;
  19. if( SL1.binarySearch(value) != - )
  20. {
  21. cout << value << " is in the sequence list." << endl;
  22. }
  23. else
  24. {
  25. cout << value << " is not in the sequence list." << endl;
  26. }
  27. }
  28. }
  29.  
  30. void test2()
  31. {
  32. ios::sync_with_stdio(false);
  33. SeqList SL1;
  34. elementType value;
  35. while( cin >> value )
  36. {
  37. if( value == - )
  38. {
  39. break;
  40. }
  41. SL1.insert(value);
  42. }
  43. SL1.showLength();
  44. cout << SL1;
  45.  
  46. elementType key;
  47. while( cin >> key )
  48. {
  49. //cin >> key;
  50. if( key == - )
  51. {
  52. //break;
  53. return;
  54. }
  55. if( SL1.binarySearch(key) != - )
  56. {
  57. cout << key << " is in the sequence list." << endl;
  58. }
  59. else
  60. {
  61. cout << key << " is not in the sequence list." << endl;
  62. }
  63. }
  64.  
  65. }
  66.  
  67. int main(int argc, char* argv[])
  68. {
  69. test2();
  70.  
  71. return ;
  72. }

二分查找(排序)树:

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

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.  
  9. #ifndef PCH_H
  10. #define PCH_H
  11.  
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <graphics.h>
  15. #include <windows.h>
  16.  
  17. using namespace std;
  18.  
  19. // TODO: add headers that you want to pre-compile here
  20.  
  21. #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.  
  11. AVLNode(T k, AVLNode *p) : key(k), balance(), parent(p),leftChild(NULL), rightChild(NULL) {}
  12. ~AVLNode();
  13. };
  1. #pragma once
  2. /* AVL tree */
  3.  
  4. #include "AVLnode.h"
  5. template <class T>
  6. class AVLTree
  7. {
  8. public:
  9. AVLTree(void);
  10. ~AVLTree(void);
  11. bool insert(T key);
  12. void deleteKey(const T key);
  13. void printBalance();
  14. void inOrderTraverse();
  15. void preOrderTraverse();
  16. void postOrderTraverse();
  17. void display();
  18.  
  19. AVLNode<T>* RR_Rotate(AVLNode<T> *AVLB); //rotate left
  20. //当在RR发生不平衡时需要进行左旋转
  21. AVLNode<T>* LL_Rotate(AVLNode<T> *AVLB); //rotate right
  22. //当在LL发生不平衡时需要进行右旋转
  23. AVLNode<T>* LR_Rotate(AVLNode<T> *AVLB); //rotate left then right
  24. AVLNode<T>* RL_Rotate(AVLNode<T> *AVLB); //rotate right then left
  25. AVLNode<T>* getRootNode();
  26. void reBalance(AVLNode<T> *AVLB);
  27. int height(AVLNode<T> *AVLB);
  28. void setBalance(AVLNode<T> *AVLB);
  29. void printBalance(AVLNode<T> *AVLB);
  30. void clearNode(AVLNode<T> *AVLB);
  31. void inOrderTraverse(AVLNode<T> *AVLB);
  32. void preOrderTraverse(AVLNode<T> *AVLB);
  33. void postOrderTraverse(AVLNode<T> *AVLB);
  34.  
  35. void display(AVLNode <T>*AVLB, int space, int colour);
  36.  
  37. private:
  38. AVLNode<T> *root;
  39. };
  1. #include "pch.h"
  2. #include "AVLnode.h"
  3.  
  4. template <class T>
  5. AVLNode<T>::~AVLNode()
  6. {
  7. delete leftChild;
  8. delete rightChild;
  9. }
  1. #include "pch.h"
  2. #include "AVLtree.h"
  3.  
  4. template <class T>
  5. void AVLTree<T>::reBalance(AVLNode<T> *AVLB)
  6. {
  7. setBalance(AVLB);
  8.  
  9. if (AVLB->balance == -)
  10. {
  11. if (height(AVLB->leftChild->leftChild) >= height(AVLB->leftChild->rightChild))
  12. AVLB = LL_Rotate(AVLB);
  13. else
  14. AVLB = LR_Rotate(AVLB);
  15. }
  16. else if (AVLB->balance == )
  17. {
  18. if (height(AVLB->rightChild->rightChild) >= height(AVLB->rightChild->leftChild))
  19. AVLB = RR_Rotate(AVLB);
  20. else
  21. AVLB = RL_Rotate(AVLB);
  22. }
  23.  
  24. if (AVLB->parent != NULL)
  25. {
  26. reBalance(AVLB->parent);
  27. }
  28. else
  29. {
  30. root = AVLB;
  31. }
  32. }
  33.  
  34. template <class T>
  35. AVLNode<T>* AVLTree<T>::RR_Rotate(AVLNode<T> *AVLB)
  36. {
  37. AVLNode<T> *tmp = AVLB->rightChild;
  38. tmp->parent = AVLB->parent;
  39. AVLB->rightChild = tmp->leftChild;
  40.  
  41. if (AVLB->rightChild != NULL)
  42. AVLB->rightChild->parent = AVLB;
  43.  
  44. tmp->leftChild = AVLB;
  45. AVLB->parent = tmp;
  46.  
  47. if (tmp->parent != NULL)
  48. {
  49. if (tmp->parent->rightChild == AVLB)
  50. {
  51. tmp->parent->rightChild = tmp;
  52. }
  53. else
  54. {
  55. tmp->parent->leftChild = tmp;
  56. }
  57. }
  58.  
  59. setBalance(AVLB);
  60. setBalance(tmp);
  61. return tmp;
  62. }
  63.  
  64. template <class T>
  65. AVLNode<T>* AVLTree<T>::LL_Rotate(AVLNode<T> *AVLB)
  66. {
  67. AVLNode<T> *tmp = AVLB->leftChild;
  68. tmp->parent = AVLB->parent;
  69. AVLB->leftChild = tmp->rightChild;
  70.  
  71. if (AVLB->leftChild != NULL)
  72. AVLB->leftChild->parent = AVLB;
  73.  
  74. tmp->rightChild = AVLB;
  75. AVLB->parent = tmp;
  76.  
  77. if (tmp->parent != NULL)
  78. {
  79. if (tmp->parent->rightChild == AVLB)
  80. {
  81. tmp->parent->rightChild = tmp;
  82. }
  83. else
  84. {
  85. tmp->parent->leftChild = tmp;
  86. }
  87. }
  88.  
  89. setBalance(AVLB);
  90. setBalance(tmp);
  91. return tmp;
  92. }
  93.  
  94. template <class T>
  95. AVLNode<T>* AVLTree<T>::LR_Rotate(AVLNode<T> *AVLB)
  96. {
  97. AVLB->leftChild = RR_Rotate(AVLB->leftChild);
  98. return LL_Rotate(AVLB);
  99. }
  100.  
  101. template <class T>
  102. AVLNode<T>* AVLTree<T>::RL_Rotate(AVLNode<T> *AVLB)
  103. {
  104. AVLB->rightChild = LL_Rotate(AVLB->rightChild);
  105. return RR_Rotate(AVLB);
  106. }
  107.  
  108. template <class T>
  109. AVLNode<T>* AVLTree<T>::getRootNode()
  110. {
  111. return root;
  112. }
  113.  
  114. template <class T>
  115. int AVLTree<T>::height(AVLNode<T> *AVLB)
  116. {
  117. if (AVLB == NULL)
  118. return -;
  119. return + max(height(AVLB->leftChild), height(AVLB->rightChild));
  120. }
  121.  
  122. template <class T>
  123. void AVLTree<T>::setBalance(AVLNode<T> *AVLB)
  124. {
  125. AVLB->balance = height(AVLB->rightChild) - height(AVLB->leftChild);
  126. }
  127.  
  128. template <class T>
  129. void AVLTree<T>::printBalance(AVLNode<T> *AVLB)
  130. {
  131. ios::sync_with_stdio(false);
  132. if (AVLB != NULL)
  133. {
  134. printBalance(AVLB->leftChild);
  135. cout << AVLB->balance << " ";
  136. //std::cout << n->key << " ";
  137. printBalance(AVLB->rightChild);
  138. }
  139. }
  140.  
  141. template <class T>
  142. void AVLTree<T>::inOrderTraverse(AVLNode<T> *AVLB)
  143. {
  144. ios::sync_with_stdio(false);
  145. if (AVLB)
  146. {
  147. inOrderTraverse(AVLB->leftChild);
  148. cout << AVLB->key << " ";
  149. inOrderTraverse(AVLB->rightChild);
  150. }
  151. }
  152.  
  153. template <class T>
  154. void AVLTree<T>::preOrderTraverse(AVLNode<T> *AVLB)
  155. {
  156. if (AVLB)
  157. {
  158. cout << AVLB->key << " ";
  159. preOrderTraverse(AVLB->leftChild);
  160. preOrderTraverse(AVLB->rightChild);
  161. }
  162. }
  163.  
  164. template <class T>
  165. void AVLTree<T>::postOrderTraverse(AVLNode<T> *AVLB)
  166. {
  167. ios::sync_with_stdio(false);
  168. if (AVLB)
  169. {
  170. postOrderTraverse(AVLB->leftChild);
  171. postOrderTraverse(AVLB->rightChild);
  172. cout << AVLB->key << " ";
  173. }
  174. }
  175.  
  176. template <class T>
  177. void AVLTree<T>::display(AVLNode <T>*AVLB, int space, int colour )
  178. {
  179. ios::sync_with_stdio(false);
  180. HANDLE hConsole;
  181. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  182. if (AVLB)
  183. {
  184. display(AVLB->rightChild, space + , colour + );
  185. SetConsoleTextAttribute(hConsole, 0x0008 | colour);
  186. //colour++;
  187. cout << endl;
  188. if (AVLB == root)
  189. cout << " Root ----> ";
  190. for (int i = ; i < space && AVLB != root; i++)
  191. cout << " ";
  192. cout << AVLB->key;
  193. display(AVLB->leftChild, space + , colour + );
  194. }
  195. }
  196.  
  197. template <class T>
  198. AVLTree<T>::AVLTree(void) : root(NULL) {}
  199.  
  200. template <class T>
  201. AVLTree<T>::~AVLTree(void)
  202. {
  203. delete root;
  204. }
  205.  
  206. template <class T>
  207. bool AVLTree<T>::insert(T key)
  208. {
  209. if (root == NULL)
  210. {
  211. root = new AVLNode<T>(key, NULL);
  212. }
  213. else
  214. {
  215. AVLNode<T> //这种风格我觉得不错
  216. //I appreciate this style of code
  217. *target = root,
  218. *parent;
  219.  
  220. while ()
  221. {
  222. if (target->key == key)
  223. return false;
  224.  
  225. parent = target;
  226.  
  227. bool goLeft = target->key > key;
  228. target = goLeft ? target->leftChild : target->rightChild;
  229.  
  230. if (target == NULL)
  231. {
  232. if (goLeft)
  233. {
  234. parent->leftChild = new AVLNode<T>(key, parent);
  235. }
  236. else
  237. {
  238. parent->rightChild = new AVLNode<T>(key, parent);
  239. }
  240.  
  241. reBalance(parent);
  242. break;
  243. }
  244. }
  245. }
  246.  
  247. return true;
  248. }
  249.  
  250. template <class T>
  251. void AVLTree<T>::deleteKey(const T delKey)
  252. {
  253. if (root == NULL)
  254. return;
  255.  
  256. AVLNode<T>
  257. *target = root,
  258. *parent = root,
  259. *delNode = NULL,
  260. *child = root;
  261.  
  262. while (child != NULL)
  263. {
  264. parent = target;
  265. target = child;
  266. child = delKey >= target->key ? target->rightChild : target->leftChild;
  267. if (delKey == target->key)
  268. delNode = target;
  269. }
  270.  
  271. if (delNode != NULL)
  272. {
  273. delNode->key = target->key;
  274.  
  275. child = target->leftChild != NULL ? target->leftChild : target->rightChild;
  276.  
  277. if (root->key == delKey)
  278. {
  279. root = child;
  280. }
  281. else
  282. {
  283. if (parent->leftChild == target)
  284. {
  285. parent->leftChild = child;
  286. }
  287. else
  288. {
  289. parent->rightChild = child;
  290. }
  291.  
  292. reBalance(parent);
  293. }
  294. }
  295. }
  296.  
  297. template <class T>
  298. void AVLTree<T>::printBalance()
  299. {
  300. ios::sync_with_stdio(false);
  301. printBalance(root);
  302. cout << endl;
  303. }
  304.  
  305. template <class T>
  306. void AVLTree<T>::inOrderTraverse()
  307. {
  308. ios::sync_with_stdio(false);
  309. inOrderTraverse(root);
  310. cout << endl;
  311. }
  312.  
  313. template <class T>
  314. void AVLTree<T>::preOrderTraverse()
  315. {
  316. ios::sync_with_stdio(false);
  317. preOrderTraverse(root);
  318. cout << endl;
  319. }
  320.  
  321. template <class T>
  322. void AVLTree<T>::postOrderTraverse()
  323. {
  324. ios::sync_with_stdio(false);
  325. postOrderTraverse(root);
  326. cout << endl;
  327. }
  328.  
  329. template <class T>
  330. void AVLTree<T>::display()
  331. {
  332. ios::sync_with_stdio(false);
  333. int color = ;
  334. display(root, , color);
  335. cout << endl;
  336. }
  1. // AVL_2.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include "AVLtree.h"
  7. #include "AVLnode.h"
  8. #include "AVLnode.cpp"
  9. #include "AVLtree.cpp"
  10.  
  11. int main()
  12. {
  13. //std::cout << "Hello World!\n";
  14. ios::sync_with_stdio(false);
  15. AVLTree<int> AVLBT;
  16. HANDLE hConsole;
  17. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  18. cout << "Inserting integer values 1 to 26" << std::endl;
  19. int Arr[] = { ,,,,,,,,,,,,,,,,,,,,,
  20. ,,, };
  21. for (int i = ; i < sizeof(Arr) / sizeof(int); i++)
  22. //for( int i = 0; Arr[i] != 0; i ++ )
  23. //AVLBT.insert(i);
  24. AVLBT.insert(Arr[i]);
  25.  
  26. cout << "Printing the balance factor of each node: " << std::endl;
  27. SetConsoleTextAttribute(hConsole, );
  28. AVLBT.printBalance();
  29. SetConsoleTextAttribute(hConsole, );
  30. cout << "Printing key: " << std::endl;
  31. SetConsoleTextAttribute(hConsole, 0x0008 | );
  32. AVLBT.inOrderTraverse();
  33. AVLBT.display();
  34. //AVLTree<int> *root = avl.getRootNode();
  35. while ()
  36. {
  37. SetConsoleTextAttribute(hConsole, );
  38. cout << "\n---------------------" << endl;
  39. cout << "AVL tree implementation" << endl;
  40. cout << "By Utah Xef developed" << endl;
  41. cout << "\n---------------------" << endl;
  42. cout << "1.insert element into the tree" << endl;
  43. cout << "2.display balanced AVL tree" << endl;
  44. cout << "3.preorder traversal" << endl;
  45. cout << "4.inorder traversal" << endl;
  46. cout << "5.postorder traversal" << endl;
  47. cout << "6.delete key" << endl;
  48. cout << "7.display the balance factor of each node" << endl;
  49. cout << "8.exit" << endl;
  50. cout << "enter your choice: ";
  51. int choice;
  52. cin >> choice;
  53.  
  54. switch (choice)
  55.  
  56. {
  57.  
  58. case :
  59.  
  60. cout << "enter value to be inserted: ";
  61. int item;
  62. cin >> item;
  63.  
  64. AVLBT.insert(item);
  65.  
  66. break;
  67.  
  68. case :
  69.  
  70. if (AVLBT.getRootNode() == nullptr)
  71.  
  72. {
  73.  
  74. cout << "tree is empty" << endl;
  75.  
  76. continue;
  77.  
  78. }
  79.  
  80. cout << "balanced avl tree:" << endl;
  81.  
  82. AVLBT.display();
  83.  
  84. break;
  85.  
  86. case :
  87.  
  88. cout << "preorder traversal:" << endl;
  89. SetConsoleTextAttribute(hConsole, 0x0008 | );
  90. AVLBT.preOrderTraverse();
  91.  
  92. cout << endl;
  93.  
  94. break;
  95. case :
  96.  
  97. cout << "inorder traversal:" << endl;
  98. SetConsoleTextAttribute(hConsole, 0x0008 | );
  99. AVLBT.inOrderTraverse();
  100.  
  101. cout << endl;
  102.  
  103. break;
  104.  
  105. case :
  106.  
  107. cout << "postorder traversal:" << endl;
  108. SetConsoleTextAttribute(hConsole, 0x0008 | );
  109. AVLBT.postOrderTraverse();
  110.  
  111. cout << endl;
  112.  
  113. break;
  114.  
  115. case :
  116. int value;
  117. cout << "Please input the value to delete:" << endl;
  118. cin >> value;
  119. AVLBT.deleteKey(value);
  120. break;
  121.  
  122. case :
  123. cout << "The balance factor of each node:" << endl;
  124. SetConsoleTextAttribute(hConsole, 0x0008 | );
  125. AVLBT.printBalance();
  126. break;
  127. case :
  128. exit();
  129.  
  130. break;
  131. default:
  132.  
  133. cout << "Wrong choice" << endl;
  134. break;
  135. }
  136.  
  137. }
  138. //std::cout << std::endl;
  139. std::cin.get();
  140. }
  141.  
  142. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  143. // Debug program: F5 or Debug > Start Debugging menu
  144.  
  145. // Tips for Getting Started:
  146. // 1. Use the Solution Explorer window to add/manage files
  147. // 2. Use the Team Explorer window to connect to source control
  148. // 3. Use the Output window to see build output and other messages
  149. // 4. Use the Error List window to view errors
  150. // 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
  151. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

数据结构实验7:实现二分查找、二叉排序(查找)树和AVL树的更多相关文章

  1. 数据结构-查找-二叉排序查找(平衡二叉树,B树,B+树概念)

    0.为什么需要二叉排序树 1)数组存储方式: 优点:通过下标访问元素,速度快,对于有序数组,可以通过二分查找提高检索效率: 缺点:如果检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低: 2 ...

  2. 第七章 二叉搜索树(d4)AVL树:(3+4)-重构

  3. 第七章 二叉搜索树 (d3)AVL树:删除

  4. 第七章 二叉搜索树 (d2)AVL树:插入

  5. 第七章 二叉搜索树 (d1)AVL树:重平衡

  6. 数据结构:JAVA_二叉数查找树基本实现(中)

    数据结构:二叉数查找树基本实现(JAVA语言版) 1.写在前面 二叉查找树得以广泛应用的一个重要原因是它能保持键的有序性,因此我们可以把它作为实现有序符号表API中的众多方法的基础. 也就是说我们构建 ...

  7. 二叉平衡查找树AvlTree(C实现)

    二叉平衡查找树即是一棵树中所有节点的左右子树高度差不超过1的查找树 头文件—————————————————————————————— #ifndef _AVLTREE_H_ #define _AVL ...

  8. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

  9. [PTA] 数据结构与算法题目集 6-12 二叉搜索树的操作集

    唯一比较需要思考的删除操作: 被删除节点有三种情况: 1.叶节点,直接删除 2.只有一个子节点,将子节点替换为该节点,删除该节点. 3.有两个子节点,从右分支中找到最小节点,将其值赋给被删除节点的位置 ...

随机推荐

  1. python之类的相关名词-继承-

    继承:父类有的功能,子类继承后也都有 继承是直接把父类方法写入子类的object里 如果定义的类有很多重复的功能,可以把重复的类定义成父类 静态方法:不需要实例化就可以调用,不可以调用类里面的变量和方 ...

  2. 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵

    题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...

  3. android 7.0 应用间文件共享FileProvider

    1.官方教程 Android 7.0 以后安全系数提高,应用间文件共享要使用FileProvider.原来的 file:/// Uri 替换为 content://Uri  https://devel ...

  4. EmitMapper系列之一:EmitMapper入门

    EmitMapper的总结 EmitMapper简介 前言: 参考官网: http://emitmapper.codeplex.com/ Project Description Powerful cu ...

  5. HttpMessageNotWritableException异常解决办法

    昨天做多对多的时遇到这个错误,网上找了一大堆,都没有解决掉,这个异常是说要解析的对象解析不了,就有可能该对象为null了,为了测试,我把数据库的数据都填上去    结果还是报错 看来是时候debug下 ...

  6. Backbone.js之Todo源码浅析

    相信每个接触了解过backbone的人都知道todo,网上的关于它的分析教程也都分析乱了.但是,知识只有自己学习领悟才是自己的,话不多说,正文开始. 在分析todo的源码之前,首先我们要知道todo具 ...

  7. java 获取ip地址

    1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...

  8. Android学习笔记(十六) ContentProvider

    1.相关概念 ContentProvider:不同应用程序之间进行数据交换的标准API:程序“暴露”数据的方法. ContentResolver:一个程序访问另一个程序被“暴露”的数据的方法. Uri ...

  9. FileZilla Server 端设置passive模式注意事项

    1,需求和问题的产生 实践中需要分布在各地的各个客户端向云端服务器上传文件,因此在阿里云服务器上安装了FileZilla Server软件作为文件FTP服务端. 客户端程序采用FTP方式向服务端传输文 ...

  10. 7z解压参数

    7z.exe x D:/test/dwpath/xxx.zip -oD:/test/dwpath/ -aoa