【数据结构】二叉查找树

前端之家收集整理的这篇文章主要介绍了【数据结构】二叉查找树前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


建树

树节点的存储方式与普通的二叉树相似,用结构体存储,其中一个 int 变量存储数据,两个指针指向左右儿子。

读入一个新数据,从根节点开始寻找,大于当前节点则继续寻找右子树,右子树为空则直接插入,小于当前节点则继续寻找左子树,左子树为空则直接插入。

注:

初始数据要求随机数,为了产生真正意义上的“随机数”,使用时间作为随机数种子。



查找

从根节点开始寻找,大于当前节点则转向右子树,小于当前节点则转向左子树。直到找到等于的节点。如果转到了空节点,则说明这个数并不在这棵二叉查找树上。




插入

插入的过程与查找相似。从头开始寻找,如果在树上找到了,则说明树上已有此节点,不需再插入。如果没有在树上,那么会走到一个空节点,此时直接将新节点插入空节点处即可。注意与上一级节点的衔接。



删除

首先是寻找要删除的节点。这一步与寻找的过程相似。

当找到要删除的节点后,会出现以下情况:

1)左右子树为空

直接删除即可

2)左子树为空

直接将右子树移到待删除节点处即可。

3)右子树为空

直接将左子树移到待删除节点处即可。

4)左右子树均不为空

先将左子树整体插入到右子树中,再将右子树移到待删除节点处。



遍历与二分查找


二叉查找树的遍历与普通二叉树相同,可以通过递归实现。先递归左子树,然后输出根节点,最后递归右子树。这样便得到一个有序的序列。

对于这个有序的序列中的任意元素,都可以通过二分查找找到他们的具体位置。二分查找可以通过一个循环实现,对于每一层循环,先比较目标元素与当前区间中心元素进行比较,目标元素大于中间元素时,说明在右区间内,否则在左区间内。直到区间锁定到一个元素,即目标元素。


CODE:
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<vector>
  6. #include<time.h>
  7. #define N 100
  8. using namespace std;
  9.  
  10. typedef struct Tree
  11. {
  12. int data;
  13. Tree *ls,*rs;
  14. };
  15.  
  16. Tree *head;
  17. int sorr[N];
  18. int nn=0;
  19. void build(Tree *T,Tree *now);
  20. void printt(Tree *now,int sorr[]);
  21. void fi(Tree *now,int ii);
  22. void find();
  23. void insee(Tree *now,Tree *ii);
  24. void insert();
  25. void inn(Tree *now,Tree *ii);
  26. void del(Tree *now,int ii);
  27. void delet();
  28. int binSearch(const int *Array,int start,int end,int key);
  29. void find_2(int sorr[]);
  30.  
  31. int main()
  32. {
  33. srand((unsigned)time(NULL));
  34. head=NULL;
  35. for (int i=0;i<10;i++)
  36. {
  37. int num=rand()%100+1;
  38. Tree *T;
  39. T=new Tree;
  40. T->data=num;
  41. T->ls=T->rs=NULL;
  42. build(T,head);
  43. }
  44. cout<<"======PRI======"<<endl;
  45. printt(head,sorr);
  46. find();
  47. insert();
  48. nn=0;cout<<"---AFTER INSERT---"<<endl;
  49. printt(head,sorr);
  50. delet();
  51. nn=0;cout<<"---AFTER DELETE---"<<endl;
  52. printt(head,sorr);
  53. find_2(sorr);
  54. cout<<"======FINAL======"<<endl;
  55. printt(head,sorr);
  56. }
  57.  
  58. void build(Tree *T,Tree *now)
  59. {
  60. if (head==NULL)
  61. {
  62. head=T;
  63. return;
  64. }
  65. if (now->data>T->data)
  66. {
  67. if (now->ls!=NULL)
  68. {
  69. build(T,now->ls);
  70. }
  71. else
  72. now->ls=T;
  73. }
  74. else if (now->data<T->data)
  75. {
  76. if (now->rs!=NULL)
  77. {
  78. build(T,now->rs);
  79. }
  80. else
  81. now->rs=T;
  82. }
  83. }
  84.  
  85. void printt(Tree *now,int sorr[])
  86. {
  87. if (head->data==0)
  88. {
  89. cout<<"NULL!"<<endl;
  90. return;
  91. }
  92. if (now->ls!=NULL)
  93. printt(now->ls,sorr);
  94. if (now->data!=0)
  95. {
  96. cout<<now->data<<" ";
  97. //if (now->ls!=NULL) cout<<now->ls->data<<" ";
  98. //if (now->rs!=NULL) cout<<now->rs->data;
  99. cout<<endl;
  100. }
  101. sorr[nn]=now->data;
  102. nn++;
  103. if (now->rs!=NULL)
  104. printt(now->rs,sorr);
  105. }
  106.  
  107. void fi(Tree *now,int ii)
  108. {
  109. if (now->data==ii)
  110. {
  111. cout<<"GET IT!"<<endl;
  112. return;
  113. }
  114. if (now->rs ==NULL && now->ls==NULL)
  115. {
  116. cout<<"NO!"<<endl;
  117. return;
  118. }
  119. if (now->data>ii) fi(now->ls,ii);
  120. else fi(now->rs,ii);
  121. return;
  122. }
  123.  
  124. void find()
  125. {
  126. cout<<"======FIND======"<<endl;
  127. int n;
  128. cin>>n;
  129. while (n!=-1)
  130. {
  131. fi(head,n);
  132. cin>>n;
  133. }
  134. //cout<<now->data<<endl;
  135. }
  136. void insee(Tree *now,Tree *ii)
  137. {
  138. if (now->data==ii->data)
  139. {
  140. cout<<"HAVE HAD IT!"<<endl;
  141. return;
  142. }
  143. if (now->data>ii->data)
  144. {
  145. if (now->ls==NULL)
  146. now->ls=ii;
  147. else insee(now->ls,ii);
  148. }
  149. else if (now->data<ii->data)
  150. {
  151. if (now->rs==NULL)
  152. now->rs=ii;
  153. else insee(now->rs,ii);
  154. }
  155. return;
  156. }
  157. void insert()
  158. {
  159. int n;
  160. cout<<"=====INSERT====="<<endl;
  161. cin>>n;
  162. while (n!=-1)
  163. {
  164. Tree *T1;
  165. T1=new Tree;
  166. T1->data=n;
  167. T1->ls=T1->rs=NULL;
  168. insee(head,T1);
  169. cin>>n;
  170. }
  171. }
  172. void inn(Tree *now,Tree *ii)
  173. {
  174. if (now->data>ii->data)
  175. {
  176. if (now->ls==NULL)
  177. now->ls=ii;
  178. else inn(now->ls,ii);
  179. }
  180. else if (now->data<ii->data)
  181. {
  182. if (now->rs==NULL)
  183. now->rs=ii;
  184. else inn(now->rs,ii);
  185. }
  186. return;
  187. }
  188. void del(Tree *now,int ii)
  189. {
  190. //cout<<now->data<<endl;
  191. if (now->data==ii)
  192. {
  193. if (now->ls==NULL && now->rs==NULL)
  194. memset(now,sizeof(Tree));
  195. else if (now->ls==NULL)
  196. *now=*(now->rs);
  197. else if (now->rs==NULL)
  198. *now=*(now->ls);
  199. else if (now->ls!=NULL)
  200. {
  201. inn(now->rs,now->ls);
  202. *now=*(now->rs);
  203. }
  204. cout<<"OK"<<endl;
  205. return;
  206. }
  207. if (now->rs ==NULL && now->ls==NULL)
  208. {
  209. cout<<"NO!"<<endl;
  210. return;
  211. }
  212. if (now->data>ii) del(now->ls,ii);
  213. else del(now->rs,ii);
  214. return;
  215. }
  216.  
  217. void delet()
  218. {
  219. cout<<"======DELETE======="<<endl;
  220. int n;
  221. cin>>n;
  222. while (n!=-1)
  223. {
  224. del(head,n);
  225. nn=0;cout<<"...NOW..."<<endl;
  226. printt(head,sorr);
  227. cout<<endl;
  228. cin>>n;
  229. }
  230. return;
  231. }
  232. int binSearch(const int *Array,int key)
  233. {
  234. int left,right;
  235. int mid;
  236. left=start;
  237. right=end;
  238. while(left<=right)
  239. {
  240. mid=(left+right)/2;
  241. if(key==Array[mid]) return mid;
  242. else if(key<Array[mid]) right=mid-1;
  243. else if(key>Array[mid]) left=mid+1;
  244. }
  245. return -1;
  246. }
  247. void find_2(int sorr[])
  248. {
  249. cout<<"=====FIND_2===="<<endl;
  250. int n;
  251. cin>>n;
  252. while (n!=-1)
  253. {
  254. cout<<binSearch(sorr,nn,n)<<endl;
  255. cin>>n;
  256. }
  257. return;
  258. }

猜你在找的数据结构相关文章