主要任务:1.具有通用性的对象栈:改写栈程序, 里面存放对象.该程序应该放在datastructure.stack 包内.还是依靠强制类型转换, 支持不同的数据类型.增加了 isEmpty() 方法.
package day25;
public class ObjectStack {
/**
* 栈的最大长度.
*/
public static final int MAX_DEPTH = 10;
/**
* 栈的当前长度.
*/
int depth;
/**
* 储存数据
*/
Object[] data;
/**
*********************
* Construct an empty sequential list(构造函数).
*********************
*/
public ObjectStack() {
depth = 0;
data = new Object[MAX_DEPTH];
}// Of the first constructor
/**
*********************
* Overrides the method claimed in Object, the superclass of any class.
*********************
*/
public String toString() {
String resultString = "";
for (int i = 0; i < depth; i++) {
resultString += data[i];
} // Of for i
return resultString;
}// Of toString
/**
*********************
* 入栈.
*
* @param paraObject
* The given object.
* @return Success or not.
*********************
*/
public boolean push(Object paraObject) {
if (depth == MAX_DEPTH) {
System.out.println("Stack full.");
return false;
} // Of if
data[depth] = paraObject;
depth++;
return true;
}// Of push
/**
*********************
* 出栈.
*
* @return 将栈顶元素返回.
*********************
*/
public Object pop() {
if (depth == 0) {
System.out.println("Nothing to pop.");
return '\0';
} // of pop
Object resultObject = data[depth - 1];
depth--;
return resultObject;
}// Of pop
/**
*********************
* 判断栈是否为空
*
* @return True if empty.
*********************
*/
public boolean isEmpty() {
if (depth == 0) {
return true;
}//Of if
return false;
}// Of isEmpty
/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
public static void main(String args[]) {
ObjectStack tempStack = new ObjectStack();
for (char ch = 'a'; ch < 'm'; ch++) {
tempStack.push(new Character(ch));
System.out.println("The current stack is: " + tempStack);
} // Of for i
char tempChar;
for (int i = 0; i < 12; i++) {
tempChar = ((Character)tempStack.pop()).charValue();
System.out.println("Poped: " + tempChar);
System.out.println("The current stack is: " + tempStack);
} // Of for i
}// Of main
}//Of class ObjectStack
运行结果:
The current stack is: a
The current stack is: ab
The current stack is: abc
The current stack is: abcd
The current stack is: abcde
The current stack is: abcdef
The current stack is: abcdefg
The current stack is: abcdefgh
The current stack is: abcdefghi
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Poped: j
The current stack is: abcdefghi
Poped: i
The current stack is: abcdefgh
Poped: h
The current stack is: abcdefg
Poped: g
The current stack is: abcdef
Poped: f
The current stack is: abcde
Poped: e
The current stack is: abcd
Poped: d
The current stack is: abc
Poped: c
The current stack is: ab
Poped: b
The current stack is: a
Poped: a
The current stack is:
Nothing to pop.
Poped:
2.中序遍历,在之前的二叉树中继续插入代码:
/**
*********************
* In-order visit with stack.
*********************
*/
public void inOrderVisitWithStack() {
ObjectStack tempStack = new ObjectStack();
BinaryCharTree tempNode = this;
while (!tempStack.isEmpty() || tempNode != null) {
if (tempNode != null) {
tempStack.push(tempNode);
tempNode = tempNode.leftChild;
} else {
tempNode = (BinaryCharTree) tempStack.pop();
System.out.print("" + tempNode.value + " ");
tempNode = tempNode.rightChild;
} // Of if
} // Of while
}// Of inOrderVisit
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));
tempTree.toDataArraysObjectQueue();
System.out.println("Only object queue.");
System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
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\nPre-order visit:");
tempTree2.preOrderVisit();
System.out.println("\r\nIn-order visit:");
tempTree2.inOrderVisit();
System.out.println("\r\nPost-order visit:");
tempTree2.postOrderVisit();
System.out.println("\r\nIn-order visit with stack:");
tempTree2.inOrderVisitWithStack();
}// Of main
运行结果:
Preorder visit:
a b d f g c e
In-order visit:
b f d g a e c
Post-order visit:
f g d b e c a
The depth is: 4
The number of nodes is: 7
The values are: [a, b, c, d, e, f, g]
The indices are: [0, 1, 2, 4, 5, 9, 10]
tempIndex = 0
Only object queue.
The values are: [a, b, c, d, e, f, g]
The indices are: [0, 1, 2, 4, 5, 9, 10]
indices 0 vs. 1
Linking 0 with 1
indices 0 vs. 2
Linking 0 with 2
indices 0 vs. 4
indices 1 vs. 4
Linking 1 with 3
indices 0 vs. 5
indices 1 vs. 5
indices 2 vs. 5
Linking 2 with 4
indices 0 vs. 12
indices 1 vs. 12
indices 2 vs. 12
indices 4 vs. 12
indices 5 vs. 12
Linking 4 with 5
Preorder visit:
A B D C E F
In-order visit:
B D A E F C
Post-order visit:
D B F E C A
In-order visit with stack:
B D A E F C
注:将栈更改了更具有通用性,这里将判断栈是否为空设置成了一个独立函数。利用栈的原理其实就是根据节点遍历顺序左右根去依次入栈道最深处,栈本来就可以实现递归的作用,因此这里先一直从左孩子入栈到最左下方的节点,然后再往上出栈,同时再往右孩子进行之前的步骤。