二叉排序树
1 先看一个需求
给你一个数列 (7, 3, 10, 12, 5, 1, 9),要求能够高效的完成对数据的查询和添加
 
2 解决方案分析
 使用数组
数组未排序, 优点:直接在数组尾添加,速度快。 缺点:查找速度慢. 
数组排序,优点:可以使用二分查找,查找速度快,缺点:为了保证数组有序,在添加新数据时,找到插入位
置后,后面的数据需整体移动,速度慢。
 使用链式存储-链表
不管链表是否有序,查找速度都慢,添加数据速度比数组快,不需要数据整体移动。
 使用二叉排序树
 
3 二叉排序树介绍
  二叉排序树:BST: (Binary Sort(Search) Tree), 对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当
前节点的值小,右子节点的值比当前节点的值大。
  特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点
  比如针对前面的数据 (7, 3, 10, 12, 5, 1, 9) ,对应的二叉排序树为:

4 二叉排序树创建和遍历
一个数组创建成对应的二叉排序树,并使用中序遍历二叉排序树,比如: 数组为 Array(7, 3, 10, 12, 5, 1, 9) , 创
建成对应的二叉排序树为 : 

5 二叉排序树的删除
二叉排序树的删除情况比较复杂,有下面三种情况需要考虑
1) 删除叶子节点 (比如:2, 5, 9, 12)
2) 删除只有一颗子树的节点 (比如:1)
3) 删除有两颗子树的节点. (比如:7, 3,10 )
4) 操作的思路分析

对删除结点的各种情况的思路分析:
第一种情况:
删除叶子节点 (比如:2, 5, 9, 12)
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 确定 targetNode 是 parent 的左子结点 还是右子结点
(4) 根据前面的情况来对应删除
左子结点 parent.left = null
右子结点 parent.right = null;
第二种情况: 删除只有一颗子树的节点 比如 1
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 确定 targetNode 的子结点是左子结点还是右子结点
(4) targetNode 是 parent 的左子结点还是右子结点
(5) 如果 targetNode 有左子结点
(5). 1 如果 targetNode 是 parent 的左子结点
parent.left = targetNode.left;
(5).2 如果 targetNode 是 parent 的右子结点
parent.right = targetNode.left;
(6) 如果 targetNode 有右子结点
(6).1 如果 targetNode 是 parent 的左子结点
parent.left = targetNode.right;
(6).2 如果 targetNode 是 parent 的右子结点
parent.right = targetNode.righ
情况三 : 删除有两颗子树的节点. (比如:7, 3,10 )
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 从 targetNode 的右子树找到最小的结点
(4) 用一个临时变量,将 最小结点的值保存 temp = 11
(5) 删除该最小结点
(6) targetNode.value = temp
 
6 二叉排序树删除结点的代码实现
  1. package com.lin.binarysorttree_0314;
  2.  
  3. public class BinarySortTreeTest {
  4.  
  5. public static void main(String[] args) {
  6. int[] arr = {7, 3, 10, 12, 5, 1, 9};
  7. BinarySortTree binarySortTree = new BinarySortTree();
  8. for (int i = 0; i < arr.length; i++) {
  9. binarySortTree.add(new SNode(arr[i]));
  10. }
  11.  
  12. binarySortTree.add(new SNode(2));
  13. binarySortTree.infixOrder();
  14.  
  15. // 删除
  16. System.out.println("***********");
  17.  
  18. binarySortTree.delNode(2);
  19. binarySortTree.delNode(3);
  20. binarySortTree.delNode(5);
  21. binarySortTree.delNode(7);
  22. binarySortTree.delNode(9);
  23. binarySortTree.delNode(12);
  24. System.out.println("root:" + binarySortTree.getRoot());
  25.  
  26. binarySortTree.infixOrder();
  27. }
  28. }
  29.  
  30. class BinarySortTree{
  31. private SNode root;
  32. // 查找要删除的节点
  33. public SNode getRoot() {
  34. return root;
  35. }
  36. public SNode searchDelNode(int value) {
  37. if(root == null) {
  38. return null;
  39. } else {
  40. return root.searchDelNode(value);
  41. }
  42. }
  43. // 查找要删除节点的父节点
  44. public SNode searchParent(int value) {
  45. if(root == null) {
  46. return null;
  47. } else {
  48. return root.searchParent(value);
  49. }
  50. }
  51. /**
  52. * @param node 传入的节点(当作二叉排序树的根节点)
  53. * @return 返回的以node为根节点的二叉排序树的最小节点的值
  54. */
  55. public int delRightTreeMin(SNode node) {
  56. SNode target = node;
  57. // 循环地查找左节点,就会找到最小值
  58. while(target.left != null) {
  59. target = target.left;
  60. }
  61. delNode(target.value);// !!!!
  62. return target.value;// !!!!!
  63. }
  64.  
  65. // 删除节点
  66. public void delNode(int value) {
  67. if(root == null) {
  68. return;
  69. } else {
  70. // 找删除节点
  71. SNode targetNode = searchDelNode(value);
  72. // 没有找到
  73. if(targetNode == null) {
  74. return;
  75. }
  76. // 如果发现当前这棵二叉树只有一个节点
  77. if(root.left == null && root.right == null) {
  78. root = null;
  79. return;
  80. }
  81. // 去找到targetNode的父节点
  82. SNode parent = searchParent(value);
  83. // 如果删除的节点是叶子节点
  84. if(targetNode.left == null && targetNode.right == null) {
  85. // 判断targetNode是父节点的左子节点还是右子节点
  86. if(parent.left != null && parent.left.value == value) {
  87. parent.left = null;
  88. } else if(parent.right != null && parent.right.value == value) {
  89. parent.right = null;
  90. }
  91. } else if(targetNode.left != null && targetNode.right != null) { // 有左右子节点
  92. int delRightTreeMin = delRightTreeMin(targetNode.right);
  93. targetNode.value = delRightTreeMin;
  94. } else {// 只有一个子节点
  95. // 要删除的节点只有左节点
  96. if(targetNode.left != null) {
  97. if(parent != null) {
  98. // 如果targetNode是parent的左子节点
  99. if(parent.left.value == value) {
  100. parent.left = targetNode.left;
  101. } else {
  102. parent.right = targetNode.left;
  103. }
  104. } else {
  105. root = targetNode.left;
  106. }
  107. } else {// 要删除的节点有右子节点
  108. if(parent != null) {
  109. if(parent.left.value == value) {
  110. parent.left = targetNode.right;
  111. } else {
  112. parent.right = targetNode.right;
  113. }
  114. } else {
  115. root = targetNode.right;
  116. }
  117. }
  118. }
  119.  
  120. }
  121. }
  122. // 中序遍历
  123. public void infixOrder() {
  124. if(root == null) {
  125. System.out.println("空树!");
  126. } else {
  127. root.infixOrder();
  128. }
  129. }
  130. // 添加
  131. public void add(SNode node) {
  132. if(root == null) {
  133. root = node;
  134. } else {
  135. root.add(node);
  136. }
  137. }
  138. }
  139.  
  140. class SNode{
  141. protected int value;
  142. protected SNode left;
  143. protected SNode right;
  144.  
  145. public SNode(int value) {
  146. // TODO Auto-generated constructor stub
  147. this.value = value;
  148. }
  149.  
  150. @Override
  151. public String toString() {
  152. // TODO Auto-generated method stub
  153. return "Node = [value = " + value + "]";
  154. }
  155.  
  156. // 添加节点
  157. public void add(SNode node) {
  158. if(node == null) {
  159. return;
  160. }
  161. if(node.value < this.value) {
  162. if(this.left == null) {
  163. this.left = node;
  164. } else {
  165. this.left.add(node);
  166. }
  167. } else {
  168. if(this.right == null) {
  169. this.right = node;
  170. } else {
  171. this.right.add(node);
  172. }
  173. }
  174. }
  175. // 中序遍历
  176. public void infixOrder() {
  177. if(this.left != null) {
  178. this.left.infixOrder();
  179. }
  180. System.out.println(this);
  181. if(this.right != null) {
  182. this.right.infixOrder();
  183. }
  184. }
  185. // 查找要删除的节点
  186. public SNode searchDelNode(int value) {
  187. if(this.value == value) {
  188. return this;
  189. } else if(this.value > value) {
  190. // 如果左子节点为空
  191. if(this.left == null) {
  192. return null;
  193. }
  194. return this.left.searchDelNode(value);
  195. } else {
  196. if(this.right == null) {
  197. return null;
  198. }
  199. return this.right.searchDelNode(value);
  200. }
  201. }
  202. // 查找要删除节点的父节点, 如果没有则返回null
  203. public SNode searchParent(int value) {
  204. if(( this.left != null && this.left.value == value)
  205. || ( this.right != null && this.right.value == value )) {
  206. return this;
  207. } else {
  208. // 如果查找的值小于当前节点的值,并且当前节点的左子节点不为空
  209. if(value < this.value && this.left != null) {
  210. return this.left.searchParent(value);
  211. } else if(value >= this.value && this.right != null) {
  212. return this.right.searchParent(value);
  213. } else {
  214. return null;
  215. }
  216. }
  217. }
  218.  
  219. }

仅供参考,有错误还请指出!

有什么想法,评论区留言,互相指教指教。

觉得不错的可以点一下右边的推荐哟

Java 树结构实际应用 三(二叉排序树)的更多相关文章

  1. Java 处理 XML 的三种主流技术及介绍

    Java 处理 XML 的三种主流技术及介绍 原文地址:https://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Ma ...

  2. java解析xml的三种方法

    java解析XML的三种方法 1.SAX事件解析 package com.wzh.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXE ...

  3. 20145213《Java程序设计》实验三敏捷开发与XP实践

    20145213<Java程序设计>实验三敏捷开发与XP实践 实验要求 1.XP基础 2.XP核心实践 3.相关工具 实验内容 1.敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法 ...

  4. 20145213《Java程序设计》第三周学习总结

    20145213<Java程序设计>第三周学习总结 教材学习内容总结 正所谓距离产生美,上周我还倾心于Java表面的基础语法.其简单的流程结构,屈指可数的基本类型分类,早已烂熟于心的运算符 ...

  5. 20145206《Java程序设计》实验三实验报告

    20145206<Java程序设计>实验三实验报告 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运 ...

  6. 20145308刘昊阳 《Java程序设计》实验三 敏捷开发与XP实践 实验报告

    20145308刘昊阳 <Java程序设计>实验三 敏捷开发与XP实践 实验报告 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 统计的PSP(Personal ...

  7. 20145330《Java程序设计》第三周学习总结

    20145330 <Java程序设计>第三周学习总结 第三周知识的难度已经逐步上升,并且一周学习两章学习压力也逐渐加大,需要更高效率的来完成学习内容,合理安排时间. 类与对象 对象(Obj ...

  8. 20145337《Java程序设计》第三周学习总结

    20145337 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象 类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象是类的实例.我觉得在视频中对类与对象关系的描 ...

  9. 20145320《Java程序设计》第三次实验报告

    20145320<Java程序设计>第三次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.22 15: ...

随机推荐

  1. select(),fd_set(),fd_isset()

    1. select函数 1. 用途 在编程的过程中,经常会遇到许多阻塞的函数,好像read和网络编程时使用的recv, recvfrom函数都是阻塞的函数,当函数不能成功执行的时候,程序就会一直阻塞在 ...

  2. POJ - 3665 icow

    Fatigued by the endless toils of farming, Farmer John has decided to try his hand in the MP3 player ...

  3. HDU 4336 Card Collector(状压 + 概率DP 期望)题解

    题意:每包干脆面可能开出卡或者什么都没有,一共n种卡,每种卡每包爆率pi,问收齐n种卡的期望 思路:期望求解公式为:$E(x) = \sum_{i=1}^{k}pi * xi + (1 - \sum_ ...

  4. React Refs All In One

    React Refs All In One https://reactjs.org/docs/react-api.html#refs Ref https://reactjs.org/docs/refs ...

  5. 如何配置 webpack 支持 preload, prefetch, dns-prefetch

    如何配置 webpack 支持 preload, prefetch, dns-prefetch webpack , preload, prefetch https://webpack.js.org/p ...

  6. React Native Apps

    React Native Apps https://github.com/ReactNativeNews/React-Native-Apps github app https://github.com ...

  7. taro defaultProps

    taro defaultProps https://nervjs.github.io/taro/docs/best-practice.html#给组件设置-defaultprops import Ta ...

  8. py django

    创建项目 $ django-admin startproject server 运行项目 $ cd server $ python manage.py runserver 创建一个模块 $ pytho ...

  9. vue农历日历

    <template> <div class="calendar-main"> <div class="choose_year"&g ...

  10. c++ string与wstring转换

    wchar_t to char #include <comdef.h> const wchar_t* exepath = L"d:\\中文 路径\\中文 路径.exe" ...