package com.sunxl.test;
import java.util.LinkedList;
import java.util.List;
import com.sun.star.packages.NoEncryptionException;
/**
* @author 熊浪
* @Email xiongl@sunline.cn
* @Time 2017年6月9日
* @此类作用
*/
@SuppressWarnings("unused")
public class BinaryTree {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static List<Node> nodeList = null;
/**
* 私有内部类 满二叉树的个数为2^i-1
*
* @author 熊浪
* @Email xiongl@sunline.cn
* @Time 2017年6月9日
* @此类作用
*/
private class Node {
Node left;// 左节点
Node right;// 有节点
int data;// 节点值
// 构造方法创建一个节点
Node(int newData) {
left = null;
right = null;
data = newData;
}
}
/**
* 公开内部静态类
*
* @author 熊浪
* @Email xiongl@sunline.cn
* @Time 2017年6月9日
* @此类作用
*/
public static class NodeObject {
NodeObject left;
NodeObject right;
Object data;
NodeObject(Object newData) {
left = null;
right = null;
data = newData;
}
public NodeObject getLeft() {
return left;
}
public void setLeft(NodeObject left) {
this.left = left;
}
public NodeObject getRight() {
return right;
}
public void setRight(NodeObject right) {
this.right = right;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
/**
* 创建二叉树
*/
public void createBinTree() {
nodeList = new LinkedList<Node>();
// 把array数组的值放入nodeList中
for (int i = 0, len = array.length; i < len; i++) {
nodeList.add(new Node(array[i]));
}
// 按照前一个节点的索引和后个一个节点创建关系,1-0,2-1、2(2*0+1=1,2*0+2=2),3-3,4(2*1+1,2*1+2),5,6(2*2+1,2*2+2);总共需要循环的次数为len/2-1次,但最后一个赋值可能不一定有有节点,所以需要电镀提取
for (int i = 0, len = array.length / 2 - 1; i < len; i++) {
nodeList.get(i).left = nodeList.get(2 * i + 1);// 左节点:把上一个节点和左节点联系起来(通过索引*2+1)
nodeList.get(i).right = nodeList.get(2 * i + 2);// 右节点:把上一个节点和右节点联系起来(通过索引*2+2)
}
// 最后一个父节点:因为最后一个父节点可能没有右节点,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
nodeList.get(lastParentIndex).left = nodeList.get(lastParentIndex * 2 + 1);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).right = nodeList.get(lastParentIndex * 2 + 2);
}
}
/**
* 创建Object数组二叉树
*
* @param nodeList
*/
public void createObjectBinTree(LinkedList<NodeObject> nodeList) {
for (int i = 0, len = nodeList.size() / 2 - 1; i < len; i++) {
nodeList.get(i).left = nodeList.get(2 * i + 1);
nodeList.get(i).right = nodeList.get(2 * i + 2);
}
int lastParentIndex = nodeList.size() / 2 - 1;
nodeList.get(lastParentIndex).left = nodeList.get(2 * lastParentIndex + 1);
if (nodeList.size() % 2 == 1) {
nodeList.get(lastParentIndex).right = nodeList.get(2 * lastParentIndex + 2);
}
}
/**
* 先序遍历
*
* @param node
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + "\t");
preOrderTraverse(node.left);// 回调遍历左节点
preOrderTraverse(node.right);// 回调遍历右节点
}
/**
* 中序遍历
*
* @param node
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.left);
System.out.print(node.data + "\t");
inOrderTraverse(node.right);
}
/**
* 后序遍历
*
* @param node
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.left);
postOrderTraverse(node.right);
System.out.print(node.data + "\t");
}
/**
* 1
* 2 3
* 4 5 6 7
* 8 9
*
* @param args
*/
public static void main(String[] args) {
BinaryTree binTree = new BinaryTree();
binTree.createBinTree();
// nodeList中第0个索引处的值即为根节点
Node root = nodeList.get(0);
System.out.println("先序遍历:");
System.out.println("效果图:");
System.out.println(1 + "\t" + 2 + "\t" + 4 + "\t" + 8 + "\t" + 9 + "\t" + 5 + "\t" + 3 + "\t" + 6 + "\t" + 7);
System.out.println("打印图:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍历:");
System.out.println("效果图:");
System.out.println(2 + "\t" + 4 + "\t" + 8 + "\t" + 9 + "\t" + 5 + "\t" + 1 + "\t" + 3 + "\t" + 6 + "\t" + 7);
System.out.println("打印图:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍历:");
System.out.println("效果图:");
System.out.println(2 + "\t" + 4 + "\t" + 8 + "\t" + 9 + "\t" + 5 + "\t" + 3 + "\t" + 6 + "\t" + 7 + "\t" + 1);
System.out.println("打印图:");
postOrderTraverse(root);
}
}
二叉树的实现和递归遍历
于 2017-06-09 18:37:50 首次发布