Day 24
1. Background
今天是学习java的第24天,今天学习的是用顺序表建立二叉树。
这个还是在之前的祖传二叉树代码中直接添加,相当于是重构了二叉树的方法。
2. Description
今天的代码,老实说我下午起床开始看的,等我稍稍有些头绪,我的室友已经叫我去吃晚饭了。后来想出来后,觉得豁然开朗。
代码最开始是将字符数组中的值全部存入一个二叉树节点数组中。额,这里可能有些绕,字符数组就是由字符组成的数组,二叉树节点数组就是由二叉树节点组成的数组。这里的落脚点在于数组,前面的修饰词是指数组内部的元素的类型。这个在C语言学习中学到过,也是很让人头疼的一章吧,比如C语言中的指针数组和数组指针,具体的详情请见我之前写的C语言笔记:https://1drv.ms/w/s!Aug-1Pt5thJpiiT6cGgNc2raOdIR?e=DSMRo9 .
言归正传,存完数据之后,就是根据传进来的另一个int
数组内对字符标的序号,将二叉树节点数组中的节点链接起来。
链接完成后,返回根节点和根节点的孩子即可。
3. Code
package datastructure;
import java.util.Arrays;
import datastructure.CircleObjectQueue.*;
public class BinaryCharTree {
/**
* The value of node.
*/
char value;
/**
* 二叉树的左孩子。
*/
BinaryCharTree leftChild;
/**
* 二叉树的右孩子。
*/
BinaryCharTree rightChild;
/**
**************
* The first constructor.
*
* @param paraValue The given value.
**************
*/
public BinaryCharTree(char paraValue) {
value = paraValue;
leftChild = null;
rightChild = null;
} // Of the first construe
/**
**************
* Reload BinaryCharTree.
*
* @param paraDataArray
* @param paraIndicesArray
**************
*/
public BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray) {
// Step 1. 把二叉树存到顺序表中。
int tempNumNodes = paraDataArray.length;
BinaryCharTree[] tempAllNodes = new BinaryCharTree[tempNumNodes];
for (int i = 0; i < tempNumNodes; i++) {
tempAllNodes[i] = new BinaryCharTree(paraDataArray[i]);
} // Of for i
// Step 2. Link nodes.
for (int i = 1; i < tempNumNodes; i++) {
for (int j = 0; j < i; j++) {
System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) { // 节点序号从0开始,故此处为左孩子
tempAllNodes[j].leftChild = tempAllNodes[i];
System.out.println("Linking " + j + " with " + i);
break;
} else if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
tempAllNodes[j].rightChild = tempAllNodes[i];
System.out.println("Linking " + j + " with " + i);
break;
} // Of if
} // Of for j
} // Of for i
// 返回二叉树的根节点
value = tempAllNodes[0].value;
leftChild = tempAllNodes[0].leftChild;
rightChild = tempAllNodes[0].rightChild;
} // Of the the second constructor
public static BinaryCharTree manualConstructTree() {
// step 1. Creat root.
BinaryCharTree resultTree = new BinaryCharTree('a');
// step 2. creat children and linked them.
BinaryCharTree tempTreeB = new BinaryCharTree('b');
BinaryCharTree tempTreeC = new BinaryCharTree('c');
BinaryCharTree tempTreeD = new BinaryCharTree('d');
BinaryCharTree tempTreeE = new BinaryCharTree('e');
BinaryCharTree tempTreeF = new BinaryCharTree('f');
BinaryCharTree tempTreeG = new BinaryCharTree('g');
// Link strat
resultTree.leftChild = tempTreeB;
resultTree.rightChild = tempTreeC;
tempTreeB.rightChild = tempTreeD;
tempTreeC.leftChild = tempTreeE;
tempTreeD.leftChild = tempTreeF;
tempTreeD.rightChild = tempTreeG;
return resultTree;
} // Of manualConstructTree
/**
**************
* 先根、中根、后根遍历树。
**************
*/
public void preOrderVisit() {
System.out.print(value + " ");
if (leftChild != null) {
leftChild.preOrderVisit();
} // Of if
if (rightChild != null) {
rightChild.preOrderVisit();
} // Of if
} // Of preOrderVisit
public void inOrderVisit() {
if (leftChild != null) {
leftChild.inOrderVisit();
} // Of if
System.out.print(value + " ");
if (rightChild != null) {
rightChild.inOrderVisit();
} // Of if
}
public void postOrderVisit() {
if (leftChild != null) {
leftChild.postOrderVisit();
} // Of if
if (rightChild != null) {
rightChild.postOrderVisit();
} // Of if
System.out.print(value + " ");
}
public int getDepth() {
if ((leftChild == null) && (rightChild == null)) {
return 1;
} // Of if
// check left
int tempLeftDepth = 0;
if (leftChild != null) {
tempLeftDepth = leftChild.getDepth();
} // Of if
// Check right
int tempRightDepth = 0;
if (rightChild != null) {
tempRightDepth = rightChild.getDepth();
} // Of if
if (tempLeftDepth >= tempRightDepth) {
return tempLeftDepth + 1;
} else {
return tempRightDepth + 1;
} // Of if
} // Of getDepth
// 遍历时存储节点中的值。
char[] valueArray;
// 二叉树的索引。
int[] indicesArray;
public void toDataArrays() {
int tempLength = getNumNodes();
valueArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
CircleObjectQueue tempQueue = new CircleObjectQueue();
tempQueue.enQueue(this);
CircleObjectQueue tempIntQueue = new CircleObjectQueue();
tempIntQueue.enQueue(0);
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.deQueue();
int tempIndex = (int) tempIntQueue.deQueue();
while (tempTree != null) {
valueArray[i] = tempTree.value;
indicesArray[i] = tempIndex;
i++;
if (tempTree.leftChild != null) {
tempQueue.enQueue(tempTree.leftChild);
tempIntQueue.enQueue(tempIndex * 2 + 1);
} // Of if
if (tempTree.rightChild != null) {
tempQueue.enQueue(tempTree.rightChild);
tempIntQueue.enQueue(tempIndex * 2 + 2);
} // Of if
tempTree = (BinaryCharTree) tempQueue.deQueue();
if (tempTree != null)
tempIndex = (int) tempIntQueue.deQueue();
} // Of while
}
public int getNumNodes() {
if ((leftChild == null) && (rightChild == null)) {
return 1;
} // Of if
int tempLeftNodes = 0;
if (leftChild != null) {
tempLeftNodes = leftChild.getNumNodes();
} // Of if
int tempRightNodes = 0;
if (rightChild != null) {
tempRightNodes = rightChild.getNumNodes();
} // Of if
return tempLeftNodes + tempRightNodes + 1;
}// Of getNumNodes
public void toDataArraysObjectQueue() {
int tempLength = getNumNodes();
valueArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
// 将object对象转换为想要的数据类型。
CircleObjectQueue tempQueue = new CircleObjectQueue();
tempQueue.enQueue(this);
CircleObjectQueue tempIntQueue = new CircleObjectQueue();
Integer tempIndexInteger = Integer.valueOf(0);
tempIntQueue.enQueue(tempIndexInteger);
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.deQueue();
int tempIndex = ((Integer) tempIntQueue.deQueue()).intValue();
System.out.println("tempIndex = " + tempIndex);
while (tempTree != null) {
valueArray[i] = tempTree.value;
indicesArray[i] = tempIndex;
i++;
if (tempTree.leftChild != null) {
tempQueue.enQueue(tempTree.leftChild);
tempIntQueue.enQueue(Integer.valueOf(tempIndex * 2 + 1));
} // Of if
if (tempTree.rightChild != null) {
tempQueue.enQueue(tempTree.rightChild);
tempIntQueue.enQueue(Integer.valueOf(tempIndex * 2 + 2));
} // Of if
tempTree = (BinaryCharTree) tempQueue.deQueue();
if (tempTree == null) {
break;
}//Of if
tempIndex = ((Integer) tempIntQueue.deQueue()).intValue();
} // Of while
}// Of toDataArraysObjectQueue
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
BinaryCharTree tempTree = manualConstructTree();
System.out.println("\r\nPreorder visit:");
tempTree.preOrderVisit();
System.out.println("\r\nIn-order visit:");
tempTree.inOrderVisit();
System.out.println("\r\nPost-order visit:");
tempTree.postOrderVisit();
System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
System.out.println("The number of nodes is: " + tempTree.getNumNodes());
tempTree.toDataArrays();
System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
tempTree.toDataArraysObjectQueue();
System.out.println("Only object queue.");
System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
char[] tempCharArray = {'A', 'B', 'C', 'D', 'E', 'F'};
int[] tempIndicesArray = {0, 1, 2, 4, 5, 12};
BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray, tempIndicesArray);
System.out.println("\r\nPreorder visit:");
tempTree2.preOrderVisit();
System.out.println("\r\nIn-order visit:");
tempTree2.inOrderVisit();
System.out.println("\r\nPost-order visit:");
tempTree2.postOrderVisit();
}// Of main
}
运行结果: