- Java 树与二叉树(三)
- 一、二叉树深度遍历的栈实现 (中序)
- 1. 具有通用性的对象栈
- 2. 中序遍历
- 二、二叉树深度遍历的栈实现 (前序和后序)
学习来源: 日撸 Java 三百行(21-30天,树与二叉树) 一、二叉树深度遍历的栈实现 (中序) 1. 具有通用性的对象栈
(1)改写栈程序, 里面存放对象.
(2)该程序应该放在 datastructure.stack 包内.
(3)还是依靠强制类型转换, 支持不同的数据类型.
(4)增加了 isEmpty() 方法.
代码如下:
package datastructure.stack;
/**
*
* @author Ling Lin E-mail:linling0.0@foxmail.com
*
* @version 创建时间:2022年4月21日 下午6:52:54
*
*/
public class ObjectStack {
// The depth
public static final int MAX_DEPTH = 10;
// The actual depth.
int depth;
// The data
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.
*/
@Override
public String toString() {
String resultString = " ";
for (int i = 0; i < depth; i++) {
resultString += data[i];
} // Of for i
return resultString;
}// Of toString
/**
* Push an element.
*
* @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
/**
*********************
* Pop an element.
*
* @return The object at the top of the stack.
*********************
*/
public Object pop() {
if (depth == 0) {
System.out.println("Nothing to pop.");
return ';'}
// of if Object
= resultObject [ data-depth 1 ];--
depth;return
; resultObject}
// Of pop/**
* Is the stack empty?
*
* @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// TODO Auto-generated method stub {
ObjectStack
= tempStack new ObjectStack ();for
( char= ch 'a' ;< ch 'm' ;++ ch). {
tempStackpush(newCharacter ()ch);System
..outprintln("The current stack is: "+ ) tempStack;}
// Of for i char
; tempCharfor
( int= i 0 ;< i 12 ;++ i)= {
tempChar ( (Character). tempStackpop()).charValue();System
..outprintln("Poped: "+ ) tempChar;System
..outprintln("The current stack is: "+ ) tempStack;}
// Of for i }
// Of main}
// Of class ObjectStack/**
* In-order visit with stack.
*/
运行结果:
(1)代码依然短.
(2)中序是几种遍历中最简单的.
(3)具体设计思路自己琢磨, 用几个例子来运行就懂了.
代码如下:
public
void inOrderVisitWithStack ()ObjectStack {
= tempStack new ObjectStack ();BinaryCharTree
= tempNode this ;while
( !.tempStackisEmpty()|| != tempNode null )if {
( !=tempNode null ). {
tempStackpush()tempNode;=
tempNode . tempNode;leftChild}
else = {
tempNode ( BinaryCharTree). tempStackpop();System
..outprint(""+ . tempNode+value " " );=
tempNode . tempNode;rightChild}
// Of if }
// Of while }
// Of inOrderVisitWithStack/**
* Pre-order visit with stack.
*/
二、二叉树深度遍历的栈实现 (前序和后序)
以为前序和后序要比中序难很多, 其实并没有.
-
前序与中序的区别, 仅仅在于输出语句的位置不同.
-
二叉树的遍历, 总共有 6 种排列: 1) 左中右 (中序); 2) 左右中 (后序); 3) 中左右 (前序); 4) 中右左; 5) 右左中; 6) 右中左. 我们平常关心的是前三种, 是因为我们习惯于先左后右. 如果要先右后左, 就相当于左右子树互换, 这个是很容易做到的.
-
如果将前序的左右子树互换, 就可得到 4) 中右左; 再进行逆序, 可以得到 2) 左右中. 因此, 要把前序的代码改为后序, 需要首先将 leftChild 和 rightChild 互换, 然后用一个栈来存储需要输出的字符, 最终反向输出即可. 这种将一个问题转换成另一个等价问题的方式, 无论在数学还是计算机领域, 都极度重要. 参见 https://blog.csdn.net/minfanphd/article/details/117318844 中莫峦奇的版本.
-
如果不按上述方式, 直接写后序遍历, 就会复杂得多, 有双重的 while 循环. 参见 https://blog.csdn.net/minfanphd/article/details/117318844 中潘佳豪的版本.
代码如下:
public
void preOrderVisitWithStack ()ObjectStack {
= tempStack new ObjectStack ();BinaryCharTree
= tempNode this ;while
( !.tempStackisEmpty()|| != tempNode null )if {
( !=tempNode null )System {
..outprint(""+ . tempNode+value " " );.
tempStackpush()tempNode;=
tempNode . tempNode;leftChild}
else = {
tempNode ( BinaryCharTree). tempStackpop();=
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
( !.tempStackisEmpty()|| != tempNode null )if {
( !=tempNode null )// Store for output. {
.
tempOutputStackpush(newCharacter (.tempNode)value);.
tempStackpush()tempNode;=
tempNode . tempNode;rightChild}
else = {
tempNode ( BinaryCharTree). tempStackpop();=
tempNode . tempNode;leftChild}
// Of if }
// Of while // Now reverse ouput.
while
( !.tempOutputStackisEmpty())System {
..outprint(""+ . tempOutputStackpop()+ " " );}
// of while }
// Of postOrderVisitWithStack/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
public
static void main (String[ args])BinaryCharTree {
= tempTree manualConstructTree ();System
..outprintln("\r\nPreorder visit:");.
tempTreepreOrderVisit();System
..outprintln("\r\nIn-order visit:");.
tempTreeinOrderVisit();System
..outprintln("\r\nPost-order visit:");.
tempTreepostOrderVisit();System
..outprintln("\r\n\r\nThe depth is: "+ . tempTreegetDepth());System
..outprintln("The number of nodes is: "+ . tempTreegetNumNodes());.
tempTreetoDataArrays();System
..outprintln("The values are: "+ Arrays .toString(.tempTree)valuesArray);System
..outprintln("The indices are: "+ Arrays .toString(.tempTree)indicesArray);.
tempTreetoDataArraysObjectQueue();System
..outprintln("Only object queue.");System
..outprintln("The values are: "+ Arrays .toString(.tempTree)valuesArray);System
..outprintln("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
..outprintln("\r\nPreorder visit:");.
tempTree2preOrderVisit();System
..outprintln("\r\nIn-order visit:");.
tempTree2inOrderVisit();System
..outprintln("\r\nPost-order visit:");.
tempTree2postOrderVisit();System
..outprintln("\r\nIn-order visit with stack:");.
tempTree2inOrderVisitWithStack();System
..outprintln("\r\nPre-order visit with stack:");.
tempTree2preOrderVisitWithStack();System
..outprintln("\r\nPost-order visit with stack:");.
tempTree2postOrderVisitWithStack();}
// Of main}
// Of class BinaryCharTree
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)