day 22 二叉树的存储
1. 思路
(结合下图来理解将二叉树存入数组中)以完全二叉树来计算结点存储索引位置,则当双亲结点为i,则左子树存储的索引为2i+1;右子树为2i+2;如果对于稀疏的二叉树而言,用这样的存储方式显然会很浪费大量的存储空间,而文章中的第二个方法则是把存储值和存储索引分开,一个数组存储结点的值,一个数组存储结点对应的索引值,这个索引值也是根据完全二叉树计算方式算出来的,这样就可以相比于第一种方法这样可以节省空间。
2.层次遍历代码
在看了本题的存储思路主要是采用存储遍历的方式来存储二叉树的存储,所以只要层次遍历的方式明白就可以理解文章中的代码,层次遍历主要就是借助队列来实现的。以下是自己结合文章写出的层次遍历算法。
/**
* Level-order visit.
*/
public void levelOderVisit(){
CircleObjectQueue tempQueue = new CircleObjectQueue();
tempQueue.enqueue(this);
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
while (tempTree != null){
System.out.print("" + tempTree.value + " ");
if (tempTree.leftChild != null) {
tempQueue.enqueue(tempTree.leftChild);
}
if (tempTree.rightChild != null) {
tempQueue.enqueue(tempTree.rightChild);
}
tempTree = (BinaryCharTree) tempQueue.dequeue();
}
}
3.代码
我发现用前中后序遍历去实现两个队列存储貌似有点麻烦,用这个层次遍历要方便点。
public void toDataArrays(){
int tempLength = getNumNodes();
valuesArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
//Traverse and convert at the same time.
CircleObjectQueue tempQueue = new CircleObjectQueue();
tempQueue.enqueue(this);
CircleIntQueue tempIntQueue = new CircleIntQueue();
tempIntQueue.enqueue(0);
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
int tempIndex = tempIntQueue.dequeue();
while (tempTree != null){
valuesArray[i] = tempTree.value;
indicesArray[i] = tempIndex;
i++;
if (tempTree.leftChild != null) {
tempQueue.enqueue(tempTree.leftChild);
tempIntQueue.enqueue(tempIndex * 2 + 1);
}
if (tempTree.rightChild != null) {
tempQueue.enqueue(tempTree.rightChild);
tempIntQueue.enqueue(tempIndex * 2 + 2);
}
tempTree = (BinaryCharTree) tempQueue.dequeue();
tempIndex = tempIntQueue.dequeue();
}
}
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.valuesArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
}
}