二叉排序数节点类
public class Node {
int value;
Node left;
Node right;
//构建节点
public Node(int value) {
this.value = value;
}
//添加节点
public void add(Node node) {
if (node == null) {
return;
}
if (node.value < this.value) {
if (this.left == null)
this.left = node;
else
this.left.add(node);
} else {
if (this.right == null)
this.right = node;
else
this.right.add(node);
}
}
//中序遍历以该节点为根节点的二叉排序树
public void list() {
if (this == null)
return;
if (this.left != null)
this.left.list();
System.out.println(this.value);
if (this.right != null)
this.right.list();
}
//查找要删除的节点
public Node search(int value) {
if (this.value == value) {
return this;
} else {
//如果左子树不为空 且该值小于当前节点 对左子树进行递归
if (this.left != null && value<this.value ) {
return this.left.search(value);
//右子树同上
} else if (this.right != null && value>=this.value) {
return this.right.search(value);
}
return null;
}
}
//查找要删除节点的根节点
public Node searchParent(Node node) {
//如果该节点的左子节点或右子节点等于要删除的结点 该节点就是父节点
if ((this.left != null && this.left == node) || (this.right != null && this.right == node)) {
return this;
} else {//根据该节点的value值进行递归查找
if (this.left != null && node.value<this.value) {
return this.left.searchParent(node);
} else if (this.right != null && node.value >= this.value) {
return this.right.searchParent(node);
} else {
return null;
}
}
}
//查找要删除节点的右子树的最小值 并将该节点返回
public Node selectNodeRMin(){
Node temp = this.right;
while(temp.left!=null){
temp = temp.left;
}
return temp;
}
}
二叉排序树类的增删查
//二叉排序树
public class BinarySortTree {
public Node treeNode;
public BinarySortTree(Node treeNode){
this.treeNode = treeNode;
}
//向二叉排序树中添加节点
public void add(Node node){
if (node==null){
return;
}else {
treeNode.add(node);
}
}
//中序遍历二叉排序树
public void listNode(){
if (treeNode==null){
return;
}
treeNode.list();
}
//删除二叉排序数的节点
public void deleteNode(int value){
Node targetNode = treeNode.search(value);
//如果找不到要删除的节点 直接结束
if (targetNode==null){
return;
}else{
//找到要删除节点的父节点
Node parent = treeNode.searchParent(targetNode);
if (parent==null){
//该节点没有父节点 说明是根节点
//找到要删除节点的右子树的最小值
Node rMin = targetNode.selectNodeRMin();
//并从树上伤处该节点
Node parentRMin = treeNode.searchParent(rMin);
if (targetNode.right.left==null){
targetNode.right=null;
}else {
parentRMin.left=null;
}
//将要删除节点的子树都挂在该节点上
rMin.left = targetNode.left;
rMin.right = targetNode.right;
treeNode = rMin;
}else {
//如果要删除节点的左右节点都为空
if (targetNode.left==null&&targetNode.right==null){
targetNode = null;
if (parent.left==targetNode){
parent.left = null;
}else {
parent.right = null;
}
//如果要删除节点只有左子节点
}else if (targetNode.left!=null&&targetNode.right==null){
//如果要删除节点是其父节点的左子节点
if (parent.left==targetNode){
parent.left = targetNode.left;
}else {
parent.right = targetNode.left;
}
//如果要删除节点只有右子节点
}else if (targetNode.left==null&&targetNode.right!=null){
//如果要删除节点是其父节点的左子节点
if (parent.left==targetNode){
parent.left = targetNode.right;
}else {//如果要删除节点是其父节点的右子节点
parent.right = targetNode.right;
}
}else if (targetNode.left!=null&&targetNode.right!=null){
//找到要删除节点的右子树的最小值
Node rMin = targetNode.selectNodeRMin();
//并从树上删除该节点
Node parentRMin = treeNode.searchParent(rMin);
if (targetNode.right.left==null){
targetNode.right=null;
}else {
parentRMin.left=null;
}
//将要删除节点的子树都挂在该节点上
if (targetNode.left!=null){
rMin.left = targetNode.left;
}
if (targetNode.right!=null){
rMin.right = targetNode.right;
}
//如果要删除节点左右子节点都不为空且为其父节点的左子节点
if (parent.left==targetNode){
parent.left = rMin;
//如果要删除节点右子节点都不为空且为其父节点的右子节点
}else {
parent.right = rMin;
}
}
}
}
}
}