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

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

所以,栈程序。

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

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

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

4,增加了isEmpty() 方法。

代码

package dataStructure.stack;

/**
 * Circle int queue
 * 
 * @author goudiyuan
 *
 */
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

	/**
	 * Push an element
	 * 
	 * @param paraObject the given Object
	 * @return Success or not
	 */
	public boolean push(Object paraObject) {
		if (depth == MAX_DEPTH) {
			System.out.println("栈满.");
			return false;
		} // of if

		data[depth] = paraObject;
		depth++;

		return true;
	}

	/**
	 * Pop an element
	 * 
	 * @return The object at the top of the stack.
	 */
	public Object pop() {
		if (depth == 0) {
			System.out.println("没有元素可以出队了!");
			return '\0';
		} // of if

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

		return resultObject;
	}// of pop

	/**
	 * Id 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) {
		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

中序遍历

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

		System.out.println("\r\n\r\n深度为: " + 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("\r\n前序遍历:");
		temptree2.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		temptree2.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		temptree2.postOrderVisit();
		
		System.out.println("\r\nIn-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 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值