树的实现中,插入数据时,确保左子数>节点>=右子树;删除时,如果存在两个子节点,将右子树中最大的节点移动到被删除节点的位置.
package basic;
public interface Tree {
/**
* 长度
*/
public int size();
/**
* 是否为空
* @return
*/
public boolean isEmpty();
/**
* 插入
* @param o
*/
public void insert(Object o);
/**
* 是否包含
* @param o
* @return
*/
public boolean contain(Object o);
/**
* 删除
* @param o
* @return
*/
public boolean delete(Object o);
/**
* 打印
*/
public void display();
}
package basic;
public class SimpleTreeImpl implements Tree {
private class Node {
private Node left;
private Node right;
private Object value;
private Node parent;
public Node(Object o) {
super();
this.value = o;
}
public int hashCode() {
return value.hashCode();
}
public String toString() {
return value.toString() + " : " + value.hashCode();
}
}
private Node root;
private int size;
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void insert(Object o) {
if (null == o)
throw new NullPointerException();
Node node = new Node(o);
if (null == root)
root = node;
else {
insert(root, node, node.hashCode());
}
size++;
}
private void insert(Node current, Node newNode, int hashCode) {
int c = current.hashCode();
if (c < hashCode) {
if (null == current.left) {
current.left = newNode;
newNode.parent = current;
} else
insert(current.left, newNode, hashCode);
} else {
if (null == current.right) {
current.right = newNode;
newNode.parent = current;
} else
insert(current.right, newNode, hashCode);
}
}
public boolean contain(Object o) {
return null != find(o);
}
private Node find(Object o) {
if (null == o)
throw new NullPointerException();
return find(root, o.hashCode());
}
private Node find(Node current, int hashCode) {
if (null == current)
return null;
int c = current.hashCode();
if (c < hashCode) {
return find(current.left, hashCode);
} else if (c > hashCode) {
return find(current.right, hashCode);
} else {
return current;
}
}
public boolean delete(Object o) {
Node node = find(o);
if (null != node) {
delete(node);
}
return true;
}
private void delete(Node node) {
size--;
Node next = null;
// next为删除后需要移动的节点,当被删除节点不存在节点时,next为null,存在1个子节点时,next为子节点,存在2个子节点时,next为右子节点中最大的节点.
// 当前被删除节点有两个子节点时
if (null != node.left && null != node.right) {
next = node.right;
// 找到右子上数最大值的节点,将其移动到被删除节点的位置上
while (next.left != null) {
next = next.left;
}
if (next != node.right) {
if (null != next.right) {
next.right.parent = next.parent;
}
next.parent.left = next.right;
next.right = node.right;
node.right.parent = next;
}
next.left = node.left;
node.left.parent = next;
} else if (null != node.left) {
next = node.left;
} else if (null != node.right) {
next = node.right;
}
// 处理下一个节点与头父节点的关系
if (null != next)
next.parent = node.parent;
if (null != node.parent) {
if (node == node.parent.left)
node.parent.left = next;
else
node.parent.right = next;
} else {
// 删除节点为头节点
root = next;
}
}
public void display() {
System.out.println();
display(root);
}
private void display(Node node) {
if (null == node)
return;
if (null != node.left) {
display(node.left);
}
System.out.print(node.value + ", ");
display(node.right);
}
public static void main(String[] args) {
SimpleTreeImpl tree = new SimpleTreeImpl();
int[] is = new int[] { 8, 12, 4, 14, 10, 6, 2, 15, 13, 11, 9, 7, 5, 3, 1 };
for (int i : is) {
tree.insert(i);
}
//
// tree.display();
// System.out.println(tree);
// tree.insert(1);
// tree.insert(2);
// tree.insert(3);
tree.delete(12);
System.out.println(tree);
}
}