二叉树的遍历

1.结点类
package edu.tcu.soft.binary;

public class BinaryNode {

	private int value;
	private BinaryNode left;
	private BinaryNode right;

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public BinaryNode getLeft() {
		return left;
	}

	public void setLeft(BinaryNode left) {
		this.left = left;
	}

	public BinaryNode getRight() {
		return right;
	}

	public void setRight(BinaryNode right) {
		this.right = right;
	}
    
	
	public BinaryNode(int value, BinaryNode left, BinaryNode right) {
		super();
		this.value = value;
		this.left = left;
		this.right = right;
	}
    
	public BinaryNode() {
	}
    
	
	public BinaryNode(int value) {
		
		this.left=null;
		this.right=null;
		this.value=value;
	}
}


 
2.二叉树类
<pre name="code" class="java">package edu.tcu.soft.binary;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinaryTree {

	public BinaryTree() {

	}

	// 插入结点
	public void insert(BinaryNode root, int data) {

		if (data > root.getValue()) // 二叉树的左节点都比根节点小
		{
			if (root.getRight() == null) {
				root.setRight(new BinaryNode(data));
			} else {
				this.insert(root.getRight(), data);
			}
		} else { // 二叉树的右节点都比根节点大
			if (root.getLeft() == null) {
				root.setLeft(new BinaryNode(data));
			} else {
				this.insert(root.getLeft(), data);
			}
		}
	}

	// 前序遍历
	public void preOrder(BinaryNode root) {

		// -------------------1.递归
		// if (root == null) {
		// return;
		// } else {
		// System.out.print(root.getValue() + " ");// 输出结点的数据域
		// preOrder(root.getLeft()); // 遍历左子树
		// preOrder(root.getRight());//遍历右子树
		// }

		// -----------------2.非递归
		Stack<BinaryNode> stack = new Stack<BinaryNode>();
		while (root != null || !stack.empty())// 当根节点为空或者栈为空才跳出循环
		{
			while (root != null) {
				System.out.print(root.getValue() + "  ");
				stack.push(root);
				root = root.getLeft();
			}
			if (!stack.empty()) {
				root = stack.pop();
				root = root.getRight();
			}
		}
	}

	// 中序遍历
	public void inOrder(BinaryNode root) {

		// ----------------1.递归
		//		if (root == null)
		//			return;
		//		else {
		//			inOrder(root.getLeft());
		//			System.out.print(root.getValue() + "  ");
		//			inOrder(root.getRight());
		//		}

		// -----------------2.非递归
		Stack<BinaryNode> stack = new Stack<BinaryNode>();
		BinaryNode s = new BinaryNode();
		while (root != null || !stack.empty()) {
			while (root != null) {
				stack.push(root);
				root = root.getLeft();
			}
			if (!stack.empty()) {
				s = stack.pop();
				System.out.print(s.getValue() + " ");
				root = s.getRight();
			}

		}
	}

	// 后序遍历
	public void postOrder(BinaryNode root) {
		//--------------递归
		//		 if(root==null)
		//			return;
		//		else{
		//			postOrder(root.getLeft());
		//			postOrder(root.getRight());
		//			System.out.println(root.getValue());
		//		}

		// ---------------非递归 ----使用了两个栈
		Stack<BinaryNode> stack = new Stack<BinaryNode>();
		Stack<Integer> stack2 = new Stack<Integer>();
		Integer i = new Integer(1);
		while (root != null || !stack.empty()) {
			while (root != null) {
				stack.push(root);
				stack2.push(new Integer(0));
				root = root.getLeft();
			}
			while (!stack.empty() && stack2.peek().equals(i)) {
				stack2.pop();
				System.out.print(stack.pop().getValue() + "  ");
			}
			if (!stack.empty()) {
				stack2.pop();
				stack2.push(new Integer(1));
				root = stack.peek();
				root = root.getRight();
			}
		}

		// ---------------------非递归 --------使用一个栈
                //还没实现    待续
	}

	// 层序遍历
	public void levelOrder(BinaryNode root) {

		if (root == null)
			return;
		else {
			Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
			queue.add(root);
			while (!queue.isEmpty()) {
				BinaryNode node = queue.remove();
				System.out.print(node.getValue() + "-");
				if (node.getLeft() != null)
					queue.add(node.getLeft());
				if (node.getRight() != null)
					queue.add(node.getRight());
			}
		}
	}

	// 释放结点
	public void release(BinaryNode node) {
		if (node == null)
			return;
		else {
			release(node.getLeft());
			release(node.getRight());
			node = null;
		}
	}
}


 

3.测试类

package edu.tcu.soft.binary;

public class Test {
   public static void main(String[] args) {
	   
	   int a[]={10,12,9,8,15,14};
	   BinaryNode root=new BinaryNode(a[0]);
	   BinaryTree tree=new BinaryTree();
	   for(int i=1;i<a.length;i++){
	    tree.insert(root, a[i]);       //向二叉树中插入数据
	   }
	   
//	   tree.preOrder(root);
//	   System.out.println();
//	   tree.inOrder(root);
//	   System.out.println();
	   tree.levelOrder(root);
}
}


其实,这是我第一次用java写关于数据结构的东西,在写这个的时候,遇到了空指针异常,就是在遍历的时候只打印了根节点的值,其他的节点打印不出,而且还报空指针异常,最后我才发现我在为左右字树赋值的时候,没有给左右子树实例化。我居然忘了在使用对象之前需要实例化的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值