前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >排序二叉树-删除节点

排序二叉树-删除节点

作者头像
shengjk1
发布2020-06-28 11:14:11
4960
发布2020-06-28 11:14:11
举报
文章被收录于专栏:码字搬砖码字搬砖码字搬砖

我们已经了解了什么是排序二叉树以及排序二叉树的遍历和添加元素,现在我们一起来看一下,排序二叉树是如何删除元素的。

步骤
先找到要删除的节点 targetNode
找到要删除节点的父节点 parent
一、删除叶子节点
1.确定 targetNoe 是 parent 的左子节点还是右子节点
2.根据前面的情况来对应删除
parent.left=null.
parent.right=null
二、删除只有一颗子树的节点
1.确定 targetNode 的子节点是右子节点还是左子节点
2.确定 targetNode 是 parent 的左子节点还是右子节点
3.对应删除
三、删除有两颗子树的节点
1.从 targetNode 的右子树找到最小的节点
2.用一个临时变量,将最小节点的值保存 temp
3.删除最小节点
4.targetNode.value=temp
代码
package xmht.datastructuresandalgorithms.datastructure.binarysortTree;

/**
 * @author shengjk1
 * @date 2020/6/15
 */
public class BinarySortTree {
	public static void main(String[] args) {
		int[] arr = {7, 3, 10, 12, 5, -1, 9};
		BinarySortTree1 binarySortTree = new BinarySortTree1();
		for (int i : arr) {
			binarySortTree.add(new Node(i));
		}
		
		System.out.println("中序遍历二叉树");
		binarySortTree.infixOrder();
		
		binarySortTree.delNode(3);
		
		System.out.println("删除之后====中序遍历二叉树");
		binarySortTree.infixOrder();
	}
}

class BinarySortTree1 {
	//
	private Node root;
	
	public void add(Node node) {
		if (this.root != null) {
			this.root.add(node);
		} else {
			root = node;
		}
	}
	
	//查找节点
	public Node search(int value) {
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}
	
	//查找父节点
	public Node searchParent(int value) {
		if (root == null) {
			return null;
		} else {
			return root.searchParent(value);
		}
	}
	
	/**
	 * 返回以 node 为根节点的二叉排序树的最小节点的值
	 * 并删除以 node 为根节点的二叉排序树的最小节点
	 *
	 * @param node 传入节点
	 * @return 以 node 为根节点的二叉排序树的最小节点的值
	 */
	public int delRightTreeMin(Node node) {
		Node target = node;
		while (target.left != null) {
			target = target.left;
		}
		delNode(target.value);
		return target.value;
		
	}
	
	//删除节点
	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			//1.先找到要删除节点 targetNode
			Node targetNode = root.search(value);
			if (targetNode == null) {
				return;
			}
			//如果发现当前的二叉树只有一个节点
			if (root.left == null && root.right == null) {
				root = null;
				return;
			}
			
			//找到 targetNode 的 parentnode
			Node parentNode = searchParent(value);
			
			//1.如果删除的节点是叶子节点
			if (targetNode.right == null && targetNode.left == null) {
				if (parentNode.left != null && parentNode.left.value == value) {
					parentNode.left = null;
				} else {
					parentNode.right = null;
				}
				//删除有两颗子树的节点
			} else if (targetNode.left != null && targetNode.right != null) {
				int minVal = delRightTreeMin(targetNode.right);
				targetNode.value = minVal;
				//删除只有一颗子树的节点
			} else {
				//如果删除的节点只有左子树
				if (targetNode.left != null) {
					if (parentNode != null) {
						if (parentNode.left.value == value) {
							parentNode.left = targetNode.left;
							//targetNode 为 parent 的右节点
						} else {
							parentNode.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}
				} else {
					if (parentNode != null) {
						if (parentNode.left.value == value) {
							parentNode.left = targetNode.right;
						} else {
							parentNode.right = targetNode.right;
						}
					} else {
						root = targetNode.right;
					}
				}
			}
		}
	}
	
	public void infixOrder() {
		if (this.root != null) {
			this.root.infixOrder();
		}
	}
	
	@Override
	public String toString() {
		return "BinarySortTree1{" +
				"root=" + root +
				'}';
	}
}

class Node {
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		this.value = value;
	}
	
	
	/**
	 * @param value 希望删除节点的值
	 * @return 有则返回否则返回null
	 */
	public Node search(int value) {
		if (value == this.value) {
			return this;
			//向左子树查找
		} else if (value < this.value) {
			if (this.left == null) {
				return null;
			}
			return this.left.search(value);
			
			//向右子树查找
		} else {
			if (this.right == null) {
				return null;
			}
			return this.right.search(value);
		}
	}
	
	//查找要删除节点的父节点
	
	/**
	 * @param value
	 * @return
	 */
	public Node searchParent(int value) {
		//如果当前节点就是要删除节点的父节点,就返回
		if ((this.left != null && this.left.value == value) ||
				(this.right != null && this.right.value == value)) {
			return this;
		} else {
			if (value < this.value && this.left != null) {
				return this.left.searchParent(value);
			} else if (value >= this.value && this.right != null) {
				return this.right.searchParent(value);
			} else {
				return null;
			}
		}
	}
	
	//添加节点
	//递归的形式添加,需要满足二叉排序树的要求
	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 infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}
		System.out.println(this);
		
		if (this.right != null) {
			this.right.infixOrder();
		}
	}
	
	
	@Override
	public String toString() {
		return "Node{" +
				"value=" + value +
				'}';
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 步骤
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档