二叉树节点类:
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;
}
}