二叉树数据删除的四种实现
1、删除的数据没有子节点
2、删除的数据有一边有子节点
3、删除左子节点和右子节点都不为空的节点
4、根节点的删除
示例:
参考图:
interface Two<T> {
public void add(Comparable<T> data);
public int size();
public Object[] toArray();
public boolean inquire(T data);
public void remove(T data);
}
class TwoImpl<T> implements Two<T> {
private Node root;
private int size;
private int footer;
private Object[] resultData;
@Override
public void add(Comparable<T> data) {
if (data == null) {
return;
}
Node node = new Node(data);
if (this.root == null) {
this.root = node;
} else {
this.root.addNode(this.root, node);
}
++this.size;
}
@Override
public int size() {
return this.size;
}
@Override
public Object[] toArray() {
if (this.size() == 0) {
return null;
}
this.footer = 0;
this.resultData = new Object[this.size];
this.root.toArrayNode();
return this.resultData;
}
@Override
public boolean inquire(T data) {
if (this.size() == 0) {
return false;
}
return this.root.inquireNode(data);
}
@Override
public void remove(T data) {
if (this.inquire(data)) {
if (this.root.data.compareTo(data) == 0) {
this.root = this.delete(data);
} else {
this.delete(data);
}
--this.size;
}
}
private Node delete(T data) {
Node sonNode = null;
Node thisNode = this.root.removeNode(data);
if (thisNode.right == null && thisNode.left == null) {
if (thisNode.parent != null) {
if (thisNode.parent.data.compareTo(data) <= 0) {
thisNode.parent.right = null;
} else {
thisNode.parent.left = null;
}
}
thisNode.parent = null;
}
if ((thisNode.right != null && thisNode.left == null)
|| (thisNode.right == null && thisNode.left != null)) {
if (thisNode.right != null) {
sonNode = thisNode.right;
} else {
sonNode = thisNode.left;
}
if (thisNode.parent != null) {
if (thisNode.parent.data.compareTo(data) <= 0) {
thisNode.parent.right = sonNode;
} else {
thisNode.parent.left = sonNode;
}
}
sonNode.parent = thisNode.parent;
}
if (thisNode.left != null && thisNode.right != null) {
sonNode = thisNode.right;
while (sonNode.left != null) {
sonNode = sonNode.left;
}
if (thisNode.right != sonNode) {
sonNode.parent.left = null;
}
if (sonNode.right != null) {
sonNode.parent.left = sonNode.right;
sonNode.right.parent = sonNode.parent;
}
sonNode.parent = thisNode.parent;
if (thisNode.right != sonNode) {
sonNode.right = thisNode.right;
}
sonNode.left = thisNode.left;
if (thisNode.parent != null) {
if (thisNode.parent.data.compareTo(data) <= 0) {
thisNode.parent.right = sonNode;
} else {
thisNode.parent.left = sonNode;
}
}
}
return sonNode;
}
private class Node {
private Comparable<T> data;
private Node left;
private Node right;
private Node parent;
public Node(Comparable<T> data) {
this.data = data;
}
public void addNode(Node parentNode, Node node) {
if (this.data.compareTo((T) node.data) <= 0) {
if (this.right == null) {
this.right = node;
node.parent = parentNode;
} else {
this.right.addNode(this.right, node);
}
} else {
if (this.left == null) {
this.left = node;
node.parent = parentNode;
} else {
this.left.addNode(this.left, node);
}
}
}
public void toArrayNode() {
if (this.left != null) {
this.left.toArrayNode();
}
TwoImpl.this.resultData[TwoImpl.this.footer++] = this.data;
if (this.right != null) {
this.right.toArrayNode();
}
}
public boolean inquireNode(T data) {
if (this.data.compareTo(data) == 0) {
return true;
} else {
if (this.data.compareTo(data) < 0) {
if (this.right != null) {
return this.right.inquireNode(data);
} else {
return false;
}
} else {
if (this.left != null) {
return this.left.inquireNode(data);
} else {
return false;
}
}
}
}
public Node removeNode(T data) {
if (this.data.compareTo(data) == 0) {
return this;
} else {
if (this.data.compareTo(data) < 0) {
return this.right.removeNode(data);
} else {
return this.left.removeNode(data);
}
}
}
}
}
public class Demo01 {
public static void main(String[] args) {
Two<Member> two = new TwoImpl<>();
two.add(new Member("孙七", 27));
two.add(new Member("赵六", 26));
two.add(new Member("王五", 25));
two.add(new Member("张三", 23));
two.add(new Member("李四1", 24));
two.add(new Member("李四2", 55));
two.add(new Member("李四3", 40));
two.add(new Member("李四4", 38));
two.add(new Member("李四5", 29));
two.add(new Member("李四6", 21));
two.add(new Member("李四7", 19));
two.add(new Member("李四11", 62));
two.add(new Member("李四8", 51));
two.add(new Member("李四9", 34));
two.add(new Member("李四10", 56));
two.add(new Member("李四12", 72));
two.add(new Member("李四13", 45));
two.add(new Member("李四46", 46));
two.add(new Member("李四14", 33));
two.add(new Member("李四15", 31));
two.remove(new Member("孙七", 27));
System.out.println(Arrays.toString(two.toArray()));
}
}
结果:
[name='李四7, age=19
, name='李四6, age=21
, name='张三, age=23
, name='李四1, age=24
, name='王五, age=25
, name='赵六, age=26
, name='李四5, age=29
, name='李四15, age=31
, name='李四14, age=33
, name='李四9, age=34
, name='李四4, age=38
, name='李四3, age=40
, name='李四13, age=45
, name='李四46, age=46
, name='李四8, age=51
, name='李四2, age=55
, name='李四10, age=56
, name='李四11, age=62
, name='李四12, age=72
]