二叉树前序和后序遍历的栈实现
一、前序遍历的栈实现

Pre-order visit with stack: A
| tempNode | 进出栈 |
|---|---|
| A | push(A) |
| A.leftChild | / |

Pre-order visit with stack: A B
| tempNode | 进出栈 |
|---|---|
| B | push(B) |
| A.leftChild | / |
| null | pop(B) |
| B.rightChild | / |

Pre-order visit with stack: A B D
| tempNode | 进出栈 |
|---|---|
| D | push(D) |
| A.leftChild | / |
| null | pop(D) |
| D.rightChild | / |
| null | pop(A) |
| A.rightChild | / |
这里注意与中序遍历的区别,虽然到目前为止,二叉树个节点的遍历顺序与站内元素都与中序遍历一样,但栈的作用完全不一样。在中序遍历中,栈中元素的出栈顺序即为遍历的顺序,而在前序遍历中,栈中元素的进栈顺序即为遍历顺序。

Pre-order visit with stack: A B D C
| tempNode | 进出栈 |
|---|---|
| C | push(C ) |
| C.leftChild | / |

Pre-order visit with stack: A B D C E
| tempNode | 进出栈 |
|---|---|
| E | push(E) |
| E.leftChild | / |
| null | pop(E) |
| E.rightChild | / |

Pre-order visit with stack: A B D C E F
| tempNode | 进出栈 |
|---|---|
| F | push(F) |
| F.leftChild | / |
| null | pop(F) |
| F.rightChild | pop(F) |
| null | pop(C ) |
| C.rightChild | / |
| null | / |
结束遍历。
二、后序遍历的栈实现

| tempNode | tempOutputStack | tempStack |
|---|---|---|
| A | push(A) | push(A) |
| A.rightChild | / | / |

| tempNode | tempOutputStack | tempStack |
|---|---|---|
| C | push© | push© |
| C.rightChild | / | / |

| tempNode | tempOutputStack | tempStack |
|---|---|---|
| null | / | push© |
| C.leftChild | / | / |
| E | push(E) | push(E) |
| E.rightChild | / | / |

| tempNode | tempOutputStack | tempStack |
|---|---|---|
| F | push(F) | push(F) |
| F.rightChild | / | / |
| null | / | pop(F) |
| F.leftChild | / | / |
| null | / | pop(E) |
| E.leftChild | / | / |
| null | / | pop(A) |
| A.leftChild | / | / |

| tempNode | tempOutputStack | tempStack |
|---|---|---|
| B | push(B) | push(B) |
| B.rightChild | / | / |

| tempNode | tempOutputStack | tempStack |
|---|---|---|
| D | push(D) | push(D) |
| D.rightChild | / | / |
| null | / | pop(D) |
| D.leftChild | / | |
| null | / | pop(B) |
遍历结束。
根据最后的tempOutputStack,Post-order visit with stack: D B F E C A。
三、代码与数据测试
/**
********************
* Pre-order visit with stack.
********************
*/
public void preOrderVisitWithStack() {
ObjectStack tempStack = new ObjectStack();
BinaryCharTree tempNode = this;
while (!tempStack.isEmpty() || tempNode != null) {
if (tempNode != null) {
System.out.print("" + tempNode.value + " ");
tempStack.push(tempNode);
tempNode = tempNode.leftChild;
} else {
tempNode = (BinaryCharTree) tempStack.pop();
tempNode = tempNode.rightChild;
} // Of if
} // Of while
} // Of preOrderVisitWithStack
/**
********************
* Post-order visit with stack.
********************
*/
public void postOrderVisitWithStack() {
ObjectStack tempStack = new ObjectStack();
BinaryCharTree tempNode = this;
ObjectStack tempOutputStack = new ObjectStack();
while (!tempStack.isEmpty() || tempNode != null) {
if (tempNode != null) {
// Store for output.
tempOutputStack.push(new Character(tempNode.value));
tempStack.push(tempNode);
tempNode = tempNode.rightChild;
} else {
tempNode = (BinaryCharTree) tempStack.pop();
tempNode = tempNode.leftChild;
} // Of if
} // Of while
// Now reverse output.
while (!tempOutputStack.isEmpty()) {
System.out.print("" + tempOutputStack.pop() + " ");
} // Of while
} // Of postOrderVisitWithStack
运行结果:

这篇博客介绍了如何使用栈来实现Java中的二叉树前序和后序遍历。在前序遍历中,栈的进栈顺序决定了遍历顺序,而在后序遍历中,遍历结束后根据栈内的元素顺序得到遍历结果。文章通过实例展示了整个过程,并提供了代码和数据测试。
4万+

被折叠的 条评论
为什么被折叠?



