暂未发现什么bug,如果发现请指出。

  1. #include<iostream>
  2. using namespace std;
  3. //定义二叉搜索树的结点
  4. struct Node
  5. {
  6. int data;
  7. Node *lc,*rc,*parent;
  8. };
  9. //中序遍历二叉搜索树
  10. void show(Node *now)
  11. {
  12. if(now==NULL) return;
  13. show(now->lc);
  14. cout<<now->data<<endl;
  15. show(now->rc);
  16. }
  17. //将值为val的结点插入到二叉搜索树中
  18. //?为什么要传指向根结点指针的指针,而不是直接传根结点的指针。因为根结点的指针可能为空,这里需要开辟内存。
  19. Node* insert(Node **rt,int val)
  20. {
  21. Node *now=*rt;//用now指针在树上移动
  22. Node *par=NULL;//记录now指针的父亲
  23. while(now!=NULL)
  24. {
  25. par=now;
  26. if(val<now->data)//如果val小于当前结点的值说明应该插到左子树中
  27. now=now->lc;
  28. else
  29. now=now->rc; //否则插入到右子树中
  30. }
  31. Node *newnode=new Node;//开辟新结点
  32. newnode->data=val;
  33. newnode->lc=newnode->rc=NULL;
  34. if(par==NULL)//当这是一棵空树的时候
  35. {
  36. newnode->parent=NULL;
  37. *rt=newnode;//直接通过指针修改根结点
  38. }
  39. else
  40. {
  41. if(val<par->data)
  42. par->lc=newnode;
  43. else
  44. par->rc=newnode;
  45. newnode->parent=par;
  46. }
  47. return newnode;
  48. }
  49. //在以now为根结点的树上,寻找值为val的结点
  50. Node* search(Node *now,int val)
  51. {
  52. while(now!=NULL&&now->data!=val)
  53. {
  54. if(val<now->data)
  55. now=now->lc;
  56. else
  57. now=now->rc;
  58. }
  59. return now;
  60. }
  61. //在以now为根结点的树上,寻找最大、最小值
  62. Node* maximun(Node *now)
  63. {
  64. while(now->rc!=NULL)
  65. {
  66. now=now->rc;
  67. }
  68. return now;
  69. }
  70. Node* minimun(Node *now)
  71. {
  72. while(now->lc!=NULL)
  73. now=now->lc;
  74. return now;
  75. }
  76. //寻找now结点的前继、后继
  77. //寻找前继:如果now的左子树非空,则返回左子树的最大值结点。否则如果now是父亲的左孩子则不断向上直到now不再是父亲的左孩子,返回父结点。
  78. Node* predecessor(Node *now)
  79. {
  80. if(now->lc!=NULL)
  81. return maximun(now->lc);
  82. Node *par=now->parent;
  83. while(par!=NULL&&now==par->lc)
  84. {
  85. now=par;
  86. par=par->parent;
  87. }
  88. return par;
  89. }
  90. Node* successor(Node *now)
  91. {
  92. if(now->rc!=NULL)
  93. return minimun(now->rc);
  94. Node *par=now->parent;
  95. while(par!=NULL&&now==par->rc)
  96. {
  97. now=par;
  98. par=par->parent;
  99. }
  100. return par;
  101. }
  102. //对于一棵以T为根结点的树,用以v为根结点的子树取代以u为根结点的子树,完成u的父亲与v之间的绑定
  103. void transplant(Node **T,Node *u,Node *v)
  104. {
  105. if(u->parent==NULL)
  106. (*T)=v;
  107. else if(u==u->parent->lc)
  108. u->parent->lc=v;
  109. else if(u==u->parent->rc)
  110. u->parent->rc=v;
  111. if(v!=NULL)
  112. v->parent=u->parent;
  113. }
  114. //删除给定结点
  115. void erase(Node **T,Node *now)
  116. {
  117. Node *temp=now;
  118. if(now->lc==NULL)
  119. {
  120. transplant(T,now,now->rc);
  121. }
  122. else if(now->rc==NULL)
  123. {
  124. transplant(T,now,now->lc);
  125. }
  126. else
  127. {
  128. Node* nextnode=successor(now);
  129. if(nextnode!=now->rc)
  130. {
  131. transplant(T,nextnode,nextnode->rc);
  132. nextnode->rc=now->rc;
  133. now->rc->parent=nextnode;
  134. }
  135. transplant(T,now,nextnode);
  136.  
  137. nextnode->lc=now->lc;
  138. now->lc->parent=nextnode;
  139. }
  140. delete temp;
  141. }
  142. int main()
  143. {
  144. Node *root=NULL;
  145. )
  146. {
  147. cout<<"1.插入结点"<<endl;
  148. cout<<"2.删除结点"<<endl;
  149. cout<<"3.中序遍历"<<endl;
  150. cout<<"4.查询"<<endl;
  151. cout<<"5.查询根"<<endl;
  152. int p;
  153. cin>>p;
  154. )
  155. {
  156. cout<<"输入待插元素值:"<<endl;
  157. int t;
  158. cin>>t;
  159. insert(&root,t);
  160. cout<<"插入成功!"<<endl;
  161. }
  162. )
  163. {
  164. cout<<"输入待删元素值:"<<endl;
  165. int t;
  166. cin>>t;
  167. Node *q=search(root,t);
  168. if(q==NULL)cout<<"该值不存在!"<<endl;
  169. else
  170. {
  171. erase(&root,q);
  172. cout<<"删除成功!"<<endl;
  173. }
  174. }
  175. )
  176. {
  177. cout<<"======"<<endl;
  178. show(root);
  179. cout<<"======"<<endl;
  180. }
  181. )
  182. {
  183. cout<<"输入待查询元素值:"<<endl;
  184. int t;
  185. cin>>t;
  186. Node *q=search(root,t);
  187. if(q==NULL) cout<<"该值不存在!"<<endl;
  188. else
  189. {
  190. Node *a=predecessor(q),*b=successor(q);
  191. if(a!=NULL)
  192. cout<<"前继:"<<a->data<<endl;
  193. if(b!=NULL)
  194. cout<<"后继:"<<b->data<<endl;
  195. }
  196. }
  197. )
  198. {
  199. if(root==NULL) cout<<"根为空!"<<endl;
  200. else cout<<"根:"<<root->data<<endl;
  201. }
  202. }
  203. ;
  204. }

二叉搜索树 C++代码实现的更多相关文章

  1. python 二叉搜索树相关代码

    class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None class Ope ...

  2. 基于visual Studio2013解决算法导论之029二叉搜索树

     题目 二叉搜索树 解决代码及点评 #include <stdio.h> #include <malloc.h> #include <stdlib.h> ty ...

  3. 二叉搜索树(Binary Search Tree)--C语言描述(转)

    图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...

  4. 自己动手实现java数据结构(六)二叉搜索树

    1.二叉搜索树介绍 前面我们已经介绍过了向量和链表.有序向量可以以二分查找的方式高效的查找特定元素,而缺点是插入删除的效率较低(需要整体移动内部元素):链表的优点在于插入,删除元素时效率较高,但由于不 ...

  5. 数据结构 - 二叉搜索树封装 C++

    二叉搜索树封装代码 #pragma once #include <iostream> using namespace std; template<class T>class T ...

  6. 每日一题 - 剑指 Offer 33. 二叉搜索树的后序遍历序列

    题目信息 时间: 2019-06-26 题目链接:Leetcode tag:分治算法 递归 难易程度:中等 题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果.如果是则返回 tr ...

  7. 编程算法 - 二叉搜索树 与 双向链表 代码(C++)

    二叉搜索树 与 双向链表 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目:输入一颗二叉搜索树, 将该二叉搜索树转换成一个排序的双向链表. 要求 ...

  8. 编程算法 - 二叉搜索树(binary search tree) 代码(C)

    二叉搜索树(binary search tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉搜索树(binary search tree)能 ...

  9. BinarySearchTree(二叉搜索树)原理及C++代码实现

    BST是一类用途极广的数据结构.它有如下性质:设x是二叉搜索树内的一个结点.如果y是x左子树中的一个结点,那么y.key<=x.key.如果y是x右子树中的一个结点,那么y.key>=x. ...

随机推荐

  1. easyui DataGrid 工具类之 列属性class

    public class ColumnVO { /**     * 列标题文本     */    private String title; /**     * 列字段名称     */    pr ...

  2. LeetCode算法题解

    1.给定两个正整数(二进制形式表示)A和B,问把A变为B需要改变多少位(bit)?也就是说,整数A和B的二进制表示中有多少位是不同的?(181) 解法一:举例说明,为了减少复杂度,就使用八位二进制吧. ...

  3. Response.Clear()和Response.ClearContent()区别

    Response.Clear()方法 Clear方法删除所有缓存中的HTML输出.但此方法只删除Response显示输入信息,不删除Response头信息. Response.ClearContent ...

  4. NSDate获取当前时区的时间

    [NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...

  5. sublime 自动编译

    Tools --> Build System --> New: { "shell_cmd": "cc.bat \"$file\"" ...

  6. Java类实例化时候的加载顺序

    面试试题中经常考到此问题,现在做进一步的总结: public class Student { public Student(String name){ System.out.println(name) ...

  7. PyCharm配置GitHub

    原文出处: https://github.com/wssnail/ws96apt/blob/master/weixin/a.py#L21-21打开file,选择settings,找到Version C ...

  8. linux 下安装安装rz/sz命令

    一.软件安装 root 账号登陆后,依次执行以下命令: cd /tmp wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz http:/ ...

  9. Python之路 day1 用户登录多次被锁定

    编写登陆接口: 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 #Author:ersa import getpass,os,sys #读取账户信息到内存中 try: accounts_fil ...

  10. angular router-ui

    将模块注入到控制器中的方法: 1.export module 2.在router中resolve解决: 2.1 resolve中直接return值 /*ngInject*/ worker : 'hi' ...