二十删除二叉树结点

Posted Heaven of freedom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二十删除二叉树结点相关的知识,希望对你有一定的参考价值。

原理:

三种类型:

    1.删除的是叶子结点

    2.删除的结点有一个子节点

    3.删除的结点有两个子节点

代码:

public class Tree {
public Node root; //根节点

public void insert(int value,String name) //插入结点
{
//封装成结点
Node newNode = new Node(value,name);
//引用当前结点
Node current = root;
//引用父结点
Node parent;
//判断是否为空树
if(current == null)
{
root = newNode;
return;
}else
{
while(true)
{
parent = current; //父结点指向当前结点
if(current.data > value) //要插入的值小于该结点的值
{
current = current.leftChild; //往左走
if(current == null) //到了叶子结点
{
parent.leftChild =newNode;
return;
}
}
else
{
current = current.rightChild; //往右走
if(current == null)
{
parent.rightChild = newNode;
return;
}
}
}

}


}


//查找结点
public Node find(int value)
{
Node current = root; //指向根节点

while(current.data != value)
{
if(current.data > value)
{
current = current.leftChild;
}
else
{
current = current.rightChild;
}

if(current == null)
{
return null;
}
}
return current;
}

/*
* 删除结点*/
public boolean delete(int value)
{
Node current = root;
Node parent = root;
boolean IsLeftChild = true;

//查找结点
while(current.data != value)
{
parent =current;
if(current.data > value)
{
current = current.leftChild;
IsLeftChild = true;
}
else
{
current = current.rightChild;
IsLeftChild = false;
}

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(IsLeftChild)
{
if(current == root)
{
current.leftChild =null;
}
parent.leftChild = current.leftChild;
}
else
{
if(current == root)
{
current.rightChild =null;
}
parent.rightChild =current.leftChild;
}
}else if(current.leftChild == null){
if(IsLeftChild)
{
parent.leftChild = current.rightChild;
}
else {
parent.rightChild =current.rightChild;
}
}
else {
Node seccesor = GetSuccessor(current);
if(current ==root)
{
root = seccesor;
}
else if(IsLeftChild)
{
parent.leftChild =seccesor;
}
else
{
parent.rightChild=seccesor;
}
seccesor.leftChild = current.leftChild;

}
return true;


}

//寻找中继结点
public Node GetSuccessor(Node delNode)
{
Node successor = delNode;
Node successorParent = 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;
}


/*
* 前序遍历
* */
public void frontOrder(Node localNode)
{
if(localNode != null) //遍历的不是叶子结点
{
System.out.print(localNode.data + "," + localNode.name+" "); //打印根
frontOrder(localNode.leftChild); //左子树
frontOrder(localNode.rightChild); //右子树
}

}

/*中序遍历*/
public void inOrder(Node localNode)
{
if(localNode != null)
{
inOrder(localNode.leftChild); //中序遍历左子树
System.out.print(localNode.data + "," + localNode.name+" "); //打印根
inOrder(localNode.rightChild); //遍历右子树
}
}

/*后序遍历*/
public void lastOrder(Node localNode)
{
if(localNode != null)
{
lastOrder(localNode.leftChild); //后序遍历左子树
lastOrder(localNode.rightChild); //后序遍历右子树
System.out.print(localNode.data + "," + localNode.name+" "); //打印根
}
}


}

 

以上是关于二十删除二叉树结点的主要内容,如果未能解决你的问题,请参考以下文章

数据结构(二十一)二叉树遍历算法的应用与二叉树的建立

数据结构(二十二)二叉树的顺序存储结构

剑指offer二十四之二叉树中和为某一值的路径

数据结构—二叉树如何删除一个结点二叉树排序树和二叉搜索树的遍历

如何删除一棵普通二叉树的叶子结点?

408数据结构与算法—树和二叉树(二十七)