1. 程式人生 > >二十、刪除二叉樹結點

二十、刪除二叉樹結點

分享 中序遍歷 二叉樹 oid str 刪除 指向 insert 封裝

原理:

技術分享圖片

三種類型:

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


}

二十、刪除二叉樹結點