二叉树最复杂的步骤即为删除操作,此处只简单介绍一下具体思路:
(1)如果待删除的节点是一片树叶,那么它可以被立即删除。然后将其父节点的相应子节点(左节点或右节点)至空。
(2)如果被删除的节点有一个子节点,那么把它的子节点直接连到它的父节点上即可。(Node:current,parent)
(3)如果被删除的节点(a)有两个子节点,就不能简单的用它的一个子节点代替它。一般找到(a)的右子树中key最小的节点(c)代替它,如果c不为叶子节点,那么递归对c进行相应的删除操作。(Node:successorParent,successor,current)
package com.donghao.erchashu;
public class Node {
public int iData;
public Node leftChild;
public Node rightChild;
public void displayNode(){
System.out.println('{' + iData + ',' + '}');
}
}
package com.donghao.erchashu;
public class Tree {
private Node root;
public Tree() {
root = null;
}
public Node find(int key) {
Node current = root;
while (current.iData != key) {
if (key < current.iData) {
current = current.leftChild;
} else {
current = current.rightChild;
}
if (current == null)
return null;
}
return current;
}
public void insert(int id) {
Node newNode = new Node();
newNode.iData = id;
if (root == null)
root = newNode;
else {
Node current = root;
Node parent;
while (true) {
parent = current;
if (id < current.iData) {
current = current.leftChild;
if (current == null) {
parent.leftChild = newNode;
return;
}
} else {
current = current.rightChild;
if (current == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
public boolean delete(int key) {
Node current = root;
Node parent = root;
boolean isLeftChild = true;
while (current.iData != key) {
parent = current;
if (key < current.iData) {
isLeftChild = true;
current = current.leftChild;
} else {
isLeftChild = false;
current = current.rightChild;
}
if (current == null)
return false;
}
// 叶子节点
if (current.leftChild == null && current.rightChild == null) {
if (current == root)
root = null;
else if (isLeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
// 只有一个子节点
} else if (current.rightChild == null) {
if (current == root)
root = current.leftChild;
else if (isLeftChild)
parent.leftChild = current.leftChild;
else
parent.rightChild = current.leftChild;
} else if (current.leftChild == null) {
if (current == root)
root = current.rightChild;
else if (isLeftChild)
parent.leftChild = current.rightChild;
else
parent.rightChild = current.rightChild;
}
// 两个子节点
else {
Node successor = getSuccessor(current);
//步骤三
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.leftChild = successor;
} else
parent.rightChild = successor;
//步骤四
successor.leftChild = current.leftChild;
}
return true;
}
private Node getSuccessor(Node delNode) {
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.rightChild;
while (current != null) {
successorParent = successor;
successor = current;
current = current.leftChild;
}
if (successor != delNode.rightChild) {
//步骤一
successorParent.leftChild = successor.rightChild;
//步骤二
successor.rightChild = delNode.rightChild;
}
return successor;
}
}
重点解释:
getSuccessor方法找到a的直接后继c(即以a的右节点为根的子树中的最左孩子)
<1>如果c为a的右节点:步骤一:将a的parent.rightChild置换为c
步骤二:将c的左子树置换为a的parent.leftChild
<2>如果c为a的右节点的左后代:步骤一:将c的parent.leftChild置换为c.rightChild(getSuccessor()方法中实现)
步骤二:将c的rightChild置换为a的parent.rightChild(getSuccessor()方法中实现)
步骤三:将a(即current节点)的parent.rightChild置换为c节点
步骤四:将最新的a节点(已被c置换掉)的leftChild置换为原a节点的左孩子(current.leftChild)