主要實作了二元樹的一般用法,可能會有些錯誤,還望修正一下。
- package structure.tree;
-
- public class Node {
-
- public int idata; public Node rightNode;
-
- public Node() {
-
- }
-
- public void display() {// отй╬╫ .out.print('{');
- System.out.print(idata);
- System.out.print(',');
- System.out.print(ddata);
- System.out.print('}');
- }
- }
-
-
-
- 複製程式碼
[code]package structure.tree;
import java.util.Stack;
public class Tree {
/* 節點屬性, 樹是由節點構成的 */
private Node root;
public Tree() {
root = null;
}
/*** 尋找指定key值的樹節點
*
* @param key
* @return*/
public Node find(int key) {
Node current = root;
while(current.idata != key) {
if(key key) {
isLeftNode = true;
current = current.leftNode;
}
else if(current.idata key) {
isLeftNode = true;
current = current.leftNode;
} else {
isLeftNode = false;
current = current.rightNode;
}
if(current == null) {// 沒有找到對應的指定節點
flag = false;
return flag;
}
}// 結束while循環
// 執行到此,就表示找到要刪除的節點current
// 刪除的節點是葉結點
if(current.leftNode == null && current.rightNode == null) {
if(isLeftNode == true)
parent.leftNode = null;
else
parent.rightNode = null;
}
// 刪除的節點只有左子節點
else if(current.rightNode == null) {
if(current == root)
root = current.leftNode;
else if(isLeftNode)
parent.leftNode = current.leftNode;
else
parent.rightNode = current.leftNode;
}
// 刪除的節點只有右子節點
else if(current.leftNode == null) {
if(current == root)
root = current.rightNode;
else if(isLeftNode)
parent.leftNode = current.rightNode;
else
parent.rightNode = current.rightNode;
}
// 刪除的節點有左子節點和右子節點
else {
Node replacedNode = getReplacedNode(current);
if(current == root)
root = replacedNode;
else if(isLeftNode)
parent.leftNode = replacedNode;
else
parent.rightNode = replacedNode;
}
return flag;
}
/*** 判斷選擇遍歷方式
*
* @param traverseType*/
public void traverse(int traverseType) {
switch(traverseType) {
case 1:
System.out.print("n先序遍歷:");
preOrder(root);
break;
case 2:
System.out.print("n中序遍歷:");
inOrder(root);
break;
case 3:
System.out.print("n後序遍歷:");
postOrder(root);
break;
}
System.out.println();
}
/*** 先序排列*/
private void preOrder(Node node) {
if(node != null) {
System.out.print(node.idata + " ");
preOrder(node.leftNode);
preOrder(node.rightNode);
}
}
/*** 中序排列*/
private void inOrder(Node node) {
if(node != null) {
preOrder(node.leftNode);
System.out.print(node.idata + " ");
preOrder(node.rightNode);
}
}
/*** 後序排列*/
private void postOrder(Node node) {
if(node != null) {
preOrder(node.leftNode);
preOrder(node.rightNode);
System.out.print(node.idata + " ");
}
}
/*** 找到替換【被刪除節點】的節點並且建構出以【替換點】為根的子樹
* 說明:尋找【被刪除節點】中右子樹中key值最小的點作為【替換節點】,很明顯是右子樹中左葉子節點(如果有的話)
*
* @param delNode
* @return*/
private Node getReplacedNode(Node delNode) {
Node current = delNode.rightNode;
Node replacedNode = delNode;
Node replacedParentNode = delNode;
while(current != null) {
replacedParentNode = replacedNode;
replacedNode = current;
current = current.leftNode;
}if(replacedNode != delNode.rightNode) {
// ReplacedNode就是要替換掉【被刪除節點】的節點
被替換的ParentNode.leftNode =被替換的Node.rightNode;
ReplaceNode.rightNode = delNode.rightNode;
}
ReplaceNode.leftNode = delNode.leftNode;
返回替換節點;
}
/*** 顯示樹狀結構
*
* @param node*/
@SuppressWarnings(“未選取”)
公共無效顯示樹(){
System.out.println("
|