1 重要特性
2 完整代码
2.1 二叉搜索树的节点
/**
* 二叉搜索树的节点
* Created by JayLai on 2019-10-05 22:45:58
*/
public class Node {
//索引
int id;
//数据项
String name;
//右子节点
Node rightNode;
//左子节点
Node leftNode;
/**
* 构造方法
* @param id
* @param name
*/
public Node(int id, String name) {
this.id = id;
this.name = name;
}
}
2.2 二叉搜索树的增删查
/**
* 二叉搜索树
* Created by JayLai on 2019-10-05 22:45:58
*/
public class BinarySearchTree {
private Node root;
public BinarySearchTree() {
this.root = null;
}
/**
* 插入新节点
* @param id 索引
* @param name 数据项
*/
public void insert(int id, String name){
Node newNode = new Node(id, name);
newNode.id = id;
if(root == null){
root = newNode;
}else {
Node current = root;
Node parent;
while (current != null){
parent = current;
if(current.id > newNode.id){
current = current.leftNode;
if (current == null){
parent.leftNode = newNode;
return;
}
} else{
current = current.rightNode;
if(current == null){
parent.rightNode = newNode;
return;
}
}
}
}
}
/**
* 前序遍历
* @param current
*/
public void preOrder(Node current){
if (current == null){
return;
}
System.out.println("id:" + current.id + ", name:" + current.name);
preOrder(current.leftNode);
preOrder(current.rightNode);
}
/**
* 中序遍历
*/
public void inOrder(Node current){
if (current == null){
return;
}
inOrder(current.leftNode);
System.out.println("id:" + current.id + ", name:" + current.name);
inOrder(current.rightNode);
}
/**
* 后序遍历
* @param current
*/
public void postOrder(Node current){
if (current == null){
return;
}
postOrder(current.leftNode);
postOrder(current.rightNode);
System.out.println("id:" + current.id + ", name:" + current.name);
}
/**
* 查找
* @param id
* @return
*/
public Node find(int id){
if (root == null){ //空树
return null;
}
Node current = root;
while (current.id != id){
if(current.id > id){
current = current.leftNode;
}else{
current = current.rightNode;
}
if(current == null){
return null;
}
}
return current;
}
/**
* 查找中序后继节点
* @param delNode
* @return
*/
public Node getSuccessor(Node delNode){
Node successor = delNode;
Node successorParnt = delNode;
Node current = delNode.rightNode;
while(current != null){
successorParnt = successor;
successor = current;
current = current.leftNode;
}
if(successor != delNode.rightNode){
successorParnt.leftNode = successor.rightNode;
successor.rightNode = delNode.rightNode;
}
return successor;
}
/**
* 删除节点
* @param id
* @return
*/
public Node delete(int id){
if (root == null){ //空树
return null;
}
Node current = root;
Node parent = root ;
boolean isLeftNode = true;
//查找要删除节点
while (current.id != id){
parent = current;
if(current.id > id){
current = current.leftNode;
isLeftNode = true;
}else{
current = current.rightNode;
isLeftNode = false;
}
if(current == null){
return null;
}
}
//待删除的节点是叶子节点
if (current.leftNode == null && current.rightNode == null){
if(current == root){
root = null;
} else if (isLeftNode){
parent.leftNode = null;
}else{
parent.rightNode = null;
}
//待删除的节点下有一个子节点
}else if( current.rightNode == null){
if(current == root){
root = current.leftNode;
}else if (isLeftNode){
parent.leftNode = current.leftNode;
}else{
parent.rightNode = current.leftNode;
}
}else if(current.leftNode == null) {
if(current == root){
root = current.rightNode;
}else if (isLeftNode) {
parent.leftNode = current.rightNode;
} else {
parent.rightNode = current.rightNode;
}
//待删除的节点下有两个子节点,用中续后继来代替该节点
}else{
Node successor = getSuccessor(current);
if(current == root){
root = successor;
}
else if (isLeftNode){
parent.leftNode = successor;
}else{
parent.rightNode = successor;
}
successor.leftNode = current.leftNode;
}
return current;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
//添加
tree.insert(10, "DuDu");
tree.insert(20, "LiLi");
tree.insert(15, "LiYang");
tree.insert(3, "Tom");
tree.insert(4, "Jay");
tree.insert(90, "Angle");
tree.insert(2, "XiaoMing");
tree.insert(80, "ZhangSan");
tree.insert(95, "LiSi");
tree.inOrder(tree.root);
System.out.println();
//查找
Node node = tree.find(20);
if( node != null){
System.out.println("id:" + node.id + ", name:" + node.name);
}
System.out.println();
//删除
tree.delete(20);
tree.inOrder(tree.root);
}
}
3 测试结果分析
4 参考文献
[1] Robert, Lafore., Java数据结构和算法.第2版 版. 2004: 中国电力出版社.
[2] Sedgewick Robert与Wayne Kevin., 算法. 2012: 人民邮电出版社.