博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构——二叉排序树(Java代码实现)
阅读量:3961 次
发布时间:2019-05-24

本文共 4841 字,大约阅读时间需要 16 分钟。

二叉排序树: BST: (Binary Sort(Search) Tree),对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。

特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点
在这里插入图片描述
创建二叉树添加节点构建排序树且遍历

首先需要构建节点类,存放单个数据。节点类当中实现添加节点的方法和遍历方法。在二叉树类当中根据root节点进行添加和遍历。

class BSTree {
private Node root; public void add(Node node) {
if (root == null) {
root = node; } else {
root.add(node); } } public void DLR() {
if (root != null) {
root.DLR(); } else {
System.out.println("树空,无法遍历"); } }}class Node {
int value; Node left; Node right; public Node(int value) {
this.value = value; } // 添加节点 public void add(Node node) {
if (node == null) {
return; } // 当前传入节点和子树节点的关系 if (node.value < this.value) {
if (this.left == null) {
this.left = node; } else {
this.left.add(node); } } else {
if (this.right == null) {
this.right = node; } else {
this.right.add(node); } } } // 前序遍历 public void DLR() {
System.out.println(this); if (this.left != null) {
this.left.DLR(); } if (this.right != null) {
this.right.DLR(); } } @Override public String toString() {
return "Node[value = " + value + "]"; }}

对代码进行测试:使用上图的数组进行测试,在构建好了的二叉排序树如上图,其前序遍历的结果是:10,5,2,4,6,8,9,15

public class BST {
public static void main(String[] args) {
int[] arr = {
10, 5, 6, 2, 4, 8, 9, 15 }; BSTree bst = new BSTree(); for (int i = 0; i < arr.length; i++) {
bst.add(new Node(arr[i])); } bst.DLR(); }}

运行记过如下图:

在这里插入图片描述
二叉树删除节点

在删除节点的时候有三种情况:

  1. 删除叶子节点
    1. 找到要删除的节点
    2. 直接删除节点,把父节点的next指向空(判断next是左右的那个指针)
  2. 删除只有一棵子树的节点
  3. 删除有俩棵子树的节点

代码实现:在node节点类当中添加俩个方法:

// 找到目标节点	public Node search(int value) {
if (this.value == value) {
return this; } else if (value < this.value) {
if (this.left == null) {
return null; } return this.left.search(value); } else {
if (this.right == null) {
return null; } return this.right.search(value); } } // 找到目标节点的父节点 public Node searchParent(int value) {
if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
return this; } else {
if (value < this.value && this.left != null) {
return this.left.searchParent(value); } else if (value >= this.value && this.right != null) {
return this.right.searchParent(value); } else {
System.out.println("该节点没有父节点!"); return null; } } }

然后在BSTree类当中编写delete删除的方法。以及node节点类当中的方法进行调用,和一个查找最小值的方法,

public Node search(int value) {
if (root == null) {
System.out.println("空树"); return null; } else {
return root.search(value); } } public Node searchParent(int value) {
if (root == null) {
return null; } else {
System.out.println("父节点:" + root.searchParent(value)); return root.searchParent(value); } } public void delete(int value) {
if (root == null) {
return; } else {
Node target = search(value); // 是否找到目标节点 if (target == null) {
return; } // 只有一个节点,直接删除这个节点 if (root.left == null && root.right == null) {
root = null; } // 查找目标节点的父节点 Node targetParent = searchParent(value); // 删叶子节点 if (target.left == null && target.right == null) {
// 判断目标节点是父节点的左右? if (targetParent.left != null && targetParent.left.value == value) {
targetParent.left = null; } if (targetParent.right != null && targetParent.right.value == value) {
targetParent.right = null; } } else if (target.left != null && target.right != null) {
int minValue = deleteMin(target.right); target.value = minValue; } else {
// 目标节点有左子树 if (target.left != null) {
// 目标节点是父节点的左右? if (targetParent != null) {
if (targetParent.left.value == value) {
targetParent.left = target.left; } else {
targetParent.right = target.left; } } else {
root = target.left; } } else {
if (targetParent != null) {
if (targetParent.left.value == value) {
targetParent.left = target.right; } else {
targetParent.right = target.right; } } else {
root = target.right; } } } } } public int deleteMin(Node node) {
Node targetNode = node; // 循环查找左子树,会找到最小值 while (targetNode.left != null) {
targetNode = targetNode.left; // targetnode指向最小的值 } delete(targetNode.value); return targetNode.value; }

进行测试:分别删除不同情况的节点:

public static void main(String[] args) {
int[] arr = {
10, 5, 6, 2, 4, 8, 9, 15 }; BSTree bst = new BSTree(); for (int i = 0; i < arr.length; i++) {
bst.add(new Node(arr[i])); } // bst.searchParent(8); // bst.search(10); bst.DLR(); System.out.println("删除 :"); bst.delete(4); // 删叶子节点 => 10 5 2 6 8 9 15 bst.delete(6); // 删只有一棵子树的节点 10 5 2 8 9 15 bst.delete(5);// 删除有两棵子树的节点 10 8 2 9 15 bst.DLR(); }

如下图所示:

在这里插入图片描述

转载地址:http://igmzi.baihongyu.com/

你可能感兴趣的文章
JSP的运行内幕
查看>>
python超简单的web服务器
查看>>
代理模式、静态代理、动态代理、aop
查看>>
Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00
查看>>
大型Web2.0站点构建技术初探
查看>>
机器学习算法汇总:人工神经网络、深度学习及其它
查看>>
解决Spring中AOP不能切入Struts的DispatchAction方法的问题
查看>>
出国以后才知道英语应该怎么学
查看>>
计算机专业权威期刊投稿经验总结
查看>>
如何在三个月内学会一门外语?
查看>>
看看你对Linux到底了解多少?
查看>>
网上看到的:ARM入门最好的文章(转)
查看>>
中国最美情诗100句
查看>>
javascript注册window的onload事件问题研究
查看>>
客户端技术分页控件javascript+css,可用于任何服务器端技术
查看>>
学习Swing 的网站[转]
查看>>
Google App engine 的第一个应用 midispot
查看>>
提问的智慧
查看>>
关于dom4j无法解析xmlns问题及生成非UTF-8字符集乱码问题的解决
查看>>
很好的一篇文章 如果让我重做一次研究生 王汎森
查看>>