当前位置:   article > 正文

C++ 二叉排序树BST(二叉查找树)_c++ 将无序数组传入二叉搜索树

c++ 将无序数组传入二叉搜索树

BST属于对无序集合排序查找算法中的一种,通过定义左小右大的规则放置无序集合中的元素,使得中序遍历能得到排序后的集合,并且任一子树都是二叉排序树。二叉排序树中右子树上任一元素大于该节点上左子树中全部元素,我是通过这个性质,写的删除,也叫后继删除。当然也可以前驱删除,从本质上来说是一样的。二叉排序树的建立、查找都是相当简单的,通过迭代、递归都可以实现,不断判断当前值是否大于小于当前节点,大于往右走,小于往左走,直到走到叶节点。删除要分为3种情况:1、叶节点可直接删除2、若只有右子树,则让右子树的根替换该节点,只有左子树的话同理3、有左右子树,找到删除节点的后继,用后继的值代替要删除节点的值,若后继有右子树又分为两种情况1、删除节点右子树上有左子树,用后继父节点的左指针指向后继的右子树2、删除节点的右子树没有左子树,那么右边的节点就代替了最左的后继,此时用后继父节点的右指针指向后继的右子树。具体就来看一看代码吧


  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4. struct Node{
  5. int data;
  6. Node* left;
  7. Node* right;
  8. };
  9. void InOrderTranversal(Node* root){
  10. if(root==0){
  11. return;
  12. }
  13. InOrderTranversal(root->left);
  14. cout<<root->data<<endl;
  15. InOrderTranversal(root->right);
  16. }
  17. void release(Node* root){
  18. if(root==0){
  19. return;
  20. }
  21. release(root->left);
  22. release(root->right);
  23. delete root;
  24. }
  25. void search(Node*& root,int key,Node**& _result){
  26. if(root==0){
  27. _result=0;
  28. return;
  29. }
  30. if(key<root->data){
  31. search(root->left,key,_result);
  32. }
  33. else if(key==root->data){
  34. _result=&root;//put purpoes's addr to result for changing the purpoes pointer point another addr by _delete
  35. return;
  36. }
  37. else{
  38. search(root->right,key,_result);
  39. }
  40. }
  41. void insert(Node*& root,int value){
  42. if(root==0){
  43. root=new Node;
  44. root->left=0;
  45. root->right=0;
  46. root->data=value;
  47. return;
  48. }
  49. if(value>root->data){
  50. insert(root->right,value);
  51. }
  52. else if(value==root->data){
  53. return;
  54. }
  55. else{
  56. insert(root->left,value);
  57. }
  58. }
  59. bool _delete(Node*& root,int key){
  60. Node** node;
  61. search(root,key,node);
  62. if(node==0){
  63. return false;
  64. }
  65. Node* temp=*node;
  66. if((*node)->left==0&&(*node)->right==0){//leaf node can remove now
  67. *node=0;
  68. cout<<"0"<<endl;
  69. delete temp;
  70. }
  71. else if((*node)->left==0){//if has no left,make parent link to right and remove itself
  72. *node=(*node)->right;
  73. delete temp;
  74. }
  75. else if((*node)->right==0){
  76. *node=(*node)->left;
  77. delete temp;
  78. }
  79. else{
  80. //it must have left and right
  81. //my method is subsequent move,walk to end of right's left
  82. //s init to right
  83. //s_prent init to root
  84. Node* s=(*node)->right;
  85. Node* s_parent=*node;
  86. while(s->left!=0){
  87. s_parent=s;
  88. s=s->left;
  89. }
  90. temp->data=s->data;
  91. if(s->right!=0){//if end of right's left has right, it should replace end of right's left
  92. if(s_parent==*node){//if s_parent does not change,s_parent has had left tree,so it should put s'right to s_parent's right
  93. s_parent->right=s->right;
  94. }
  95. else{
  96. s_parent->left=s->right;
  97. }
  98. }
  99. else{
  100. if(s_parent==root){
  101. s_parent->right=0;
  102. }
  103. else{
  104. s_parent->left=0;
  105. }
  106. }
  107. delete s;
  108. }
  109. return true;
  110. }
  111. int main(){
  112. srand(time(NULL));
  113. Node* root=new Node;
  114. root->left=0;
  115. root->right=0;
  116. root->data=10;
  117. for(int i=0;i<100;i++){
  118. insert(root,rand()%100);
  119. }
  120. InOrderTranversal(root);
  121. Node** _result;
  122. int value;
  123. cout<<"please cin the value you want to find:";
  124. cin>>value;
  125. search(root,value,_result);
  126. if(_result==0){
  127. cout<<"can't find purpoes!"<<endl;
  128. }
  129. else{
  130. cout<<"the result is:"<<(*_result)->data<<endl;
  131. }
  132. cout<<"please cin the value you want to delete:";
  133. cin>>value;
  134. if(_delete(root,value)){
  135. InOrderTranversal(root);
  136. cout<<"delete succeed!"<<endl;
  137. }
  138. else{
  139. cout<<"delete failed!"<<endl;
  140. }
  141. release(root);
  142. return 0;
  143. }


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

闽ICP备14008679号