前面和后面的问题都有,解决了之后会check
22天
1.this关键字的继承方式,在我们目前Java课程里面只涉及到了继承当中的子类继承父类的成员变量就是 this.父类成员变量 。所以这里我不是太能理解他到底是怎么完成继承到上面声明的树里面的根节点的。
2.后面的入队出队问题我会画一个图来在理解一遍。树里面的知识一定要整扎实一点。
- 数据转换的代码很短, 但要理解还是比较花时间. 反正我写的时候比较辛苦.
- 用了两个队列.
- 实际上涵盖了广度优先 (或称层次) 遍历.
/**
********************
* 将树转换为数据数组,包括 char 数组和 int 数组。完整二叉树中的索引。 他的结果储存在两个成员变量当中
*
* @see #valuesArray 这个放char
* @see #indicesArray 这个放int
*
*********************
*/
public void toDataArrays() {
// 初始化数组
int tempLength = getNumNodes();
valuesArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
// 同时遍历和转换数组.
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);
} // Of if
if (tempTree.rightChild != null) {
tempQueue.enqueue(tempTree.rightChild);
tempIntQueue.enQueue(tempIndex * 2 + 2);
} // Of if
tempTree = (BinaryCharTree) tempQueue.dequeue();
tempIndex = tempIntQueue.deQueue();
} // Of while
}// Of toDataArrays
23天
昨天使用的队列有两种: 存储二叉树节点的队列; 存储整数的队列. 这样的话, 难道我们要为每种类型单独写一个队列? 这样显然没有充分利用代码的复用性. 实际上, 我们只需要一个存储对象的队列就够啦!
理解不够充分的问题。
/**
********************
* 将树转换为数据数组,包括 char 数组和 int 数组。完整二叉树中的索引。 他的结果储存在两个成员变量当中 但是是以队列的形式
*
* @see #valuesArray 这个放char
* @see #indicesArray 这个放int
*********************
*/
public void toDataArraysObjectQueue() {
// 初始化.
int tempLength = getNumNodes();
valuesArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
// 同时遍历和转换
CircleObjectQueue tempQueue = new CircleObjectQueue();
tempQueue.enqueue(this);
CircleObjectQueue tempIntQueue = new CircleObjectQueue();
Integer tempIndexInteger = new Integer(0);
tempIntQueue.enqueue(tempIndexInteger);
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
int tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
System.out.println("tempIndex = " + tempIndex);
while (tempTree != null) {
valuesArray[i] = tempTree.value;
indicesArray[i] = tempIndex;
i++;
if (tempTree.leftChild != null) {
tempQueue.enqueue(tempTree.leftChild);
tempIntQueue.enqueue(new Integer(tempIndex * 2 + 1));
} // Of if
if (tempTree.rightChild != null) {
tempQueue.enqueue(tempTree.rightChild);
tempIntQueue.enqueue(new Integer(tempIndex * 2 + 2));
} // Of if
tempTree = (BinaryCharTree) tempQueue.dequeue();
if (tempTree == null) {
break;
} // Of if
tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
} // Of while
}// Of toDataArraysObjectQueue
24天,还差一点,个人认为理解就差那么一内内。
27天,就是我自己在写24天ObjectStack的时候把depth设成了静态常量,导致了后序里面的两个Stack用的是一个东西。我感觉里面还有一些底层的东西可以整。(check)