}
public Tree(Node root) {
this.root = root;
}
public Tree() {
}
public void add(Node node){
if (root == null){
root = node;
}else {
root.add(node);
}
}
public void midOrder(){
if (root != null){
root.midOrder();
}else {
System.out.println("树为空");
}
}
//查找要删除的节点
public Node search(int val){
if (this.root == null){
return null;
}else {
return root.search(val);
}
}
//查找父节点
public Node searchParent(Node node){
if (root == null){
return null;
}else {
return root.searchParent(node);
}
}
/**
* 功能:
* 当删除的节点有两课子树时,将要删除的节点当作根节点
* 找到子树的最小值,删除最小值,并返回最小值的值
* @param node 要删除的节点(传入后当作根节点)
* @return 返回子树的最小值
*/
public int delRightTreeMin(Node node){
Node target = node;
//循环的查找左节点,就会找到最小值
while (target.left != null){
target = target.left;
}
//此时,target就时最小节点,此时删除最小节点的值
delNode(target.val);
return target.val;
}
//删除节点
public void delNode(int val){
if (root == null){
return;
}else {
Node targetnode = root.search(val);
if (targetnode == null ){
return;
}
if (targetnode == root && targetnode.left == null && targetnode.right == null){
root = null;
return;
}
Node parent = searchParent(targetnode);
//要删除的节点是叶子节点
if (targetnode.left == null && targetnode.right == null){
if (targetnode == parent.left && parent.left != null){
parent.left = null;
}else if (parent.right != null && parent.right == targetnode ){
parent.right = null;
}
}
//删除右两棵子树的节点
else if (targetnode.left != null && targetnode.right != null){
int rightTreeMinVal = delRightTreeMin(targetnode.right);//在右子树中找最小值,用来顶替替要删除的节点
targetnode.val = rightTreeMinVal;//此时将右子树的最小值付给了要删除的节点
}
//
必看视频!获取2024年最新Java开发全套学习资料 备注Java
删除you一棵子树的节点
else {
//如果要删除的节点有左子节点
if (targetnode.left != null) {
if(parent != null) {
//如果要删除的节点是parent的左子节点
if (parent.left.val == targetnode.val) {
parent.left = targetnode.left;
} else {
parent.right = targetnode.left;
}
}else {
root = targetnode.left;
}
}
//如果眼删除的节点有右子节点
else {//如果要删除的节点是parent的右子节点
if (parent != null){
if (parent.left.val == targetnode.val){
parent.left = targetnode.right;
}else {
parent.right = targetnode.right;
}
}else {
root = parent.right;
}
}
}
}
}
}
class Node{
int val;
Node left;
Node right;
public Node(int val) {
this.val = val;
}
@Override
public String toString() {
return "Node{" +
"val=" + val +
'}';
}
//添加节点
public void add(Node node){
if (node == null){
return;
}
if (node.val > this.val){
if (this.right == null){
this.right = node;
}else {//右递归
this.right.add(node);
}
}else {
if (this.left == null){
this.left = node;
}else {
this.left.add(node);
}
}
}
//中序遍历
public void midOrder(){
if (this.left != null){
this.left.midOrder();
}
System.out.println(this.val);
if (this.right != null){
this.right.midOrder();
}
}
//查找要删除的节点
public Node search(int val){
if (this.val == val){
return this;
}else if (this.val > val){
//如果左子节点但为空
if (this.left != null) {
return this.left.search(val);
}
return null;
}else {
// 如果右子节点为空,return null;
if (this.right != null) {
return this.right.search(val);
}
return null;
}
}
//查找要删除的叶子节点的父节点
public Node searchParent(Node node){
//如果当前节点是眼删除节点的父节点,直接返回
最后
各位读者,由于本篇幅度过长,为了避免影响阅读体验,下面我就大概概括了整理了
return null;
}
}
//查找要删除的叶子节点的父节点
public Node searchParent(Node node){
//如果当前节点是眼删除节点的父节点,直接返回
最后
各位读者,由于本篇幅度过长,为了避免影响阅读体验,下面我就大概概括了整理了
[外链图片转存中…(img-AQgMcTek-1716459189908)]
[外链图片转存中…(img-6KiA6ztc-1716459189909)]
[外链图片转存中…(img-P9IpPMLH-1716459189909)]
[外链图片转存中…(img-9c3pOvoi-1716459189910)]