import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 一个简单二叉树结构
* @author tang
*
*/
public class Tree {
private Node root;
public void add(Node node) {
if (root == null) {
root = node;
} else {
Node temp = root;
while (true) {
if (node.getValue() <= temp.getValue()) {// 往左找
if (temp.getLeft() != null) {
temp = temp.getLeft();
} else {// 找到了
temp.setLeft(node);
node.setParent(temp);
break;
}
} else {// 往右找
if (temp.getRight() != null) {
temp = temp.getRight();
} else {// 找到了
temp.setRight(node);
node.setParent(temp);
break;
}
}
}
}
}
public boolean remove(Node node) {
Node find = find(root, node);
if (find != null) {
Node parent = find.getParent();
if (parent == null) {// 如果要删除的节点是根节点
Node left = find.getLeft();
left.setParent(null);
root=left;
Node right = find.getRight();
Node leftMaxNode = getMaxNode(left);
leftMaxNode.setRight(right);
right.setParent(leftMaxNode);
} else if (parent.getLeft() == find) {// 如果要删除的节点在父节点的左侧
Node left = find.getLeft();
left.setParent(parent);
parent.setLeft(left);
Node right = find.getRight();
Node leftMaxNode = getMaxNode(left);
leftMaxNode.setRight(right);
right.setParent(leftMaxNode);
} else {// 如果要删除的节点在父节点的右侧
Node right = find.getRight();
right.setParent(parent);
parent.setRight(right);
Node left = find.getLeft();
Node rightMinNode = getMinNode(right);
rightMinNode.setLeft(left);
left.setParent(rightMinNode);
}
find.setLeft(null);
find.setRight(null);
find.setParent(null);
return true;
}
return false;
}
private Node getMinNode(Node node) {
Node min = node;
if (min != null) {
while (min.getLeft() != null) {
min = min.getLeft();
}
}
return min;
}
private Node getMaxNode(Node node) {
Node max = node;
if (max != null) {
while (max.getRight() != null) {
max = max.getRight();
}
}
return max;
}
private Node find(Node node, Node target) {
if (node == target) {
return node;
} else {
Node left = node.getLeft();
if (left != null) {
return find(left, target);
}
Node right = node.getRight();
if (right != null) {
return find(right, target);
}
return null;
}
}
public boolean contains(Node node) {
Node find = find(root, node);
if (find == null) {
return false;
} else {
return true;
}
}
private void toList(List<Node> list, Node node) {
if (node != null) {
Node left = node.getLeft();
if (left != null) {
toList(list, left);
}
list.add(node);
Node right = node.getRight();
if (right != null) {
toList(list, right);
}
}
}
private List<Node> toList() {
List<Node> list = new ArrayList<>();
toList(list, root);
return list;
}
public Iterator<Node> iterator() {
return toList().iterator();
}
public int size() {
return toList().size();
}
@Override
public String toString() {
return toList().toString();
}
public static void main(String[] args) {
Tree tree = new Tree();
List<Node> list = new ArrayList<>();
list.add(new Node(10));
list.add(new Node(5));
list.add(new Node(15));
list.add(new Node(3));
list.add(new Node(8));
list.add(new Node(12));
list.add(new Node(20));
list.add(new Node(1));
list.add(new Node(4));
list.add(new Node(6));
list.add(new Node(9));
list.add(new Node(11));
list.add(new Node(13));
list.add(new Node(16));
list.add(new Node(20));
for (Node treeNode : list) {
tree.add(treeNode);
}
System.out.println(tree);
tree.remove(list.get(1));
tree.remove(list.get(0));
System.out.println(tree);
}
/**
* 二叉树节点
* @author tang
*/
public static class Node {
private Node parent;
private Node left;
private Integer value;
private Node right;
public Node(Integer value) {
this.value = value;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Integer getValue() {
return value;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (value == null) {
if (other.value != null)
return false;
} else
if (!value.equals(other.value))
return false;
return true;
}
@Override
public String toString() {
return value.toString();
}
}
}
用Java实现一个简单的二叉树
最新推荐文章于 2024-09-15 19:46:31 发布