Java第25天——二叉树深度遍历的栈实现(中序)

Java第25天——二叉树深度遍历的栈实现(中序),第1张

Java第25天——二叉树深度遍历的栈实现(中序)

既然是用栈实现的,那么肯定不是简单的只在二叉树的代码中完成,要先写一个栈类,然后再improt引入这个方法。

所以,栈程序。

1,改写之前的栈程序,将char转换成Object,里面存放对象。

2,该程序放在package dataStructure.stack 里面。

3,还是依靠强制类型转换,支持不同的数据类型(其实用泛型也可以做到)。

4,增加了isEmpty() 方法。

代码

package dataStructure.stack;


public class ObjectStack {

	// 栈的深度
	public static final int MAX_DEPTH = 10;
	// 实际深度
	static int depth;
	// 存放数据的地方
	Object[] data;

	// 构造一个空的线性表
	public ObjectStack() {
		depth = 0;
		data = new Object[MAX_DEPTH];
	}// of the first constructor

	// 重写Object中的toString方法
	public String toString() {
		String resultString = "";
		for (int i = 0; i < depth; i++) {
			resultString += data[i];
		} // of for i
		return resultString;
	}// of toString

	
	public boolean push(Object paraObject) {
		if (depth == MAX_DEPTH) {
			System.out.println("栈满.");
			return false;
		} // of if

		data[depth] = paraObject;
		depth++;

		return true;
	}

	
	public Object pop() {
		if (depth == 0) {
			System.out.println("没有元素可以出队了!");
			return '';
		} // of if

		Object resultObject = data[depth - 1];
		depth--;

		return resultObject;
	}// of pop

	
	public boolean isEmpty() {
		if (depth == 0) {
			return true;
		} // of if

		return false;
	}// of isEmpty

	
	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("当前栈中元素为:" + tempStack);
		} // of for ch

		char tempChar;
		for (int i = 0; i < 12; i++) {
			tempChar = ((Character) tempStack.pop()).charValue();
			System.out.println("将该元素出栈:" + tempChar);
			System.out.println("当前栈中元素为:" + tempStack);
			
		} // of for i

	}// of main
}// of class ObjectStack

中序遍历

	
	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("rn前序遍历:");
		temptree.preOrderVisit();
		System.out.println("rn中序遍历:");
		temptree.inOrderVisit();
		System.out.println("rn后序遍历:");
		temptree.postOrderVisit();

		System.out.println("rnrn深度为: " + temptree.getDepth());
		System.out.println("节点总数为: " + temptree.getNumNodes());

		temptree.toDataArrays();
		System.out.println("数据为: " + Arrays.toString(temptree.valuesArray));
		System.out.println("索引为: " + Arrays.toString(temptree.indicesArray));

		temptree.toDataArraysObjectQueue();
		System.out.println("仅对象队列");
		System.out.println("数据为: " + Arrays.toString(temptree.valuesArray));
		System.out.println("索引为: " + Arrays.toString(temptree.indicesArray));
		System.out.println();

		char[] tempCharArray = { 'A', 'B', 'C', 'D', 'E', 'F' };
		int[] tempIndicesArray = { 0, 1, 2, 4, 5, 12 };
		System.out.println("数据为: " + Arrays.toString(tempCharArray));
		System.out.println("索引为: " + Arrays.toString(tempIndicesArray));
		System.out.println();
		BinaryCharTree temptree2 = new BinaryCharTree(tempCharArray, tempIndicesArray);

		System.out.println("rn前序遍历:");
		temptree2.preOrderVisit();
		System.out.println("rn中序遍历:");
		temptree2.inOrderVisit();
		System.out.println("rn后序遍历:");
		temptree2.postOrderVisit();
		
		System.out.println("rnIn-order visit with stack:");
		temptree2.inOrderVisitWithStack();
	}//of main

运行结果


前序遍历:
a b d f g c e 
中序遍历:
b f d g a e c 
后序遍历:
f g d b e c a 

深度为: 4
节点总数为: 7
队列中已经没有元素
数据为: [a, b, c, d, e, f, g]
索引为: [0, 1, 2, 4, 5, 9, 10]
tempIndex = 0
仅对象队列
数据为: [a, b, c, d, e, f, g]
索引为: [0, 1, 2, 4, 5, 9, 10]

数据为: [A, B, C, D, E, F]
索引为: [0, 1, 2, 4, 5, 12]

索引 0 vs. 1
连接 0 和 1
索引 0 vs. 2
连接 0 和 2
索引 0 vs. 4
索引 1 vs. 4
连接 1 和 3
索引 0 vs. 5
索引 1 vs. 5
索引 2 vs. 5
连接 2 和 4
索引 0 vs. 12
索引 1 vs. 12
索引 2 vs. 12
索引 4 vs. 12
索引 5 vs. 12
连接 4 和 5

前序遍历:
A B D C E F 
中序遍历:
B D A E F C 
后序遍历:
D B F E C A 
In-order visit with stack:
B D A E F C 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5695477.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存