目录
- 1 概念
- 2 操作
- 2.1 查找
- 2.2 插入
- 2.3 删除
- 3 性能分析
- 4 和 java 类集的关系
1 概念
二叉搜索树 又称 二叉排序树,它是一棵空树,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值;
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值。
- 它的左右子树也分别为二叉搜索树
你会发现它中序遍历的结果就是有序的。
如下图所示就是一颗二叉搜索树:
2 操作
2.1 查找
具体实现代码示例如下所示:
package bstree;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 BSNode search(int val){if(root == null) return null;BSNode cur = root;while (cur != null){if(cur.val == val){return cur;}else if(cur.val > val){cur = cur.left;}else{cur = cur.right;}}return null;}}public class TestDemo {public static void main(String[] args) {}
}
2.2 插入
- 如果树为空树,即根 == null,直接插入。
- 如果树不是空树,按照查找逻辑确定插入位置,插入新结点。
具体实现代码示例如下所示:
package bstree;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 BSNode search(int val){if(root == null) return null;BSNode cur = root;while (cur != null){if(cur.val == val){return cur;}else if(cur.val > val){cur = cur.left;}else{cur = cur.right;}}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){return false;}else if(cur.val > val){parent = cur;cur = cur.left;}else{parent = cur;cur = cur.right;}}if(parent.val < val){parent.right = bsNode;}else{parent.left = bsNode;}return true;}}public class TestDemo {public static void preOrder(BinarySearchTree.BSNode root){if(root == null){return;}System.out.print(root.val+" ");preOrder(root.left);preOrder(root.right);}public static void inOrder(BinarySearchTree.BSNode root){if(root == null){return;}inOrder(root.left);System.out.print(root.val+" ");inOrder(root.right);}public static void main(String[] args) {BinarySearchTree binarySearchTree = new BinarySearchTree();binarySearchTree.insert(4);binarySearchTree.insert(3);binarySearchTree.insert(1);binarySearchTree.insert(15);binarySearchTree.insert(11);preOrder(binarySearchTree.root);System.out.println();inOrder(binarySearchTree.root);System.out.println();try {BinarySearchTree.BSNode ret = binarySearchTree.search(4);System.out.println(ret.val);}catch (NullPointerException e){System.out.println("没有找到当前的节点..........");e.printStackTrace();}}
}
2.3 删除
前提是删除这个节点之后,整棵树还是一棵二叉搜索树。
删除思路:
设待删除结点为 cur, 待删除结点的双亲结点为 parent。
则分为下面三种情况:
- 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
- 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
- cur.left != null && cur.right != null
- 如果像原来那样删除,那么一个节点就会出现两个父亲节点?
需要使用替换法进行删除,即在它的右子树中寻找中序下的第一个结点(关键值最小),用它的值填补到被删除节点中,再来处理该结点的删除问题。- 我们怎么知道放谁上去?
当前需要删除的节点的左边找最大的,右边找最小的。
具体实现代码示例如下所示:
package bstree;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 BSNode search(int val) {if (root == null) return null;BSNode cur = root;while (cur != null) {if (cur.val == val) {return cur;} else if (cur.val > val) {cur = cur.left;} else {cur = cur.right;}}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) {return false;} else if (cur.val > val) {parent = cur;cur = cur.left;} else {parent = cur;cur = cur.right;}}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);} 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 = cur.right;}else if(parent.left == cur){parent.left = cur.right;}else if(parent.right == cur){parent.right = cur.right;}}else if(cur.right == null){if(cur == root){root = cur.left;}else if(parent.left == cur){parent.left = cur.left;}else if(parent.right == cur){parent.right = cur.left;}}else{//这里采取的是右边找最小的方法BSNode targetParent = cur;BSNode target = cur.right;while(target.left != null){targetParent = target;target = target.left;}//target指向的节点就是 右边的最小值cur.val = target.val;if(target == targetParent.left){targetParent.left = target.right;}else{targetParent.right = target.right;}}}}public class TestDemo {public static void preOrder(BinarySearchTree.BSNode root){if(root == null){return;}System.out.print(root.val+" ");preOrder(root.left);preOrder(root.right);}public static void inOrder(BinarySearchTree.BSNode root){if(root == null){return;}inOrder(root.left);System.out.print(root.val+" ");inOrder(root.right);}public static void main(String[] args) {BinarySearchTree binarySearchTree = new BinarySearchTree();binarySearchTree.insert(4);binarySearchTree.insert(3);binarySearchTree.insert(1);binarySearchTree.insert(15);binarySearchTree.insert(11);preOrder(binarySearchTree.root);System.out.println();inOrder(binarySearchTree.root);System.out.println();binarySearchTree.remove(15);System.out.println("=============删除===============");preOrder(binarySearchTree.root);System.out.println();inOrder(binarySearchTree.root);System.out.println();try {BinarySearchTree.BSNode ret = binarySearchTree.search(4);System.out.println(ret.val);}catch (NullPointerException e){System.out.println("没有找到当前的节点..........");e.printStackTrace();}}
}
3 性能分析
插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。
但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:
最优情况下: 二叉搜索树为完全二叉树,其比较次数为:O(log2^n)
最差情况下: 二叉搜索树退化为单支树,其比较次数为:O(n)。
问题:如果退化成单支树,二叉搜索树的性能就失去了。那能否进行改进,不论按照什么次序插入关键码,都可以是二叉搜索树的性能最佳?
4 和 java 类集的关系
TreeMap 和 TreeSet 即 java 中利用搜索树实现的 Map 和 Set;实际上用的是红黑树,而红黑树是一棵近似平衡的二叉搜索树,即在二叉搜索树的基础之上 + 颜色以及红黑树性质验证,关于红黑树的内容在后边笔记中再进行描述。