数据结构(二叉树查找)——二叉排序树的构造和查找、插入、删除

待我称王封你为后i 2022-12-24 11:51 294阅读 0赞

设计一个读入一串整数,然后构造二叉排序树,进行查找、插入、删除。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TRUE 1
  4. #define FALSE 0
  5. #define ENDKEY 0
  6. typedef int KeyType;
  7. typedef struct node
  8. {
  9. KeyType key ; /*关键字的值*/
  10. struct node *lchild,*rchild;/*左右指针*/
  11. }BSTNode, *BSTree;
  12. int InsertBST(BSTree *bst, KeyType key)
  13. /*若在二叉排序树中不存在关键字等于key的元素,插入该元素*/
  14. {
  15. BSTree s;
  16. if(*bst==NULL)
  17. {
  18. s=(BSTree)malloc(sizeof(BSTNode));
  19. s->key=key;
  20. s->lchild=NULL;
  21. s->rchild=NULL;
  22. *bst=s;
  23. }
  24. else if(key<(*bst)->key)
  25. InsertBST(&((*bst)->lchild),key);
  26. else if(key>(*bst)->key)
  27. InsertBST(&((*bst)->rchild),key);
  28. return 0;
  29. //请完成本函数的功能
  30. }
  31. void CreateBST(BSTree *bst)
  32. /*从键盘输入元素的值,创建相应的二叉排序树*/
  33. {
  34. KeyType key;
  35. *bst=NULL;
  36. scanf("%d",&key);
  37. while(key!=ENDKEY)
  38. {
  39. InsertBST(bst,key);
  40. scanf("%d",&key);
  41. }
  42. //请完成本函数的功能
  43. }
  44. void InOrder(BSTree bst)
  45. /*中序遍历二叉树, root为指向二叉树(或某一子树)根结点的指针*/
  46. {
  47. if (bst!=NULL)
  48. {
  49. InOrder(bst ->lchild); /*中序遍历左子树*/
  50. printf("%d->",bst->key); /*访问根结点*/
  51. InOrder(bst ->rchild); /*中序遍历右子树*/
  52. }
  53. }
  54. BSTree SearchBST(BSTree bst, KeyType key)
  55. /*在根指针bst所指二叉排序树中,递归查找某关键字等于key的元素,若查找成功,返回指向该元素结点指针,否则返回空指针*/
  56. {
  57. if(!bst)
  58. return NULL;
  59. else if(bst->key==key)
  60. return bst;
  61. else if(bst->key>key)
  62. return SearchBST(bst->lchild,key);
  63. else
  64. return SearchBST(bst->rchild,key);
  65. //请完成本函数的功能
  66. }
  67. int DelBST(BSTree t, KeyType k) /*在二叉排序树t中删去关键字为k的结点*/
  68. {
  69. BSTNode *p, *f,*s ,*q;
  70. p=t;
  71. f=NULL;
  72. while(p) /*查找关键字为k的待删结点p*/
  73. {
  74. if(p->key==k ) break; /*找到则跳出循环*/
  75. f=p; /*f指向p结点的双亲结点*/
  76. if(p->key>k)
  77. p=p->lchild;
  78. else
  79. p=p->rchild;
  80. }
  81. if(p==NULL) return 0; /*若找不到,返回原来的二叉排序树*/
  82. if(p->lchild==NULL) /*p无左子树*/
  83. {
  84. if(f==NULL)
  85. t=p->rchild; /*p是原二叉排序树的根*/
  86. else
  87. if(f->lchild==p) /*p是f的左孩子*/
  88. f->lchild=p->rchild ; /*将p的右子树链到f的左链上*/
  89. else /*p是f的右孩子*/
  90. f->rchild=p->rchild ; /*将p的右子树链到f的右链上*/
  91. free(p); /*释放被删除的结点p*/
  92. }
  93. else /*p有左子树*/
  94. {
  95. q=p;
  96. s=p->lchild;
  97. while(s->rchild) /*在p的左子树中查找最右下结点*/
  98. {
  99. q=s;
  100. s=s->rchild;
  101. }
  102. if(q==p)
  103. q->lchild=s->lchild ; /*将s的左子树链到q上*/
  104. else
  105. q->rchild=s->lchild;
  106. p->key=s->key; /*将s的值赋给p*/
  107. free(s);
  108. }
  109. return 1;
  110. } /*DelBST*/
  111. void main()
  112. {
  113. BSTree T,p;
  114. int keyword,temp;
  115. char ch,j='y';
  116. T=NULL;
  117. while(j!='n')
  118. {
  119. printf("1.创建二叉排序树\n");
  120. printf("2.显示排序的数据\n");
  121. printf("3.查找数据\n");
  122. printf("4.在查找树中插入一个数据\n");
  123. printf("5.在查找树中删除一个数据\n");
  124. printf("6.程序结束,退出\n");
  125. scanf(" %c",&ch); //输入操作选项
  126. switch(ch)
  127. {
  128. case '1':
  129. printf("请输入数据,以0作为数据输入结束。\n");
  130. CreateBST(&T);
  131. break;
  132. case '2':
  133. if(!T) printf("二叉排序树中没有数据。\n");
  134. else {
  135. InOrder(T);printf("\b\b \n");}
  136. break;
  137. case '3':
  138. printf("输入待查找的数据值:\n");
  139. scanf("%d",&keyword); //输入要查找元素的关键字
  140. p=SearchBST(T, keyword);
  141. if(!p) printf("%d 没有找到。\n",keyword); //没有找到
  142. else printf("%d 查找成功。\n",keyword); //成功找到
  143. break;
  144. case '4':
  145. printf("输入待插入的数据:");
  146. scanf("%d",&keyword); //输入要插入元素的关键字
  147. temp=InsertBST(&T, keyword);
  148. if(temp==FALSE)
  149. printf("%d 已在二叉树中!\n",keyword); //该元素已经存在
  150. else
  151. printf("%d 插入成功!\n",keyword);
  152. break;
  153. case '5':
  154. printf("输入待删除的数据:");
  155. scanf("%d",&keyword); //输入要删除元素的关键字
  156. temp=DelBST(T, keyword);
  157. if(temp==FALSE) printf("%d 不存在!\n",keyword); //该元素不存在
  158. else printf("成功删除%d\n",keyword); //成功删除
  159. break;
  160. default:
  161. j='n';
  162. }
  163. }
  164. printf("程序结束!\nPress any key to shut off the window!\n");
  165. getchar();
  166. getchar();
  167. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读

    相关 查找(排序)

    构造一棵二叉排序树并对其进行中序遍历输出。 在二叉排序树中查找某一关键字,若存在,显示“查找成功”以及查找成功时关键字比较次数;若不存在,将其插入到二叉排序树中,再中序遍历输出

    相关 查找-排序查找

    二叉排序树的性质: (1)若某节点的左子树非空,则左子树上所有元素的值都小于该元素的值。 (2)若某节点的右子树非空,则右子树上所有元素的值都大于该元素的值。 问题:在二