1. 程式人生 > >二叉樹(java版)

二叉樹(java版)

二叉樹節點類:

package com.node;
public class TreeNode {
public int data;
public TreeNode leftChild;
public TreeNode rightChild;
public TreeNode(int data) {
this.data = data;
this.leftChild = null;
this.rightChild = null;
}

}

測試類:


package com.node;

public class TreeTest {

public static TreeNode root;

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = { 35, 17, 39, 9, 28, 65, 56, 87 };
root = new TreeNode(array[0]); // 建立二叉樹
for (int i = 1; i < array.length; i++) {
insert(root, array[i]); // 向二叉樹中插入資料
}
/*
* 前序遍歷或稱先跟遍歷 即遍歷順序為:先根節點-左子樹-右子樹
*/
// System.out.println("先根遍歷:");
// preOrder(root);
/*
* 中序遍歷或稱中跟遍歷 即遍歷順序為:先左子樹-根節點-右子樹
*/


System.out.println();
System.out.println("中根遍歷:");
inOrder(root);

delete(65);
System.out.println("\n刪除後:");
inOrder(root);
/*
* 後序遍歷或稱後跟遍歷 即遍歷順序為:先左子樹-右子樹-根節點
*/
// System.out.println();
// System.out.println("後根遍歷:");
// postOrder(root);
}
// 向二叉樹中插入資料
public static void insert(TreeNode root, int data) {
if (data > root.data) { // 如果插入的節點大於跟節點
if (root.rightChild == null) { // 如果右子樹為空,就插入,如果不為空就再建立一個節點
root.rightChild = new TreeNode(data); // 就把插入的節點放在右邊
} else {
insert(root.rightChild, data);
}
} else { // 如果插入的節點小於根節點
if (root.leftChild == null) { // 如果左子樹為空,就插入,如果不為空就再建立一個節點
root.leftChild = new TreeNode(data); // 就把插入的節點放在左邊邊
} else {
insert(root.leftChild, data);
}
}
}


public static void preOrder(TreeNode root) { // 先根遍歷
if (root != null) {
System.out.print(root.data + "-");
preOrder(root.leftChild);
preOrder(root.rightChild);
}
}


public static void inOrder(TreeNode root) { // 中根遍歷


if (root != null) {
inOrder(root.leftChild);
System.out.print(root.data + "-");
inOrder(root.rightChild);
}
}


public static void postOrder(TreeNode root) { // 後根遍歷


if (root != null) {
postOrder(root.leftChild);
postOrder(root.rightChild);
System.out.print(root.data + "-");
}
}
  //刪除節點
public static void delete(int value) {
TreeNode current = root;
TreeNode 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) {
System.out.println("沒有找到該節點");
return;
}


}
          //刪除葉枝節點
if (current.leftChild == null && current.rightChild == null) {
if (isLeftChild)
parent.leftChild = null;
else
parent.rightChild = null;

else if (current.rightChild == null) {
if (current == root) {
root = current.leftChild;
} else if (isLeftChild) {
parent.leftChild = current.leftChild;
} else {
parent.rightChild = current.leftChild;
}
} else if (current.leftChild == null) {
if (current == root) {
root = current.rightChild;
} else if (isLeftChild) {
parent.leftChild = current.rightChild;
} else {
parent.rightChild = current.rightChild;
}
} else {
TreeNode successor = getSuccessor(current);
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.leftChild = successor;
} else {
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;
}


}


public static TreeNode getSuccessor(TreeNode delNode) {
TreeNode successor = delNode;
TreeNode successorParent = delNode;
TreeNode 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;
}


}