一、创建二叉排序树 添加二叉树结点以及遍历
二、删除一颗二叉排序数的结点
这里分为三种情况:
1.删除的结点为叶子结点
①首先要去找需要删除的结点 targetNode
②找到需要删除的结点的父结点 parentNode 判断需要删除的结点 targetNode是左子结点还是右子结点
③根据②判断 parentNode.left = null;
parentNode.right = null;
2.删除的结点有一个子结点
①首先要去找需要删除的结点 targetNode
②找到需要删除的结点的父结点 parentNode 判断需要删除的结点 targetNode是左子结点还是右子结点
③如果需要删除的是结点有左子节点
如果targetNode为父结点的左节点parentNode.left = targetNode.left
如果targetNode为父结点的右节点parentNode.right = targetNode.left
如果需要删除的是结点有右子节点
如果targetNode为父结点的左节点parentNode.left = targetNode.right
如果targetNode为父结点的右节点parentNode.right = targetNode.right
3.删除的结点有两个子节点
①首先要去找需要删除的结点 targetNode
②找到需要删除的结点的父结点 parentNode 判断需要删除的结点 targetNode是左子结点还是右子结点
③找到删除结点的右节点下的所有子树的最小值 先创建一个临时变量保存下来 将其删除
④最后将targetNode指向临时变量
package datastructure.tree.binarysorttree;
public class BinarySortTree {
public static void main(String[] args) {
int[] arr = {7,3,10,12,5,1,9,2};
BinarySort binarySort = new BinarySort();
for (int i = 0; i < arr.length; i++) {
binarySort.add(new Node(arr[i]));
}
binarySort.inOrder();
binarySort.deleteNode(1);
binarySort.deleteNode(2);
binarySort.deleteNode(5);
binarySort.deleteNode(3);
binarySort.deleteNode(9);
binarySort.deleteNode(10);
binarySort.deleteNode(12);
binarySort.deleteNode(7);
System.out.println("===============");
binarySort.inOrder();
}
}
class BinarySort{
public Node root;
public int delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
deleteNode(target.id);
return target.id;
}
public void deleteNode(int id) {
if (root == null) {
return;
}else {
Node targetNode = root.searchNode(id);
if (targetNode == null ) {
return;
}
if (root.left == null && root.right == null) {
root = null;
return;
}
Node parent = searchParent(id);
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.id == id) {
parent.left = null;
} else if (parent.right != null && parent.right.id == id) {
parent.right = null;
}
}else if (targetNode.left != null && targetNode.right !=null){
targetNode.id = delRightTreeMin(targetNode.right);
}else {
if (targetNode.left != null) {
if (parent != null) {
if (parent.left != null && parent.left.id == id) {
parent.left = targetNode.left;
}else {
parent.right = targetNode.left;
}
}else {
root = targetNode.left;
}
}else {
if (parent != null) {
if (parent.left != null && parent.left.id == id) {
parent.left = targetNode.right;
}else {
parent.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
}
public Node search(int id) {
if (root == null) {
return null;
}else {
return root.searchNode(id);
}
}
public Node searchParent(int id) {
if (root == null) {
return null;
}else {
return root.searchParentNode(id);
}
}
public void add(Node node){
if (root == null) {
root = node;
}else {
root.add(node);
}
}
public void inOrder() {
if (root == null) {
System.out.println("节点为空");
}else {
root.inOrder();
}
}
}
class Node {
public int id;
public Node left;
public Node right;
public Node(int id) {
this.id = id;
}
@Override
public String toString() {
return "Node{" +
"id=" + id +
'}';
}
public void add(Node node) {
if (node == null) {
return;
}
if (node.id < this.id) {
if (left == null) {
this.left = node;
}else {
this.left.add(node);
}
}else {
if (right == null) {
this.right = node;
}else {
this.right.add(node);
}
}
}
public void inOrder() {
if (this.left != null) {
this.left.inOrder();
}
System.out.println("node=>"+this.id);
if (this.right != null) {
this.right.inOrder();
}
}
public Node searchNode(int id) {
if (id == this.id) {
return this;
} else if (id < this.id) {
if (this.left == null) {
return null;
}
return this.left.searchNode(id);
} else {
if (this.right == null) {
return null;
}
return this.right.searchNode(id);
}
}
public Node searchParentNode(int id) {
if (this.left != null && this.left.id == id ||
this.right != null && this.right.id == id){
return this;
}else {
if (this.id > id && this.left != null) {
return this.left.searchParentNode(id);
} else if (this.id < id && this.right != null) {
return this.right.searchParentNode(id);
} else {
return null;
}
}
}
}