1、定义
左子树节点的值小于根节点的值,右子树节点的值大于根节点的值
2、基本性质
设x是二叉搜索树中的一个结点。如果y是x左子树中的一个结点,那么y.key≤x.key。如果y是x右子树中的一个结点,那么y.key≥x.key。
在二叉搜索树中:
① 若任意结点的左子树不空,则左子树上所有结点的值均不大于它的根结点的值;
② 若任意结点的右子树不空,则右子树上所有结点的值均不小于它的根结点的值;
③ 任意结点的左、右子树也分别为二叉搜索树。
一棵典型的二叉搜索树如下:
3、二叉搜索树的基本操作
3.1插入
1.根节点为空,直接插入
2.如果根节点不为空,查找相应的位置进行插入
3.不能直接插入, 会破坏二叉搜索树, 只能在叶子结点进行插入
代码实现:
public boolean insert(int val) {
BSNode bsNode = new BSNode(val);
if(root == null) {
root = bsNode;
return true;
}
BSNode cur = root;
BSNode parent = null;
while(cur != null){
if(cur.val < val){
parent = cur;
cur = cur.right;
}else if(cur.val > val){
parent = cur;
cur = cur.left;
}else{
return false;
}
}
if(parent.val < val){
parent.right = bsNode;
}else{
parent.left = bsNode;
}
return true;
}
3.2查找
查找某一节点的值时,可以根据二叉搜索树的特性,从根节点开始查找,小于走左子树,大于走右子树
代码实现:
public BSNode search(int val) {
if(root == null) return null;
BSNode cur = root;
while(cur != null){
if(cur.val == val){
return cur;
}
if(cur.val < val){
cur = cur.right;
}
if(cur.val > val){
cur = cur.left;
}
}
return null;
}
3.3遍历
采用递归的思想进行遍历
代码实现:
public void inOrder(BSNode cur){
if(cur == null){
return;
}
inOrder(cur.left);
System.out.print(cur.val+" ");
inOrder(cur.right);
}
3.4删除
对二叉树的节点进行删除时,产生的情况比较多,我们可以分情况讨论,这样清晰明了
设待删除结点为 cur, 待删除结点的双亲结点为 parent
1.cur.left == null
- cur 是 root,则 root = cur.right
- cur 不是 root,cur 是 parent.left,则 parent.left = cur.right
- cur 不是 root,cur 是 parent.right,则 parent.right = cur.right
2.cur.right == null
- cur 是 root,则 root = cur.left
- cur 不是 root,cur 是 parent.left,则 parent.left = cur.left
- cur 不是 root,cur 是 parent.right,则 parent.right = cur.left
3.cur.left != null && cur.right != null
等效替换法:需要使用替换法进行删除,即在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被
删除节点中,再来处理该结点的删除问题
代码实现:
public void remove(int val) {
if(root == null){
return;
}
BSNode cur = root;
BSNode parent = null;
while(cur != null){
if(cur.val == val){
removeNode(parent,cur,val);
break;
}else if(cur.val < val){
parent = cur;
cur = cur.right;
}else{
parent = cur;
cur = cur.left;
}
}
}
//删除具体实现
public void removeNode(BSNode parent,BSNode cur,int val){
if(cur.left == null){
if(cur == root){
root = root.right;
}else if(cur == parent.left){
parent.left = cur.right;
}else{
parent.right = cur.right;
}
}else if(cur.right == null){
if(cur == root){
root = root.left;
}else if(cur == parent.left){
parent.left = cur.left;
}else{
parent.right = cur.left;
}
}else{
BSNode targetParent = cur;
BSNode target = cur.right;
while(target.left != null){
targetParent = target;
target = target.left;
}
cur.val = target.val;
if(target == targetParent.left){
targetParent.left = target.right;
}else{
cur.right = target.right;
}
}
}
4、性能分析
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。
(1)最优情况下,二叉搜索树为完全二叉树,其平均比较次数为:O(logN)
(2)最差情况下,二叉搜索树退化为单支树,其平均比较次数为:O(N)
5、完整代码
package demo03;
import java.util.LinkedList;
class BinarySearchTree {
//二叉树的节点
static class BSNode {
public int val;
public BSNode left;
public BSNode right;
public BSNode(int val) {
this.val = val;
}
}
//根节点初始化为空
public BSNode root = null;
//二叉搜索树的中序遍历
public void inOrder(BSNode cur){
if(cur == null){
return;
}
inOrder(cur.left);
System.out.print(cur.val+" ");
inOrder(cur.right);
}
//二叉搜索树的层次遍历
public void levelOrder(BSNode root){
BSNode cur = root;
if(cur == null){
return;
}
LinkedList<BSNode> list = new LinkedList<>();
list.add(cur);
while(!list.isEmpty()){
BSNode dan = list.poll();
System.out.print(dan.val+" ");
if(dan.left != null){
list.offer(dan.left);
}
if(dan.right != null){
list.offer(dan.right);
}
}
}
//二叉搜索树的查找
public BSNode search(int val) {
if(root == null) return null;
BSNode cur = root;
while(cur != null){
if(cur.val == val){
return cur;
}
if(cur.val < val){
cur = cur.right;
}
if(cur.val > val){
cur = cur.left;
}
}
return null;
}
//二叉搜索树的插入
public boolean insert(int val) {
BSNode bsNode = new BSNode(val);
if(root == null) {
root = bsNode;
return true;
}
BSNode cur = root;
BSNode parent = null;
while(cur != null){
if(cur.val < val){
parent = cur;
cur = cur.right;
}else if(cur.val > val){
parent = cur;
cur = cur.left;
}else{
return false;
}
}
if(parent.val < val){
parent.right = bsNode;
}else{
parent.left = bsNode;
}
return true;
}
//二叉搜索树的删除
public void remove(int val) {
if(root == null){
return;
}
BSNode cur = root;
BSNode parent = null;
while(cur != null){
if(cur.val == val){
removeNode(parent,cur,val);
break;
}else if(cur.val < val){
parent = cur;
cur = cur.right;
}else{
parent = cur;
cur = cur.left;
}
}
}
//删除具体实现
public void removeNode(BSNode parent,BSNode cur,int val){
if(cur.left == null){
if(cur == root){
root = root.right;
}else if(cur == parent.left){
parent.left = cur.right;
}else{
parent.right = cur.right;
}
}else if(cur.right == null){
if(cur == root){
root = root.left;
}else if(cur == parent.left){
parent.left = cur.left;
}else{
parent.right = cur.left;
}
}else{
BSNode targetParent = cur;
BSNode target = cur.right;
while(target.left != null){
targetParent = target;
target = target.left;
}
cur.val = target.val;
if(target == targetParent.left){
targetParent.left = target.right;
}else{
cur.right = target.right;
}
}
}
}
public class TestDemo {
public static void main(String[] args) {
BinarySearchTree binarySearchTree = new BinarySearchTree();
System.out.print("中序遍历为:");
binarySearchTree.inOrder(binarySearchTree.root);
System.out.println();
System.out.print("层次遍历为:");
binarySearchTree.levelOrder(binarySearchTree.root);
System.out.println();
System.out.println("=====删除======");
binarySearchTree.remove(4);
System.out.print("中序遍历为:");
binarySearchTree.inOrder(binarySearchTree.root);
System.out.println();
System.out.print("层次遍历为:");
binarySearchTree.levelOrder(binarySearchTree.root);
System.out.println();
}
}