二叉树 in Java

本文详细介绍了使用Java实现二叉树中序遍历的两种方法:递归调用和手动写栈模拟。通过对比,展示了递归方法简洁但可能导致栈溢出的问题,以及手动栈实现的复杂性与优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在看 Data Structures outside in Java

看到 二叉树 于是自己学着写了一个

 

运行结果是 中序遍历:

The recursive travell:
D B F H E G A C
The stack way travell:
D B F H E G A C

 

Notes:

遍历的方法

1:递归调用: 思路清晰,编程简单。但是问题也很严重,需要消耗很多的栈空间,经过测试,我把节点开到10000的时候 StackOverFlow了,同时时间效率可能也不高。

2:手动写栈,模拟:代码复杂了一点,但是不会暴栈

 

import java.util.Date;
import java.util.Stack;

/**
 * Just a demo about BinaryTree in Java
 * @author Jason
 * 2010.5.22
 * Notes: If wants to attach of delete  a subTree of a  node, just modify the leftChild/rightChild attribute.
 */
public class BinaryTree<T> {

	private T data;
	public BinaryTree<T> parent;
	public BinaryTree<T> leftChild;
	public BinaryTree<T> rightChild;
	
	// constructor 
	public BinaryTree ()
	{
		this.data=null;
		this.parent=null;
		this.leftChild=null;
		this.rightChild=null;
	}
	// constructor without para
	public BinaryTree (T data)
	{
		this.data=data;
		this.parent=null;
		this.leftChild=null;
		this.rightChild=null;
	}
	
	public void setData(T data)
	{
		this.data=data;
	}
	public T getData()
	{
		return this.data;
	}
	
	// inorder travell with recursive way
	public void travellRecursive(BinaryTree<T> root)
	{
		if(root.leftChild!=null)	travellRecursive(root.leftChild);
		System.out.print(root.getData()+" ");
		if(root.rightChild!=null) travellRecursive(root.rightChild);
	}
	
	
	// inorder travell with self made stack
	public void travellStack( BinaryTree<T> root)
	{
		if(root==null)	return;
		
		class StackNode
		{
			public int millStone;
			public BinaryTree<T> node;
			public StackNode ( BinaryTree<T> n, int m)
			{
				this.node=n;
				this.millStone=m;
			}
		}
		
		java.util.Stack<StackNode> stack=new Stack<StackNode>();
		stack.push( new StackNode(root, 1) );
		
		StackNode temp;
		while(!stack.empty())
		{
			temp=stack.pop();
			switch (temp.millStone)
			{
				case 1:
					temp.millStone=2;
					stack.push(temp);
					if(temp.node.leftChild!=null)
						stack.push( new StackNode(temp.node.leftChild, 1) );
					break;
				case 2:
					temp.millStone=3;
					stack.push(temp);
					System.out.print(temp.node.getData()+" ");
					if(temp.node.rightChild!=null)
						stack.push( new StackNode(temp.node.rightChild, 1) );
					break;
				case 3:
					break;
				default:
					break;
			}
		}
		
	}
	
	// find the whole root of this tree
	public BinaryTree<T> findRoot()
	{
		if(this.parent==null)
			return this;
		BinaryTree<T> nextParent=this.parent;
		while(nextParent.parent!=null)
			nextParent=nextParent.parent;
		return nextParent;
	}
	
	// this is just a demo to test
	static BinaryTree<String> generateTree()
	{
		BinaryTree<String> treeA=new BinaryTree<String>("A");
		BinaryTree<String> treeB=new BinaryTree<String>("B");
		BinaryTree<String> treeC=new BinaryTree<String>("C");
		BinaryTree<String> treeD=new BinaryTree<String>("D");
		BinaryTree<String> treeE=new BinaryTree<String>("E");
		BinaryTree<String> treeF=new BinaryTree<String>("F");
		BinaryTree<String> treeG=new BinaryTree<String>("G");
		BinaryTree<String> treeH=new BinaryTree<String>("H");
		
		treeF.rightChild=treeH;		treeH.parent=treeF;
		treeE.rightChild=treeG;		treeG.parent=treeE;
		treeE.leftChild=treeF;		treeF.parent=treeE;
		treeB.rightChild=treeE;		treeE.parent=treeB;
		treeB.leftChild=treeD;		treeD.parent=treeB;
		treeA.rightChild=treeC;		treeC.parent=treeA;
		treeA.leftChild=treeB;		treeB.parent=treeA;
		
//		BinaryTree<String> BinaryFather=treeH;
//		for(int i=1;i<=10000;i++)
//		{
//			BinaryTree<String> treeTemp=new BinaryTree<String>(String.valueOf(i));
//			treeTemp.parent=BinaryFather;
//			BinaryFather.leftChild=treeTemp;
//			BinaryFather=treeTemp;
//		}
		return treeH;
	}
	
	
	public static void main(String[] args) {
		BinaryTree<String> root=generateTree();
		root=root.findRoot();
		
		Date date1=new Date();
		System.out.println("The recursive travell:");
		root.travellRecursive(root);
		//System.out.println("Time cost is :"+(new Date().getTime()-date1.getTime()));
		System.out.println();
		
		date1=new Date();
		System.out.println("The stack way travell:");
		root.travellStack(root);
		//System.out.println("Time cost is :"+(new Date().getTime()-date1.getTime()));
	}

}

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值