Java数据结构——二叉树深度遍历的栈实现

深度遍历

1. 创建通用型栈

  • ObjectStack
//package cb;

/**
 * 这是一个通用类型的栈
 * @author NoBug
 * @version 3.0
 */
public class ObjectStack {
	
	/**
	 * MAXSIZE = 100
	 */
	public static final int MAXSIZE = 100;
	
	/**
	 * 这是栈的实际占量,整型
	 */
	int stacksize;
	
	/**
	 * 栈存放数据的数组
	 */
	Object[] data;
	
	/**
	 * 构造一个空栈
	 */
	public ObjectStack() {
		this.stacksize = 0;
		this.data = new Object[MAXSIZE];
	} // of construct
	
	/**
	 * toString()
	 */
	public String toString() {
		String result = "";
		
		for(int i=0; i<this.stacksize; i++) {
			result += data[i] + " ";
		}
		
		return result;
	} // of toString
	
	/**
	 * push
	 * @author 己千之
	 * @param elem 类型Object
	 * @return 成功或失败
	 */
	public boolean push(Object elem) {
		
		// 栈满
		if(this.stacksize==MAXSIZE) {
			System.out.println("Stack full.");
			return false;
		}
		
		this.data[this.stacksize] = elem;
		this.stacksize++;
		
		return true;
	}//of push
	
	/**
	 * pop
	 * @author 己千之
	 * @return Object类型
	 */
	public Object pop() {
		
		// 栈空
		if(this.stacksize==0) {
			System.out.println("StackEmpty.");
		}
		
		Object elem = data[this.stacksize-1];
		this.stacksize--;
		
		return elem;
	}//of pop
	
	/**
	 * 判断栈是否为空
	 * @author 己千之
	 * @return true or false 空或者不空
	 */
	public boolean isEmpty() {
		
		// Stack is empty
		if(this.stacksize==0) {
			return true;
		}
		else {
			return false;
		}
	}//of isEmpty
	
}
  • 测试类
//package cb;

public class Test_ObjectStack {

	public static void main(String[] args) {
		ObjectStack S = new ObjectStack();
		S.push(1);
		S.push('b');
		S.push(2);
		S.push("ad");
		S.push(12.1);
		System.out.println(S.isEmpty());
		System.out.println(S.toString());
	}

}
  • 效果图

在这里插入图片描述


2. 二叉树深度遍历的栈实现

  • 深度遍历:前面已经讲过,是前序遍历,中序遍历,后序遍历。

  • 代码

	/**
	 * 中序遍历的栈实现
	 */
	public void inorderTraversalWithStack() {
		ObjectStack stack = new ObjectStack();
		BinaryTree tempNode = this;
		while(!stack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				
				// 入栈
				stack.push(tempNode);
				tempNode = tempNode.lchild;
			} else {
				
				// 出栈
				tempNode = (BinaryTree) stack.pop();
				System.out.print(tempNode.data + " ");
				tempNode = tempNode.rchild;
			}
		} // end while
		
	} // of inorderTraversalWithStack
	
	/**
	 * 先序遍历的栈实现
	 */
	public void preorderTraversalWithStack() {
		ObjectStack stack = new ObjectStack();
		BinaryTree tempNode = this;
		while(!stack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				System.out.print(tempNode.data + " ");
				stack.push(tempNode);
				tempNode = tempNode.lchild;
			} else {
				tempNode = (BinaryTree) stack.pop();
				tempNode = tempNode.rchild;
			}
		} // end while
		
	} // of preorderTraversalWithStack
	
	/**
	 * 后序遍历的栈实现
	 */
	public void postorderTraversalWithStack() {
		ObjectStack stack = new ObjectStack();
		BinaryTree tempNode = this;
		ObjectStack tempStack = new ObjectStack();
		while(!stack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				tempStack.push(tempNode.data);
				stack.push(tempNode);
				tempNode = tempNode.rchild;
			} else {
				tempNode = (BinaryTree) stack.pop();
				tempNode = tempNode.lchild;
			}
		} // end while
		
		while(!tempStack.isEmpty()) {
			System.out.print(tempStack.pop() + " ");
		}
		
	} // of postorderTraversalWithStack
  • 源码
//package cb;


public class BinaryTree {
	
	/**
	 * 数据域
	 */
	Object data;
	
	/**
	 * 左孩子
	 */
	BinaryTree lchild;
	
	/**
	 * 右孩子
	 */
	BinaryTree rchild;
	
	/**
	 * 构造函数,为单个节点,赋值,形成独立的节点
	 * @param elem 
	 */
	public BinaryTree(Object elem) {
		data = elem;
		lchild = null;
		rchild = null;
	} // of construct
	
	/**
	 * 构造函数,创建二叉树的储存结构
	 * @param value 数据域数组
	 * @param key 下标数组
	 */
	public BinaryTree(Object[] value, int[] key) {
		
		// BinaryTree型的数组,装每一个节点
		int node = value.length;
		BinaryTree[] allNodes = new BinaryTree[node];
		for(int i=0; i<node; i++) {
			allNodes[i] = new BinaryTree(value[i]);
		}
		
		// 链接节点
		for(int i=1; i<node; i++) {
			for(int j=0; j<i; j++) {
				if(key[i] == key[j]*2+1) {
					allNodes[j].lchild = allNodes[i];
					break;
				}else if(key[i] == key[j]*2+2) {
					allNodes[j].rchild = allNodes[i];
					break;
				}
			}
		}
		
		// 根节点
		data = allNodes[0].data;
		lchild = allNodes[0].lchild;
		rchild = allNodes[0].rchild;
		
	} // of construct

	/**
	 * 先序遍历
	 */
	public void preorderTraversal() {
		System.out.print(data + " ");
		if(lchild != null) {
			lchild.preorderTraversal();
		}
		if(rchild != null) {
			rchild.preorderTraversal();
		}
	} // of preorderTraversal
	
	/**
	 * 中序遍历
	 */
	public void inorderTraversal() {
		if(lchild != null) {
			lchild.inorderTraversal();
		}
		System.out.print(data + " ");
		if(rchild != null) {
			rchild.inorderTraversal();
		}
	} // of inorderTraversal
	
	/**
	 * 后序遍历
	 */
	public void postorderTraversal() {
		if(lchild != null) {
			lchild.postorderTraversal();
		}
		if(rchild != null) {
			rchild.postorderTraversal();
		}
		System.out.print(data + " ");
	} // of postorderTraversal
	
	/**
	 * 层次遍历
	 * @param T 传进一个二叉树
	 */
	public void levelorderTraversal(BinaryTree T) {
		if(T != null) {
			
			// 不用队列,只能取数组了
			BinaryTree[] B = new BinaryTree[100];
			
			// 根节点第一个
			B[0] = T;
			
			// 巧妙之处 
			int first = 0;
			int after = 1;
			
			// 循环 
			while(first < after) {
				System.out.print(B[first].data + " ");
				if(B[first].lchild != null) {
					B[after] = B[first].lchild;
					after++;
				} // end if
				if(B[first].rchild != null) {
					B[after] = B[first].rchild;
					after++;
				} // end if
				
				first++;			
			} // end while
		} // end if
		
	} // of LevelorderTraversal
	
	/**
	 * @author 己千之
	 * 对照层次遍历,和这个方法,可以很轻松的写出二叉树的结构
	 * @param T
	 */
	public void structOfBinary(BinaryTree T) {
		if(T != null) {
			
			// 不用队列,只能取数组了
			BinaryTree[] B = new BinaryTree[100];
			
			// 根节点第一个
			B[0] = T;
			
			// 巧妙之处 
			int first = 0;
			int after = 1;
			
			// 循环 
			while(first < after) {
				System.out.println(B[first].data + " ");
				
				if(B[first].lchild != null) {
					B[after] = B[first].lchild;
					after++;
				}else {
					System.out.println(B[first].data + "这个节点的左节点是空的 ");
				}
				
				if(B[first].rchild != null) {
					B[after] = B[first].rchild;
					after++;
				}else {
					System.out.println(B[first].data + "这个节点的右节点是空的 ");
				}
				
				first++;			
			} // end while
		} // end if
	}
	
	/**
	 * 二叉树的深度
	 * @return depth 
	 * 			二叉树的深度
	 */
	public int getDepth() {
		
		// 只有根节点
		if((lchild == null)&&(rchild == null)) {
			return 1;
		} // end if
		
		// 左
		int lDepth = 0;
		if(lchild != null) {
			lDepth = lchild.getDepth();
		} // end if
		
		// 右
		int rDepth = 0;
		if(rchild != null) {
			rDepth = rchild.getDepth();
		} // end if
		
		// 左右比较
		if(lDepth >= rDepth) {
			return lDepth+1;
		}
		else {
			return rDepth+1;
		}
		
	} // of getDepth
	
	/**
	 * 二叉树的节点数
	 * @return node
	 * 			二叉树的节点数
	 * 			左 + 右 + 1
	 */
	public int getNode() {
		
		// 只有根节点
		if((lchild == null)&&(rchild == null)) {
			return 1;
		} // end if
		
		// 左节点遍历
		int lNode = 0;
		if(lchild != null) {
			lNode = lchild.getNode();
		} // end if
		
		// 右节点遍历
		int rNode = 0;
		if(rchild != null) {
			rNode = rchild.getNode();
		} // end if
		
		return lNode+rNode+1;
	} // of getNode
	
	/**
	 * 中序遍历的栈实现
	 */
	public void inorderTraversalWithStack() {
		ObjectStack stack = new ObjectStack();
		BinaryTree tempNode = this;
		while(!stack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				
				// 入栈
				stack.push(tempNode);
				tempNode = tempNode.lchild;
			} else {
				
				// 出栈
				tempNode = (BinaryTree) stack.pop();
				System.out.print(tempNode.data + " ");
				tempNode = tempNode.rchild;
			}
		} // end while
		
	} // of inorderTraversalWithStack
	
	/**
	 * 先序遍历的栈实现
	 */
	public void preorderTraversalWithStack() {
		ObjectStack stack = new ObjectStack();
		BinaryTree tempNode = this;
		while(!stack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				System.out.print(tempNode.data + " ");
				stack.push(tempNode);
				tempNode = tempNode.lchild;
			} else {
				tempNode = (BinaryTree) stack.pop();
				tempNode = tempNode.rchild;
			}
		} // end while
		
	} // of preorderTraversalWithStack
	
	/**
	 * 后序遍历的栈实现
	 */
	public void postorderTraversalWithStack() {
		ObjectStack stack = new ObjectStack();
		BinaryTree tempNode = this;
		ObjectStack tempStack = new ObjectStack();
		while(!stack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				tempStack.push(tempNode.data);
				stack.push(tempNode);
				tempNode = tempNode.rchild;
			} else {
				tempNode = (BinaryTree) stack.pop();
				tempNode = tempNode.lchild;
			}
		} // end while
		
		while(!tempStack.isEmpty()) {
			System.out.print(tempStack.pop() + " ");
		}
		
	} // of postorderTraversalWithStack

}
  • 测试类
package cb;

public class Test_BinaryTree {
	
	public static void main(String[] args) {
		Object[] valueArray = {1, 2, 3, 4, 5};
		int[] indexArray = {0, 1, 2, 3, 6};
		BinaryTree BiTree1 = new BinaryTree(valueArray, indexArray);
		System.out.print("先序遍历:");
		BiTree1.preorderTraversalWithStack();;
		System.out.println();
		
		System.out.print("中序遍历:");
		BiTree1.inorderTraversalWithStack();
		System.out.println();
		
		System.out.print("后序遍历:");
		BiTree1.postorderTraversalWithStack();
		System.out.println();
	}
}
  • 效果图

在这里插入图片描述


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜满月

鼓励,鼓励,更加努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值