当前位置:   article > 正文

顺序表的查找和二叉排序树的查找-数据结构类C语言_用c语言实现顺序表的顺序查找和二分查找或建立一个二叉排序树并尽心查找操作实现

用c语言实现顺序表的顺序查找和二分查找或建立一个二叉排序树并尽心查找操作实现

目录

一、功能函数(func.cpp)

二、主调函数(main.cpp)

三、头文件(before.h)

四、运行截图及功能函数部分展示

        1、顺序表

       (1)顺序表的创建

       (2)顺序查找法

       (3)冒泡排序

       (4)折半查找法

        2、二叉排序树

        (1)二叉排序树的建立

        (2)二叉排序树的插入

        (3)二叉排序树的查找

五、运行环境


一、功能函数(func.cpp)

  1. #include <iostream>
  2. #include "before.h"
  3. using namespace std;
  4. //*************************************//
  5. //顺序表相关函数定义
  6. status CreateSST(SSTable &ST,int numb)
  7. {
  8. ST.R = (ElemType *)malloc(sizeof(ElemType)*(numb+1));
  9. if(!ST.R) return ERROR; //空间分配失败
  10. ST.length = numb;
  11. cout << "---请输入" << numb << "个元素:" << endl;
  12. for(int i = 1;i < numb+1;i ++) cin >> ST.R[i].key;
  13. fflush(stdin); //清除缓存
  14. return OK; //创建成功
  15. }//创建顺序表
  16. status Search_Seq(SSTable ST,KeyType key,int &cnt)
  17. {
  18. ST.R[0].key = key; //监视哨
  19. int i;
  20. for(i = ST.length;i > 0;i --) //末尾开始查找
  21. {
  22. if(ST.R[i].key != key)
  23. {
  24. cnt ++;
  25. cout << "经过的元素:" << ST.R[i].key << endl;
  26. }
  27. if(ST.R[i].key == key) break;
  28. }
  29. cnt ++;
  30. return i; //返回-1则不存在于顺序表中
  31. }//顺序查找
  32. status Search_Bin(SSTable ST,KeyType key,int &cnt)
  33. {
  34. int low = 1;
  35. int high = ST.length;
  36. int mid;
  37. if(high < 1) return OVERFLOW; //表空
  38. while(low <= high)
  39. {
  40. mid = (low+high)/2;
  41. cout << "经过元素:" << ST.R[mid].key << endl;
  42. cnt ++;
  43. if(key == ST.R[mid].key) return mid; //找到待查元素
  44. else if(key < ST.R[mid].key) high = mid-1; //在前一子表进行查找
  45. else low = mid+1; //在后一子表进行查找
  46. }
  47. return ERROR; //key不存在于顺序表
  48. }//二分查找
  49. status Bubble(SSTable &ST)
  50. {
  51. int length = ST.length;
  52. if(length < 1) return OVERFLOW; //表为空
  53. for(int i = length-1;i > 0;i --) //循环次数
  54. {
  55. for(int j = 1;j < i+1;j ++)
  56. {
  57. if(ST.R[j].key > ST.R[j+1].key) //后一个值比前一个值小就交换
  58. {
  59. KeyType temp = ST.R[j].key;
  60. ST.R[j].key = ST.R[j+1].key;
  61. ST.R[j+1].key = temp;
  62. }
  63. }
  64. }
  65. return OK;
  66. }//冒泡排序
  67. status QuickSort(SSTable &ST,int left,int right)
  68. {
  69. if(left >= right) return OK;
  70. int begin = left;
  71. int end = right;
  72. int keyi = begin;
  73. while(left < end)
  74. {
  75. while(begin < end && ST.R[end].key >= ST.R[keyi].key) end--;
  76. while(begin < end && ST.R[begin].key <= ST.R[keyi].key) begin++;
  77. KeyType temp = ST.R[begin].key;
  78. ST.R[begin].key = ST.R[end].key;
  79. ST.R[end].key = temp;
  80. }
  81. KeyType temp = ST.R[begin].key;
  82. ST.R[begin].key = ST.R[keyi].key;
  83. ST.R[keyi].key = temp;
  84. QuickSort(ST,left,begin-1);
  85. QuickSort(ST,begin+1,right);
  86. }//快速排序
  87. void TraverseSST(SSTable ST)
  88. {
  89. cout << "当前顺序表为:" << endl;
  90. for(int i = 1;i < ST.length+1;i ++)
  91. {
  92. if(i%7 == 0) cout << endl;
  93. cout << ST.R[i].key << " ";
  94. }
  95. cout << endl;
  96. }//遍历顺序表
  97. //*************************************//
  98. //*************************************//
  99. //二叉排序树的函数定义
  100. void CreateBST(BSTree &T)
  101. {
  102. T = NULL; //初始化为空树
  103. ElemType e;
  104. int numb[1000],i;
  105. cout << "---请输入一组二叉排序树的整型元素(以-1作为结束标志):" << endl;
  106. for(i = 0;numb[i] != -1;i ++)
  107. {
  108. cin >> numb[i];
  109. if(numb[i] == -1) break;
  110. e.key = numb[i];
  111. InsertBST(T,e);
  112. }
  113. }//二叉排序树的创建
  114. void InsertBST(BSTree &T,ElemType e)
  115. {
  116. if(!T)
  117. {
  118. BSTNode *s = (BSTNode *)malloc(sizeof(BSTNode));
  119. s->data.key = e.key;
  120. s->lchild = s->rchild = NULL;
  121. T = s;
  122. }
  123. else if(e.key < T->data.key) InsertBST(T->lchild,e); //插入左子树
  124. else if(e.key > T->data.key) InsertBST(T->rchild,e); //插入右子树
  125. }//二叉排序树的插入
  126. BSTree SearchBST(BSTree T,KeyType key,int &cnt)
  127. {
  128. cnt ++;
  129. if((T == NULL) || (T->lchild==NULL&&T->rchild==NULL&&T->data.key!=key) || key==T->data.key)
  130. {
  131. return T;//查找结束
  132. }
  133. else if(key < T->data.key)
  134. {
  135. cout << "经过元素:" << T->data.key << endl;
  136. return SearchBST(T->lchild,key,cnt); //在左子树中继续查找
  137. }
  138. else
  139. {
  140. cout << "经过元素:" << T->data.key << endl;
  141. return SearchBST(T->rchild,key,cnt); //在右子树中继续查找
  142. }
  143. }//二叉排序树的递归查找
  144. status TraverseBST(BSTree T)
  145. {
  146. if(T == NULL) return OK;
  147. else
  148. {
  149. TraverseBST(T->lchild);
  150. cout << T->data.key << " ";
  151. TraverseBST(T->rchild);
  152. }
  153. }//二叉排序树的中序遍历
  154. //调用菜单
  155. void Menu()
  156. {
  157. printf("\t\t\t********************此为最后一次数据结构实验的菜单***********************\n");
  158. printf("\t\t\t\t1、顺序表 2、 二叉排序树 \n");
  159. printf("\t\t\t\t0、退出当前操作系统 \n");
  160. printf("\t\t\t**********************************************************************\n");
  161. }//调用菜单的打印
  162. void Menu_SL()
  163. {
  164. printf("\t\t\t*************************此为顺序表的菜单*******************************\n");
  165. printf("\t\t\t\t1、顺序表的创建 2、顺序查找 \n");
  166. printf("\t\t\t\t3、冒泡排序 4、快速排序 \n");
  167. printf("\t\t\t\t5、折半查找法 6、遍历顺序表 \n");
  168. printf("\t\t\t\t0、退出当前操作系统 \n");
  169. printf("\t\t\t**********************************************************************\n");
  170. }//顺序表菜单的打印
  171. void Menu_BST()
  172. {
  173. printf("\t\t\t***********************此为二叉排序树的菜单*****************************\n");
  174. printf("\t\t\t\t1、二叉排序树的建立 2、二叉排序树的查找 \n");
  175. printf("\t\t\t\t3、二叉排序树的插入 4、二叉排序树的遍历 \n");
  176. printf("\t\t\t\t0、退出当前操作系统 \n");
  177. printf("\t\t\t**********************************************************************\n");
  178. }//二叉排序树菜单的打印
  179. //
  180. //Created by somewon on 2022/12/6.
  181. //

二、主调函数(main.cpp)

  1. #include <iostream>
  2. #include "before.h"
  3. using namespace std;
  4. int main()
  5. {
  6. int CH;
  7. do {
  8. Menu();
  9. cout << "---请输入进入的菜单:";
  10. cin >> CH;
  11. if(CH == 1) //顺序表调用函数
  12. {
  13. int ch;
  14. int numb;
  15. int fine,cnt;
  16. KeyType find;
  17. SSTable ST;
  18. do {
  19. Menu_SL();
  20. cout << "---请输入你的选择:";
  21. cin >> ch;
  22. switch(ch)
  23. {
  24. case 1:
  25. cout << "---请输入顺序表元素的个数:";
  26. cin >> numb;
  27. fine = CreateSST(ST,numb);
  28. if(fine == ERROR) cout << "当前顺序表空间分配失败!请再次进行操作!" << endl;
  29. else TraverseSST(ST);
  30. break;
  31. case 2:
  32. cout << "---请输入需要查找的元素:";
  33. cin >> find;
  34. cnt = 0; //查找次数
  35. fine = Search_Seq(ST,find,cnt);
  36. if(fine == 0)
  37. {
  38. cout << "该元素不在顺序表中!" << endl;
  39. cout << "查找次数为:" << cnt << endl; //末尾开始查找
  40. }
  41. else
  42. {
  43. cout << "所查找的元素" << find << "的位置为:" << fine << endl;
  44. cout << "查找次数为:" << cnt << endl; //末尾开始查找
  45. }
  46. break;
  47. case 3:
  48. fine = Bubble(ST);
  49. if(fine == OVERFLOW) cout << "当前顺序表为空!" << endl;
  50. else if(fine == OK)
  51. {
  52. cout << "冒泡排序成功!" << endl;
  53. TraverseSST(ST);
  54. }
  55. break;
  56. case 4:
  57. fine = QuickSort(ST,1,ST.length);
  58. if(fine == OK)
  59. {
  60. cout << "快速排序成功!" << endl;
  61. TraverseSST(ST);
  62. }
  63. break;
  64. case 5:
  65. cout << "---请输入需要查找的元素:";
  66. cin >> find;
  67. cnt = 0;
  68. fine = Search_Bin(ST,find,cnt);
  69. if(fine == ERROR)
  70. {
  71. cout << "该元素不在顺序表中!" << endl;
  72. cout << "查找的次数为:" << cnt << endl;
  73. }
  74. else if(fine == OVERFLOW) cout << "该表为空!" << endl;
  75. else
  76. {
  77. cout << "所查找的元素" << find << "的位置为:" << fine << endl;
  78. cout << "查找的次数为:" << cnt << endl;
  79. }
  80. break;
  81. case 6:
  82. TraverseSST(ST);
  83. break;
  84. case 0:
  85. goto END;
  86. }
  87. }while(ch);
  88. }
  89. else if(CH == 2) //二叉排序树调用函数
  90. {
  91. int ch;
  92. BSTree T,s;
  93. int cnt;
  94. KeyType find;
  95. ElemType e;
  96. do {
  97. Menu_BST();
  98. cout << "---请输入你的选择:";
  99. cin >> ch;
  100. switch(ch)
  101. {
  102. case 1:
  103. CreateBST(T);
  104. cout << "二叉排序树建立完成!" << endl;
  105. TraverseBST(T);
  106. cout << endl;
  107. break;
  108. case 2:
  109. cout << "---请输入需要查询的整型数据:";
  110. cin >> find;
  111. cnt = 0;
  112. s = NULL;
  113. s = SearchBST(T,find,cnt);
  114. if(s == NULL || s->data.key!=find)
  115. {
  116. cout << "该元素不存在于二叉排序树中!" << endl;
  117. cout << "查找次数为:" << cnt << endl;
  118. }
  119. else
  120. {
  121. cout << "查找的元素" << find <<"存在于二叉排序树中!" << endl;
  122. cout << "查找次数为:" << cnt << endl;
  123. }
  124. break;
  125. case 3:
  126. cout << "---请输入需要插入的元素:";
  127. cin >> e.key;
  128. InsertBST(T,e);
  129. cout << "插入成功!" << endl;
  130. cout << "当前排序二叉树为:" << endl;
  131. TraverseBST(T);
  132. cout << endl;
  133. break;
  134. case 4:
  135. cout << "当前排序二叉树为:" << endl;
  136. TraverseBST(T);
  137. cout << endl;
  138. break;
  139. case 0:
  140. goto END;
  141. }
  142. }while(ch);
  143. }
  144. else if(CH == 0) goto ENDD;
  145. else cout << "无该选择!" << endl;
  146. END:
  147. cout << "已退出当前菜单!" << endl;
  148. }while(CH);
  149. ENDD:
  150. cout << "已退出当前程序!" << endl;
  151. return 0;
  152. }
  153. //
  154. //Created by somewon on 2022/12/6.
  155. //

三、头文件(before.h)

  1. #ifndef _BEFORE_H
  2. #define _BEFORE_H
  3. #define OK 1
  4. #define ERROR 0
  5. #define OVERFLOW (-1)
  6. typedef int status;
  7. typedef int KeyType;
  8. typedef int InfoType;
  9. typedef struct
  10. {
  11. KeyType key;
  12. InfoType otherinfo;
  13. }ElemType;
  14. //顺序表相关函数声明
  15. typedef struct
  16. {
  17. ElemType *R;
  18. int length;
  19. }SSTable;
  20. status CreateSST(SSTable &ST,int numb); //创建顺序表
  21. status Search_Seq(SSTable ST,KeyType key,int &cnt);//顺序查找
  22. status Search_Bin(SSTable ST,KeyType key,int &cnt);//二分查找
  23. status Bubble(SSTable &ST); //冒泡排序
  24. status QuickSort(SSTable &ST,int left,int right); //快速排序
  25. void TraverseSST(SSTable ST); //遍历顺序表
  26. //排序二叉树相关函数声明
  27. typedef struct BSTNode
  28. {
  29. ElemType data;
  30. struct BSTNode *lchild,*rchild;
  31. }BSTNode,*BSTree;
  32. void CreateBST(BSTree &T); //二叉排序树的创建
  33. void InsertBST(BSTree &T,ElemType e); //二叉排序树的插入
  34. BSTree SearchBST(BSTree T,KeyType key,int &cnt); //二叉排序树的递归查找
  35. status TraverseBST(BSTree T); //二叉排序树的遍历
  36. //相关菜单的函数声明
  37. void Menu(); //大菜单
  38. void Menu_SL(); //顺序表的菜单
  39. void Menu_BST(); //二叉排序树的菜单
  40. #endif
  41. //
  42. //Created by somewon on 2022/12/6.
  43. //

四、运行截图及功能函数部分展示

        1、顺序表

       (1)顺序表的创建

  1. status CreateSST(SSTable &ST,int numb)
  2. {
  3. ST.R = (ElemType *)malloc(sizeof(ElemType)*(numb+1));
  4. if(!ST.R) return ERROR; //空间分配失败
  5. ST.length = numb;
  6. cout << "---请输入" << numb << "个元素:" << endl;
  7. for(int i = 1;i < numb+1;i ++) cin >> ST.R[i].key;
  8. fflush(stdin); //清除缓存
  9. return OK; //创建成功
  10. }//创建顺序表

         (2)顺序查找法

  1. status Search_Seq(SSTable ST,KeyType key,int &cnt)
  2. {
  3. ST.R[0].key = key; //监视哨
  4. int i;
  5. for(i = ST.length;i > 0;i --) //末尾开始查找
  6. {
  7. if(ST.R[i].key != key)
  8. {
  9. cnt ++;
  10. cout << "经过的元素:" << ST.R[i].key << endl;
  11. }
  12. if(ST.R[i].key == key) break;
  13. }
  14. cnt ++;
  15. return i; //返回-1则不存在于顺序表中
  16. }//顺序查找

 

        (3)冒泡排序

  1. status Bubble(SSTable &ST)
  2. {
  3. int length = ST.length;
  4. if(length < 1) return OVERFLOW; //表为空
  5. for(int i = length-1;i > 0;i --) //循环次数
  6. {
  7. for(int j = 1;j < i+1;j ++)
  8. {
  9. if(ST.R[j].key > ST.R[j+1].key) //后一个值比前一个值小就交换
  10. {
  11. KeyType temp = ST.R[j].key;
  12. ST.R[j].key = ST.R[j+1].key;
  13. ST.R[j+1].key = temp;
  14. }
  15. }
  16. }
  17. return OK;
  18. }//冒泡排序

         (4)折半查找法

  1. status Search_Bin(SSTable ST,KeyType key,int &cnt)
  2. {
  3. int low = 1;
  4. int high = ST.length;
  5. int mid;
  6. if(high < 1) return OVERFLOW; //表空
  7. while(low <= high)
  8. {
  9. mid = (low+high)/2;
  10. cout << "经过元素:" << ST.R[mid].key << endl;
  11. cnt ++;
  12. if(key == ST.R[mid].key) return mid; //找到待查元素
  13. else if(key < ST.R[mid].key) high = mid-1; //在前一子表进行查找
  14. else low = mid+1; //在后一子表进行查找
  15. }
  16. return ERROR; //key不存在于顺序表
  17. }//二分查找

        2、二叉排序树

        (1)二叉排序树的建立

  1. void CreateBST(BSTree &T)
  2. {
  3. T = NULL; //初始化为空树
  4. ElemType e;
  5. int numb[1000],i;
  6. cout << "---请输入一组二叉排序树的整型元素(以-1作为结束标志):" << endl;
  7. for(i = 0;numb[i] != -1;i ++)
  8. {
  9. cin >> numb[i];
  10. if(numb[i] == -1) break;
  11. e.key = numb[i];
  12. InsertBST(T,e);
  13. }
  14. }//二叉排序树的创建

         (2)二叉排序树的插入

  1. void InsertBST(BSTree &T,ElemType e)
  2. {
  3. if(!T)
  4. {
  5. BSTNode *s = (BSTNode *)malloc(sizeof(BSTNode));
  6. s->data.key = e.key;
  7. s->lchild = s->rchild = NULL;
  8. T = s;
  9. }
  10. else if(e.key < T->data.key) InsertBST(T->lchild,e); //插入左子树
  11. else if(e.key > T->data.key) InsertBST(T->rchild,e); //插入右子树
  12. }//二叉排序树的插入

 

         (3)二叉排序树的查找

  1. BSTree SearchBST(BSTree T,KeyType key,int &cnt)
  2. {
  3. cnt ++;
  4. if((T == NULL) || (T->lchild==NULL&&T->rchild==NULL&&T->data.key!=key) || key==T->data.key)
  5. {
  6. return T;//查找结束
  7. }
  8. else if(key < T->data.key)
  9. {
  10. cout << "经过元素:" << T->data.key << endl;
  11. return SearchBST(T->lchild,key,cnt); //在左子树中继续查找
  12. }
  13. else
  14. {
  15. cout << "经过元素:" << T->data.key << endl;
  16. return SearchBST(T->rchild,key,cnt); //在右子树中继续查找
  17. }
  18. }//二叉排序树的递归查找

         

五、运行环境

        CLion、Dev(注:Dev中运行可能需要加上一些头文件,如:#include <stdlib.h>)

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

闽ICP备14008679号