完全二叉树的创建和遍历(JAVA)
public class Main {
public static void main(String[] args) {
int[] tree = {7, 5, 6, 1, 2, 3, 4};
TreeNode root = new TreeNode(tree, CreateType.CENG), node;
root.preorderThis();
tree = new int[] {7, 5, 1, 2, 6, 3, 4};
root = new TreeNode(tree, CreateType.QIAN);
root.cengorderThis();
tree = new int[] {1, 5, 2, 7, 3, 6, 4};
root = new TreeNode(tree, CreateType.ZHONG);
root.postorderThis();
tree = new int[] {1, 2, 5, 3, 4, 6, 7, 1, 2, 5, 3, 4, 6, 7, 1, 2, 5, 3, 4, 6, 7};
root = new TreeNode(tree, CreateType.HOU);
root.inorderThis();
root.printTreeGraphic();
node = root.getlNode();
node.printTreeGraphic();
node.preorderThis();
node.inorderThis();
node.postorderThis();
}
}
enum CreateType {
CENG, QIAN, ZHONG, HOU;
}
class TreeNode {
private int data, n, i;
private TreeNode lNode, rNode;
private int[] array, tmp;
private boolean start;
public TreeNode getlNode() {
return lNode;
}
public TreeNode getrNode() {
return rNode;
}
public TreeNode(int data) {
this.data = data;
this.lNode = null;
this.rNode = null;
}
public TreeNode(int[] array, CreateType xu) {
switch (xu) {
case CENG:
this.array = array;
n = array.length;
break;
case QIAN:
n = array.length;
this.array = new int[n];
this.tmp = array;
i = 0;
qianCreateTree(1);
break;
case ZHONG:
n = array.length;
this.array = new int[n];
this.tmp = array;
i = 0;
zhongCreateTree(1);
break;
case HOU:
n = array.length;
this.array = new int[n];
this.tmp = array;
i = 0;
houCreateTree(1);
break;
default:
break;
}
this.data = this.array[0];
this.lNode = null;
this.rNode = null;
cengCreateTree(this, 1);
}
private void cengCreateTree(TreeNode node, int k) {
if(k >= n) return;
node.lNode = new TreeNode(array[k]);
if(k + 1 >= n) return;
node.rNode = new TreeNode(array[k + 1]);
cengCreateTree(node.lNode, k * 2 + 1);
cengCreateTree(node.rNode, k * 2 + 3);
}
private void qianCreateTree(int x) {
if (x > n) return;
array[x - 1] = tmp[i ++];
qianCreateTree(x << 1);
qianCreateTree((x << 1) + 1);
}
private void zhongCreateTree(int x) {
if (x > n) return;
zhongCreateTree(x << 1);
array[x - 1] = tmp[i ++];
zhongCreateTree((x << 1) + 1);
}
private void houCreateTree(int x) {
if (x > n) return;
houCreateTree(x << 1);
houCreateTree((x << 1) + 1);
array[x - 1] = tmp[i ++];
}
public void preorderThis() {
start = true;
System.out.println("前序遍历:");
preorder(this);
System.out.println(";");
}
public void inorderThis() {
start = true;
System.out.println("中序遍历:");
inorder(this);
System.out.println(";");
}
public void postorderThis() {
start = true;
System.out.println("后序遍历:");
postorder(this);
System.out.println(";");
}
public void cengorderThis() {
start = true;
System.out.println("层序遍历:");
if (array == null || array.length == 0)createArray();
for(int i = 0; i < n - 1; ++i) {
System.out.print(array[i] + " -> ");
}
System.out.println(array[n - 1] + ";");
}
public void printTreeGraphic() {
System.out.println("树形输出:");
if (array == null || array.length == 0)createArray();
int h = (int) (Math.log(n) / Math.log(2)), m = lcm(2, 5);
for (int i = 0; i < h - 1; i++) {
m = lcm(m, (1 << (i + 2)) + 1);
if (m > 100) { m /= m/100; }
}
int c = 1;
for (int i = 0; i < h + 1; ++ i) {
for (int j = 0; j < c; j++) {
int t = j + c - 1;
if (t >= n) {
System.out.println();
return;
}
System.out.print(kong(m/((1<<(i+1))+1)) + array[t]);
}
System.out.println();
c *= 2;
}
}
private int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private String kong(int n) {
String s = "";
for(int i = 0; i < n; ++i) {
s += " ";
}
return s;
}
private void preorder(TreeNode node) {
if (node == null) return;
if (start) {
System.out.print(node.data);
start = false;
} else {
System.out.print(" -> " + node.data);
}
preorder(node.lNode);
preorder(node.rNode);
}
private void inorder(TreeNode node) {
if (node == null) return;
inorder(node.lNode);
if (start) {
System.out.print(node.data);
start = false;
} else {
System.out.print(" -> " + node.data);
}
inorder(node.rNode);
}
private void postorder(TreeNode node) {
if (node == null) return;
postorder(node.lNode);
postorder(node.rNode);
if (start) {
System.out.print(node.data);
start = false;
} else {
System.out.print(" -> " + node.data);
}
}
private void createArray() {
if (n <= 0) countNode(this);
array = new int[n];
createArray1(this, 1);
}
private void countNode(TreeNode node) {
if (node == null) return;
countNode(node.lNode);
countNode(node.rNode);
n ++;
}
private void createArray1(TreeNode node, int x) {
if (node == null) return;
createArray1(node.lNode, x << 1);
createArray1(node.rNode, (x << 1) + 1);
array[x - 1] = node.data;
}
}