图片展示见:
http://marcospring.iteye.com/blog/1623571
方法思路参考:
http://www.cnblogs.com/xunmengyoufeng/archive/2012/10/01/BityTree.html
代码实现:
package com.datestrut.searchTree;
/**
*
* 二叉搜索树
*/
/*
* 二叉树最复杂的步骤即为删除操作,此处只简单介绍一下具体思路:
(1)如果待删除的节点是一片树叶,那么它可以被立即删除。然后将其父节点的相应子节点(左节点或右节点)至空。
(2)如果被删除的节点有一个子节点,那么把它的子节点直接连到它的父节点上即可。(Node:current,parent)
(3)如果被删除的节点(a)有两个子节点,就不能简单的用它的一个子节点代替它。
一般找到(a)的右子树中key最小的节点(c)代替它,
如果c不为叶子节点,那么递归对c进行相应的删除操作。
(Node:successorParent,successor,current)
*/
public class Tree {
private Node root;
public Tree() {
root = null;
}
public Node getRoot(){
return root;
}
//查找节点
public Node find(int key) {
Node current = root;
while (current.iData != key) {
if (key < current.iData) {
current = current.leftChild;
} else {
current = current.rightChild;
}
if (current == null)
return null;
}
return current;
}
public void insert(int id) {
Node newNode = new Node();
newNode.iData = id;
if (root == null)
root = newNode;
else {
Node current = root;
Node parent;
while (true) {
parent = current;
if (id < current.iData) {
current = current.leftChild;
if (current == null) {
parent.leftChild = newNode;
return;
}
} else {
current = current.rightChild;
if (current == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
//删除节点
public boolean delete(int key) {
Node current = root;
Node parent = root;
boolean isLeftChild = true;
while (current.iData != key) {
parent = current;
if (key < current.iData) {
isLeftChild = true;
current = current.leftChild;
} else {
isLeftChild = false;
current = current.rightChild;
}
if (current == null)
return false;
}
// 叶子节点
if (current.leftChild == null && current.rightChild == null) {
if (current == root)
root = null;
else if (isLeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
// 只有一个子节点
} else if (current.rightChild == null) {
if (current == root)
root = current.leftChild;
else if (isLeftChild)
parent.leftChild = current.leftChild;
else
parent.rightChild = current.leftChild;
} else if (current.leftChild == null) {
if (current == root)
root = current.rightChild;
else if (isLeftChild)
parent.leftChild = current.rightChild;
else
parent.rightChild = current.rightChild;
}
// 两个子节点
else {
Node successor = getSuccessor(current);
//步骤三
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.leftChild = successor;
} else
parent.rightChild = successor;
//步骤四
successor.leftChild = current.leftChild;
}
return true;
}
private Node getSuccessor(Node delNode) {
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.rightChild;
while (current != null) {
successorParent = successor;
successor = current;
current = current.leftChild;
}
if (successor != delNode.rightChild) {
//步骤一
successorParent.leftChild = successor.rightChild;
//步骤二
successor.rightChild = delNode.rightChild;
}
return successor;
}
//展示节点
public void show(Node node){
System.out.print(node.iData+" ");
}
//前序遍历 根左右
public void DLR(Node node){
if(node!=null){
show(node);
DLR(node.leftChild);
DLR(node.rightChild);
}
}
}
class Node {
public int iData;
public Node leftChild;
public Node rightChild;
public void displayNode(){
System.out.println('{' + iData + ',' + '}');
}
}
测试代码:
package com.datestrut.searchTree;
public class Main
{
public static void main(String[] args)
{
Tree tree = new Tree();
/*for(int i=0;i<5;i++){
tree.insert(i);
}*/
tree.insert(53);
tree.insert(30);
tree.insert(72);
tree.insert(14);
tree.insert(39);
tree.insert(61);
tree.insert(84);
tree.insert(9);
tree.insert(23);
tree.insert(34);
tree.insert(47);
tree.insert(79);
Node root = tree.getRoot();
tree.DLR(root);
System.out.println();
// tree.delete(61);
// tree.delete(84);
// tree.delete(72);
tree.delete(14);
tree.DLR(root);
}
}