二分搜索树是一个二叉树,它的性质是:每个节点的值大于左孩子的值,而小于右孩子的值,因此一颗二分搜索树不需要是一颗完全二叉树.
这里我们从以下几个方面介绍二分搜索树:
- 节点类 BSTNode
- 属性
- 重要方法
- 深度优先遍历与广度优先遍历(前中后序遍历,层序遍历)
- 全部代码
一.节点类 BSTNode
这里使用了两种创建结点的方法,一个是直接通过key-value来创建一个新节点,另一个是通过一个原本存在的结点copy出一个一模一样的结点
二.属性
这个数只有两个属性:
三.重要方法
还是先把接口呈现出来,可以一目了然的明白重要的方法
之后是每个函数的详解:
//得到节点的数量int size();
//判空
boolean isEmpty();
//插入一个节点
void insert(int key, int value);
//判断树中是否包含key
boolean contain(int key);
//得到key对应的value
int search(int key);
//删除最小值
void removeMin();
//删除最大值
void removeMax();
//根据 key 删除节点
void removeNode(int key);
四.深度优先遍历与广度优先遍历
深度优先:
广度优先:
五.全部代码
接口类 IBSTree.java:
package com.tree;
public interface IBSTree {
//得到节点的数量
int size();
//判空
boolean isEmpty();
//插入一个节点
void insert(int key, int value);
//判断树中是否包含key
boolean contain(int key);
//得到key对应的value
int search(int key);
//删除最小值
void removeMin();
//删除最大值
void removeMax();
//根据 key 删除节点
void removeNode(int key);
}
实例类 BinarySearchIBSTree.java:(这里名字不规范,博主这样做是为了与用到二分搜索树的其他的算法区分)
package com.tree;
import java.util.concurrent.ArrayBlockingQueue;
/**
* 二分搜索树
* 性质:
* >>>每个节点的值大于左孩子的值
* >>>每个节点的值小于右孩子的值
*/
public class BinarySearchIBSTree implements IBSTree {
private BSTNode root; //根节点
private int count; //节点的数量
//---------------------------------------------------------------//
//得到 node 的个数
public int size() {
return count;
}
//---------------------------------------------------------------//
//判断tree是否为空
public boolean isEmpty() {
return count == 0;
}
//---------------------------------------------------------------//
public void insert(int key, int value) {
//非常巧妙地实时更新 root 节点
root = insert(root, key, value);
}
//在以 root 为根节点的二叉搜索树中根据键 key 来插入,如果 key 已经存在,则更新 value
//返回插入节点后的二叉搜索树的根节点
private BSTNode insert(BSTNode node, int key, int value) {
if (node == null) {
count++;
return new BSTNode(key, value);
}
if (key == node.key) {
node.value = value;
} else if (key < node.key) {
node.left = insert(node.left, key, value);
} else {
//key > node.key
node.right = insert(node.right, key, value);
}
return node;
}
//---------------------------------------------------------------//
/*查询二叉树是否包含指定 key 值*/
public boolean contain(int key) {
return contain(root, key);
}
private boolean contain(BSTNode node, int key) {
if (node == null) {
return false;
}
if (node.value == key) {
return true;
} else if (key > node.value) {
return contain(node.right, key);
} else {
return contain(node.left, key);
}
}
//---------------------------------------------------------------//
/*返回 key 对应的 value 值*/
public int search(int key) {
return search(root, key);
}
private int search(BSTNode node, int key) {
if (node == null) {
throw new NullPointerException("key is not valid");
}
if (node.key == key) {
return node.value;
} else if (node.key > key) {
return search(node.left, key);
} else {
return search(node.right, key);
}
}
//---------------------------------------------------------------//
//前序遍历
void preOrder(BSTNode node) {
if (node != null) {
System.out.println(node.value);
preOrder(node.left);
preOrder(node.right);
}
}
//中序遍历
void inOrder(BSTNode node) {
inOrder(node.left);
System.out.println(node.value);
inOrder(node.right);
}
//后序遍历
void postOrder(BSTNode node) {
preOrder(node.left);
preOrder(node.right);
System.out.println(node.value);
}
//层序遍历(广度优先遍历)
/* 需要一个队列,步骤如下:
* (入队根节点后,循环的执行直到队列为空)
* >>1.出队一个节点,得到这个节点
* >>2.入队这个节点的左孩子与右孩子(如果有孩子的话).*/
public void levelOrder() {
ArrayBlockingQueue<BSTNode> queue = new ArrayBlockingQueue<BSTNode>(count);
//这里使用offer()来加入元素,使用poll()来获取并移出元素
// 入队根节点
queue.offer(root);
//循环的执行直到队列为空
while (!queue.isEmpty()) {
BSTNode node = queue.poll();
System.out.println(node.value);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
//---------------------------------------------------------------//
//得到整个树的最小值的节点
public BSTNode getMinNode() {
if (count != 0) {
return getMinNode(root);
}
return null;
}
private BSTNode getMinNode(BSTNode node) {
if (node.left == null) {
return node;
}
return getMinNode(node.left);
}
//得到整个数的最大值的节点
public BSTNode getMaxNode() {
if (count != 0) {
return getMaxNode(root);
}
return null;
}
private BSTNode getMaxNode(BSTNode node) {
if (node.right == null) {
return node;
}
return getMaxNode(node.right);
}
//---------------------------------------------------------------//
//删除最小值所在的节点,并且返回根root
public void removeMin() {
if (root != null) {
root = removeMin(root);
}
count--;
}
private BSTNode removeMin(BSTNode node) {
if (node.left == null) {
BSTNode rightNode = node.right;
//todo why we can't use "return node.right"
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
//删除最大值所在的节点,并且返回根root
public void removeMax() {
if (root != null) {
root = removeMax(root);
}
count--;
}
private BSTNode removeMax(BSTNode node) {
if (node.right == null) {
BSTNode leftNode = node.left;
return leftNode;
}
node.right = removeMax(node.right);
return node;
}
//删除任意节点
/*
* 如果即将被删除的节点只有左孩子,那么就让右子树代替它(如果左右孩子都为空,也执行这个操作,无影响)
* 如果即将被删除的节点只有右孩子,那么就让左子树代替它
* 如果即将被删除的节点既有左孩子又有右孩子,那么就让右子树中的最小值代替它(1962,Hubbard Deletion算法)*/
public void removeNode(int key) {
removeNode(root, key);
count--;
}
private BSTNode removeNode(BSTNode node, int key) {
if (node == null) {
return null;
}
if (key < node.key) {
node.left = removeNode(node.left, key);
return node;
} else if (key > node.key) {
node.right = removeNode(node.right, key);
return node;
} else {
//现在找到 key 对应的 node 节点
if (node.left == null) {
//如果即将被删除的节点只有右孩子,那么就让左子树代替它
BSTNode right = node.right;
return right;
} else if (node.right == null) {
//如果即将被删除的节点只有左孩子,那么就让右子树代替它
BSTNode left = node.left;
return left;
} else {
//左右孩子都不为空,让右子树中的最小值代替它
BSTNode rightNode = new BSTNode(getMinNode(node.right)); //得到右子树中的最小值
//为更新后的节点的左右两个子树分别赋值吗
rightNode.left = node.left;
rightNode.right = removeMin(node.right);
return rightNode;
}
}
}
/*Node 节点类*/
private class BSTNode {
int key;
int value;
BSTNode left;
BSTNode right;
BSTNode(int key, int value) {
this.key = key;
this.value = value;
left = null;
right = null;
}
BSTNode(BSTNode node) {
this.key = node.key;
this.value = node.value;
left = node.left;
right = node.right;
}
}
}