目录
一、基本介绍
- 二叉排序树(BST),任何一个非叶子结点的值,要大于其左子结点的值,小于其右子结点的值
二、二叉排序树的建立
2.1 思路分析
- 定义结点类,设置结点的相关属性
- 定义向二叉排序树添加结点的方法,如何实现该方法呢?
A:若传入结点为空,则直接返回
B:若传入的结点不为空,则判断传入结点(node)的值和当前子树根结点的值的大小关系
a:若node.value < this.value,若左子树不存在,直接将node挂到this的左子树,若存在,则继续比较
b:若node.value > this.value,若右子树不存在,直接将node挂到this的右子树,若存在,则继续比较
- 定义结点的中序遍历方法
- 定义二叉排序树类,定义根结点、生成二叉排序树的方法以及中序遍历的方法,实际上是利用结点的方法
2.2 代码实现
package cn.itcast_02;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = { 7, 3, 10, 12, 5, 1, 9 };
BinarySortTree sortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++) {
sortTree.add(new Node(arr[i]));
}
sortTree.infixOrder();
}
}
/**
* 二叉排序树类
*/
class BinarySortTree {
private Node root;
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
// 中序遍历
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉排序树为空!");
}
}
}
/**
* 这个是结点类
*
* @author Administrator
*
*/
class Node {
int value;
Node left;
Node right;
public Node(int value) {
super();
this.value = value;
}
/**
* 该方法用来添加结点至二叉排序树
*
* @param node
*/
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 infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}
三、二叉排序树删除结点
3.1 待删除的结点是叶子结点
-
思路分析
3.2 待删除的结点只有一棵子树
-
思路分析
- 待删除结点是根结点的情况比较简单,就不演示了。
3.3 待删除的结点有两棵子树
-
思路分析
3.4 二叉树只有一个结点的情况
- 这个时候直接将该结点置空,并且返回终止程序
四、代码实现
package cn.itcast_02;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = { 7, 3, 10, 12, 5, 1, 9, 2 };
BinarySortTree sortTree = new BinarySortTree();
// 循环添加结点到二叉排序树
for (int i = 0; i < arr.length; i++) {
sortTree.add(new Node(arr[i]));
}
// 中序遍历二叉树
// sortTree.infixOrder();
// 测试删除叶子结点
// sortTree.delNode(12);
// 测试删除只有一颗子树的结点
// sortTree.delNode(1);
// 测试删除有两棵子树的结点
sortTree.delNode(7);
System.out.println("删除结点后:");
sortTree.infixOrder();
}
}
/**
* 二叉排序树类
*/
class BinarySortTree {
private Node root;
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
// 中序遍历
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉排序树为空!");
}
}
// 查找要删除的结点
public Node search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
// 查找待删除结点的父结点
public Node searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}
/**
* 该方法用来查找右子树中结点的最小值,返回后删除
*
* @param node
* 右子树的根结点
* @return 返回该右子树中结点的最小值
*/
public int delRightTree(Node node) {
Node target = node;
// 查找该树的左结点
while (target.left != null) {
target = target.left;
}
delNode(target.value);
return target.value;
}
/**
* 该方法用来删除结点
*
* @param value
*/
public void delNode(int value) {
if (root == null) {
return;
} else {
Node targetNode = search(value);
// 没有要删除的结点
if (targetNode == null) {
return;
}
// 二叉树只有一个结点
if (root.left == null && root.right == null) {
root = null;
return;
}
// 查找targetNode的父结点
Node parent = searchParent(value);
// 待删除结点是叶子结点的情况
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
} else if (parent.right != null && parent.right.value == value) {
parent.right = null;
}
// 待删除的结点有两棵子树的情况
} else if (targetNode.left != null && targetNode.right != null) {
// 返回右子树最小值并重置待删除
int minValue = delRightTree(targetNode.right);
targetNode.value = minValue;
} else {
if (parent != null) {
// 待删除结点只有一颗子树的情况
// 待删除的结点有左子结点,有2种情况
if (targetNode.left != null) {
if (parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
// 待删除的结点有右子结点,有2种情况
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
}
} else {
// 根结点只有左子树
if (targetNode.left != null) {
targetNode.value = targetNode.left.value;
targetNode.left = null;
} else {
// 根结点只有右子树
targetNode.value = targetNode.right.value;
targetNode.right = null;
}
}
}
}
}
}
/**
* 这个是结点类
*
* @author Administrator
*
*/
class Node {
int value;
Node left;
Node right;
public Node(int value) {
super();
this.value = value;
}
/**
* 该方法用来添加结点至二叉排序树
*
* @param node
*/
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 infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
/**
* 查找待删除的结点
*
* @param value
* 待删除结点的值
* @return 找到返回结点,否则返回null
*/
public Node search(int value) {
// 找到了待删除的结点
if (value == this.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);
}
}
/**
* 查找待删除结点的父结点
*
* @param value
* 待删除结点的值
* @return 返回待删除结点的父结点,没有则返回null
*/
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 {
return null;
}
}
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}