/*
* 三叉链表实现排序二叉树
*/
public class BSTByThreeNode {
/**
* 内部类: 节点Node
*
* @author Administrator
*/
static class Node {
Object data;
Node parent;
Node left;
Node right;
public Node(Object data, Node parent, Node left, Node right) {
this.data = data;
this.parent = parent;
this.left = left;
this.right = right;
}
@Override
public String toString() {
final String s = data.toString();
return s;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj.getClass() == Node.class) {
Node target = (Node) obj;
return data.equals(target.data)
&& left == target.left
&& right == target.right
&& parent == target.parent;
}
return false;
}
@Override
public int hashCode() {
return 3 * data.hashCode() + 5 * left.hashCode() + 7 * right.hashCode() + 13 * parent.hashCode();
}
}
private Node root;
/**
* 创建空排序二叉树
*/
public BSTByThreeNode() {
root = null;
}
/**
* 创建排序二叉树
*/
public BSTByThreeNode(T o) {
root = new Node(o, null, null, null);
}
/**
* 添加节点
*/
public void add(T ele) {
if (root == null) {
root = new Node(ele, null, null, null);
} else {
Node current = root;
Node parent = null;
int cmp = 0;
while (current != null) {
parent = current;
cmp = ele.compareTo(current.data);
if (cmp > 0) {
current = current.right;
} else {
current = current.left;
}
}
Node newNode = new Node(ele, parent, null, null);
if (cmp > 0) {
parent.right = newNode;
} else {
parent.left = newNode;
}
}
}
/**
* 删除节点
*/
public void remove(T ele) {
Node target = getNode(ele);
if (target == null) {
return;
}
//被删除节点的左右子树都为空
if (target.left == null && target.right == null) {
if (target == root) {
root = null;
} else {
if (target == target.parent.left) {
target.parent.left = null;
} else {
target.parent.right = null;
}
target.parent = null;
}
}
//左子树为空,右子树不为空
else if (target.left == null && target.right != null) {
//被删除的是根节点
if (target == root) {
root = target.right;
} else {
//被删除的节点是父节点的左子节点
if (target == target.parent.left) {
target.parent.left = target.right;
}
//被删除的节点是父节点的右子节点
else {
target.parent.right = target.right;
}
//被删除的节点的右子节点的父节点指向被删除节点的父节点
target.right.parent = target.parent;
}
}
//左子树不为空,右子树为空
else if (target.left != null && target.right == null) {
//被删除的是根节点
if (target == root) {
root = target.left;
} else {
//被删除的节点是父节点的左子节点
if (target == target.parent.left) {
target.parent.left = target.left;
}
//被删除的节点是父节点的右子节点
else {
target.parent.right = target.left;
}
//被删除的节点的左子节点的父节点指向被删除节点的父节点
target.left.parent = target.parent;
}
}
//左右子树都不为空(让中序前驱或后继节点替代所删除的节点,即左子树的最右下子节点或右子树的最左下节点)
else {
//leftMaxNode用于保存被删除节点左子树的最大节点
Node leftMaxNode = target.left;
//搜索leftMaxNode
while (leftMaxNode.right != null) {
leftMaxNode = leftMaxNode.right;
}
//从原来的子树中删除leftMaxNode
leftMaxNode.parent.right = null;
leftMaxNode.parent = target.parent;
//被删除的节点是父节点的左子节点
if (target == target.parent.left) {
target.parent.left = leftMaxNode;
} else {//被删除的节点是父节点的右子节点
target.parent.right = leftMaxNode;
}
leftMaxNode.left = target.left;
leftMaxNode.right = target.right;
}
//被删除的节点的引用都置空
target.left = target.right = target.parent = null;
}
/**
* 根据给定的值搜索节点
*/
public Node getNode(T ele) {
Node p = root;
while (p != null) {
int cmp = ele.compareTo(p.data);
if (cmp > 0) {
p = p.right;
} else if (cmp < 0) {
p = p.left;
} else {
break;
}
}
return p;
}
/**
* 广度遍历
*/
public List breadthFirst() {
Queue queue = new ArrayDeque();
List list = new ArrayList();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
list.add(queue.peek());
Node p = queue.poll();
if (p.left != null) {
queue.offer(p.left);
}
if (p.right != null) {
queue.offer(p.right);
}
}
return list;
}
/**
* main方法测试
*/
public static void main(String[] args) {
BSTByThreeNode tree = new BSTByThreeNode();
//添加节点
tree.add(5);
tree.add(20);
tree.add(10);
tree.add(3);
tree.add(8);
tree.add(15);
tree.add(30);
System.out.println(tree.breadthFirst());
//删除节点
tree.remove(20);
System.out.println(tree.breadthFirst());
}
}