Java--算法--二叉排序树(BST)
时间:2021-07-16
本文章向大家介绍Java--算法--二叉排序树(BST),主要包括Java--算法--二叉排序树(BST)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
- 二叉排序树的基本介绍:
-
二叉排序树的遍历和创建
-
package com.model.tree; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/16 9:34 * 二叉排序树的创建与遍历(Binary Sort tree) */ public class TreeDemo08 { public static void main(String[] args) { BinarySortTree sortTree = new BinarySortTree(); SortNode root = new SortNode(0); SortNode node1 = new SortNode(1); SortNode node2 = new SortNode(2); SortNode node3 = new SortNode(3); sortTree.setRoot(root); sortTree.add(node1); sortTree.add(node2); sortTree.add(node3); sortTree.infixOrder(); } } class BinarySortTree{ private SortNode root; public void add(SortNode node){ if (root!=null) { root.add(node); }else { root=node; } } public void infixOrder(){ if (root!=null) { root.infixOrder(); }else { System.out.println("树为空"); } } public SortNode getRoot() { return root; } public void setRoot(SortNode root) { this.root = root; } } class SortNode{ private int value; private SortNode left; private SortNode right; public int getValue() { return value; } // 添加 public void add(SortNode node){ if (node==null){ return; } if (this.value< node.value){ if (this.right==null){ this.right= node; }else { this.right.add(node); } }else { if (this.left==null){ this.left=node; }else { this.left.add(node); } } } // 中序遍历 public void infixOrder(){ if (this==null){ return; }else { if (this.left!=null){ this.left.infixOrder(); } System.out.println(this.toString()); if (this.right!=null){ this.right.infixOrder(); } } } @Override public String toString() { return "SortNode{" + "value=" + value + '}'; } public void setValue(int value) { this.value = value; } public SortNode getLeft() { return left; } public void setLeft(SortNode left) { this.left = left; } public SortNode getRight() { return right; } public void setRight(SortNode right) { this.right = right; } public SortNode() { } public SortNode(int value) { this.value = value; } }
-
- 二叉排序树的节点删除:
-
package com.model.tree; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/16 9:34 * 二叉排序树的创建与遍历(Binary Sort tree) */ public class TreeDemo08 { public static void main(String[] args) { BinarySortTree sortTree = new BinarySortTree(); // SortNode root = new SortNode(0); // SortNode node1 = new SortNode(1); // SortNode node2 = new SortNode(2); // SortNode node3 = new SortNode(3); // SortNode node4 = new SortNode(-1); // SortNode node5 = new SortNode(-2); // sortTree.setRoot(root); // sortTree.add(node2); // sortTree.add(node1); // sortTree.add(node3); // sortTree.add(node4); // sortTree.add(node5); // sortTree.infixOrder(); // sortTree.delete(1); // System.out.println("-----------------------"); // sortTree.infixOrder(); int[] array={7,3,10,12,5,1,9,2}; for (int i = 0; i < array.length; i++) { sortTree.add(new SortNode(array[i])); } sortTree.infixOrder(); System.out.println("------------------"); sortTree.delete(2); sortTree.infixOrder(); } } class BinarySortTree { private SortNode root; public void add(SortNode node) { if (root != null) { root.add(node); } else { root = node; } } public void infixOrder() { if (root != null) { root.infixOrder(); } else { System.out.println("树为空"); } } public SortNode searchParent(int value) { if (root == null) { return null; } else { if (root.getValue() == value) { return null; } else { return root.searchParent(value); } } } public SortNode search(int value) { if (root == null) { return null; } else { if (root.getValue() == value) { return root; } else { return root.search(value); } } } public void delete(int value){ if (root==null){ System.out.println("树为空,无法进行删除"); return; }else { SortNode delNode = search(value); SortNode parentNode = searchParent(value); if (delNode!=null&&parentNode!=null){//要删除的节点和他的父节点都存在 if (parentNode.getLeft()==delNode){//要删除的节点为父节点的左节点 if (delNode.getLeft()==null&&delNode.getRight()==null){//要删除节点的为叶子节点 parentNode.setLeft(null); }else if (delNode.getLeft()==null&&delNode.getRight()!=null) { parentNode.setLeft(delNode.getRight()); }else if (delNode.getLeft()!=null&&delNode.getRight()==null){ parentNode.setLeft(delNode.getLeft()); }else {//左右子树都不为空,从右子树中找到最小的一个放在要删除的节点行即可 int temp = delNode.getRight().searchMin().getValue(); delete(temp); delNode.setValue(temp); } }else {//要删除的节点为父节点的右节点 if (delNode.getLeft()==null&&delNode.getRight()==null){//要删除节点的为叶子节点 parentNode.setRight(null); }else if (delNode.getLeft()==null&&delNode.getRight()!=null) { parentNode.setRight(delNode.getRight()); }else if (delNode.getLeft()!=null&&delNode.getRight()==null){ parentNode.setRight(delNode.getLeft()); }else {//左右子树都不为空,从右子树中找到最小的一个放在要删除的节点行即可 int temp = delNode.getRight().searchMin().getValue(); delete(temp); delNode.setValue(temp); } } }else if (parentNode==null&&delNode==root){//要删除的节点存在,他的父节点不存在,接为 root节点 if (delNode.getLeft()==null&&delNode.getRight()==null){//要删除节点的为叶子节点 root=null; }else if (delNode.getLeft()==null&&delNode.getRight()!=null) { root=delNode.getRight(); }else if (delNode.getLeft()!=null&&delNode.getRight()==null){ root=delNode.getLeft(); }else {//左右子树都不为空,从右子树中找到最小的一个放在要删除的节点行即可 int temp = delNode.getRight().searchMin().getValue(); delete(temp); root.setValue(temp); } }else {//两个节点都为空 System.out.println("没有此节点,无法删除"); return; } } } public SortNode getRoot() { return root; } public void setRoot(SortNode root) { this.root = root; } } class SortNode { private int value; private SortNode left; private SortNode right; public void delete(int value){ } // 找到树的最小值 public SortNode searchMin(){ if (this.left==null){ return this; }else { return this.left.searchMin(); } } // 查找某个节点的父节点 public SortNode searchParent(int value) { if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) { return this; } else { if (this.value <= value && this.right != null) { return this.right.searchParent(value); } else if (this.value > value && this.left != null) { return this.left.searchParent(value); } else { return null; } } } public SortNode searchParent02(int value) { if (this.left != null) { if (this.left.value == value) { return this; } else { if (this.value > value) { return this.left.search(value); } else { return null; } } } else if (this.right != null) { if (this.right.value == value) { return this; } else { if (this.value <= value) { return this.right.search(value); } else { return null; } } } else { return null; } } // 通过 某个值查找某个节点 public SortNode search(int value) { if (value == this.value) { return this; } else { if (this.value > value) { if (this.left == null) { return null; } else { return this.left.search(value); } } else { if (this.right == null) { return null; } else { return this.right.search(value); } } } } // 添加 public void add(SortNode node) { if (node == null) { return; } if (this.value < node.value) { if (this.right == null) { this.right = node; } else { this.right.add(node); } } else { if (this.left == null) { this.left = node; } else { this.left.add(node); } } } // 中序遍历 public void infixOrder() { if (this == null) { return; } else { if (this.left != null) { this.left.infixOrder(); } System.out.println(this.toString()); if (this.right != null) { this.right.infixOrder(); } } } @Override public String toString() { return "SortNode{" + "value=" + value + '}'; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public SortNode getLeft() { return left; } public void setLeft(SortNode left) { this.left = left; } public SortNode getRight() { return right; } public void setRight(SortNode right) { this.right = right; } public SortNode() { } public SortNode(int value) { this.value = value; } }
原文地址:https://www.cnblogs.com/zzhAylm/p/15019328.html
- 55. 上传文件(Web版) | 厚土Go学习笔记
- R语言与机器学习学习笔记(分类算法
- 54. 心跳的实现 | 厚土Go学习笔记
- 通过错误的sql来测试推理sql的解析过程(二) (r8笔记第7天)
- 53. Socket服务三次握手的示例 | 厚土Go学习笔记
- R分词继续,"不|知道|你在|说|什么"分词添加新词
- Java开发Spring第一天
- 关于R安装中文分词包安装不上的问题install.packages("tm")
- dataguard备库的数据文件的迁移实战(r8笔记第24天)
- Hive的left join、left outer join和left semi join三者的区别
- 52. Socket Server 自定义协议的简单实现 | 厚土Go学习笔记
- dataguard备库的数据文件的迁移(r8笔记第22天)
- 46. 实现一个简单的网络爬虫 | 厚土Go学习笔记
- 45. sync.Mutex 互斥和互斥锁 | 厚土Go学习笔记
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释