搜索树
基础概念
二叉搜索树又称二叉排序树,它或者是一棵空树**,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
- 搜索树中序遍历是有序的。
- 搜索树是纯Key模型,Key值不允许有重复的。
查找
public static class Node{
int key;
Node left;
Node right;
public Node(int key){
this.key=key;
}
}
private Node root=null;
在搜索树中查找key,找到返回key所在的结点,找不到返回null
public Node search(int key){
Node pre=root;
while(pre!=null){
if(key==pre.key){
return pre;
}else if(key<pre.key){
pre=pre.left;
}else{
pre=pre.right;
}
}
return null;
}
插入
插入成功返回true,插入失败返回false, 搜索树不允许key重复,所以遇到key相等直接返回false。如果根结点为null 则直接插入,否则先找到合适的父母结点再插入key值,
public boolean insert(int key){
if(root==null){
root=new Node(key);
return true;
}
Node pre=root;
Node parent=null;
while(pre!=null){
if(key==pre.key){
return false;
}else if(key<pre.key){
parent=pre;
pre=pre.left;
}else{
parent=pre;
pre=pre.right;
}
}
Node node=new Node(key);
if(key<parent.key){
parent.left=node;
}else{
parent.right=node;
}
return true;
}
删除
设待删除结点为 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
需要使用替换法进行删除,即在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题(即查找左子树做大的或者右子树最小的来替换)
public boolean remove(int key){
Node cur=root;
Node parent=null;
while(cur!=null){
if(key==cur.key){
removeNode(parent,cur);
return true;
}else if(key<cur.key){
parent=cur;
cur=cur.left;
}else{
parent=cur;
cur=cur.right;
}
}
return false;
}
public void removeNode(Node parent,Node cur){
if(cur.left==null){
if(cur==root){
root=cur.right;
}else if(cur==parent.left){
parent.left=cur.right;
}else{
parent.right=cur.right;
}
}else if(cur.right==null){
if(cur==root){
root=cur.left;
}else if(cur==parent.left){
parent.left=cur.left;
}else{
parent.right=cur.left;
}
}else{
Node goatparent=cur;
Node goat=cur.right;
while(goat.left!=null){
goatparent=goat;
goat=goat.left;
}
cur.key=goat.key;
if(goat==goatparent.left){
goatparent.left=goat.right;
}else{
goatparent .right=goat.right;
}
}
}
测试
public static void main(String[] args) {
// 1. 创建搜索树
// 2. 随机插入一些数据
// 3. 打印前序 + 中序遍历
// 4. 查找
Text1 tree = new Text1();
int[] keys = { 3, 9, 7, 4, 1, 6, 2, 8, 5 };
for (int key : keys) {
System.out.println(tree.insert(key));
}
System.out.println("插入重复数据");
System.out.println(tree.insert(7));
System.out.println("前序遍历");
preOrder(tree.root);
System.out.println("中序遍历");
inOrder(tree.root);
System.out.println(tree.search(7).key);
System.out.println(tree.search(8).key);
System.out.println(tree.search(5).key);
}
private static void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.println(node.key);
inOrder(node.right);
}
}
private static void preOrder(Node node) {
if (node != null) {
System.out.println(node.key);
preOrder(node.left);
preOrder(node.right);
}
}