二十、删除二叉树结点

时间:2021-10-05 12:06:46

原理:

二十、删除二叉树结点

三种类型:

    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+" "); //打印根
}
}


}