ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

日撸代码300行:第25天(二叉树深度遍历的栈实现 (中序))

2022-01-28 22:02:43  阅读:113  来源: 互联网

标签:25 return 300 System depth 二叉树 tempTree println out


代码来自闵老师”日撸 Java 三百行(21-30天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975721

package datastructure.stack;

/**
 * 
 * Object stack.
 * 
 * @author WX873
 *
 */

public class ObjectStack {
	
	/**
	 * The depth.
	 */
	public static final int MAX_DEPTH = 10;
	
	/**
	 * The data.
	 */
	Object[] data;
	
	/**
	 * The actual depth. 
	 */
	int depth;
	
	/**
	 * construct an empty char stack.
	 */
	public ObjectStack() {
		// TODO Auto-generated constructor stub
		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 = "";
		
		if (depth == 0) {
			System.out.println("The stack is empty!");
			return null;
		}//of if
		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 Push 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("No element to pop!");
			return '\0';
		}//of if
		
		depth--;
		return data[depth];
	}//of pop
	
	/**
	 * ************************************************************
	 * Is the stack empty?
	 * 
	 * @return True, if empty.
	 * ************************************************************
	 */
	public boolean isEmpty() {
		if (depth == 0) {
			System.out.println("The stack is empty!");
			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.toString());
		}//of for ch
		
		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.toString());
		}//of for i
		
	}//of main

}//of ObjectStack

这里自己写代码的时候,将pop()中的if语句里的返回值写成了“return null;”,结果运行的时候就报错。网上查了一下,‘\0’表示空字符。相当于这里还是有返回值,只是返回值为空字符。
return null;方法的返回值必须不是原始数据类型(封装类除外)和void。return 就是跳出方法;return null也是跳出方法并返回null。不要认为null就是没有值,null就是值。
原始类型 | 封装类
boolean | Boolean
char | Character
byte | Byte
short | Short
int | Integer
long | Long
float | Float
double | Double
关于原始类型与原始封装类的介绍:https://www.cnblogs.com/wisdo/p/4915774.html

/**
	 * ******************************************************
	 * 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.println("" + tempNode.value + "");
				tempNode = tempNode.rightChild;
			}
		}//of while
		
	}//of inOrderVisitWithStack
	
	/**
	 * *********************************************************
	 * The entrance of the program.
	 * 
	 * @param args  Not used now
	 * *********************************************************
	 */
	public static void main(String args[]) {
		BinaryCharTree tempTree = manualConstructTree();
		
		System.out.println("\r\nPre-order visit:");
		tempTree.preOrderVisit();
		
		System.out.println("\r\nIn-order visit:");
		tempTree.inOrderVisit();
		
		System.out.println("\r\nPost-order visit:");
		tempTree.postOrderVisit();
		
		int tempDepth = tempTree.getDepth();
		System.out.println("\nThe depth is: " + tempDepth);
		
		System.out.println("\rThe number of nodes for the binary tree is: " + tempTree.getNumNodes());
		
		tempTree.toDataArrays();
		System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
		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.valueArray));
	   	System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
	   	
		tempTree.toDataArrayGenericQueue();
		System.out.println("Generic queue.");
	   	System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
	   	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};
	   	char[] tempCharArray = {'a','b','c','d','e','f','g'};
	   	int[] tempIndicesArray = {0, 1, 2, 4, 5, 9, 10};
	   	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

inOrderVisitWithStack()的设计思路相当巧妙,用一个while循环同时实现了入栈和出栈;入栈后指向左孩子节点,出栈后指向右孩子节点。

标签:25,return,300,System,depth,二叉树,tempTree,println,out
来源: https://blog.csdn.net/u010619558/article/details/122733199

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有