java用三叉链表构建二叉树,三叉链表实现排序二叉树

/*

* 三叉链表实现排序二叉树

*/

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());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值