二叉树 Java

package binarytree;

import java.util.*;

public class BinaryTree {
	public Node root = new Node();
	private static int maxsize = 100;

	public BinaryTree() {
		root = null;
	}

	public void Insert(int value) {
		Node tmp = root;
		Node pNode = root;
		int flag = 0;
		if (null == root) {
			root = new Node(value);
			return;
		}
		while (tmp != null)
			if (value > tmp.value) {
				pNode = tmp;
				tmp = tmp.right;
				flag = 0;
			} else if (value < tmp.value) {
				pNode = tmp;
				tmp = tmp.left;
				flag = 1;
			} else
				return;
		tmp = new Node(value);
		if (0 == flag)
			pNode.right = tmp;
		else
			pNode.left = tmp;
	}

	public void MidShow(Node node) {
		if (null == node)
			return;
		MidShow(node.left);
		System.out.print(node.value + "\t");
		MidShow(node.right);
	}
	public static void MidShow2(Node t) {  
        Stack<Node> s = new Stack<Node>();  
        while (t != null || !s.empty()) {  
            while (t != null) {  
                s.push(t);  
                t = t.left;  
            }  
            if (!s.empty()) {  
                t = s.pop();  
                System.out.print(t.value + "\t");  
                t = t.right;  
            }  
        }  
    }
	public static void FrontShow2(Node t) {  
        Stack<Node> s = new Stack<Node>();  
        while (t != null || !s.empty()) {  
            while (t != null) {  
                System.out.print(t.value + "\t");  
                s.push(t);  
                t = t.left;  
            }  
            if (!s.empty()) {  
                t = s.pop();  
                t = t.right;  
            }  
        }  
    } 
	public static void BackShow2(Node t) {  
        Stack<Node> s = new Stack<Node>();  
        Stack<Integer> s2 = new Stack<Integer>(); 
        Integer i = new Integer(1);
        while (t != null || !s.empty()) {  
            while (t != null) {  
                s.push(t);  
                s2.push(new Integer(0));  
                t = t.left;  
            }  
            while (!s.empty() && s2.peek().equals(i)) {  
                s2.pop();  
                System.out.print(s.pop().value + "\t");  
            }  
  
            if (!s.empty()) {  
                s2.pop();  
                s2.push(new Integer(1));  
                t = s.peek();  
                t = t.right;  
            }  
        }  
    }  
	public void FrontShow(Node node) {
		if (null == node)
			return;
		System.out.print(node.value + "\t");
		FrontShow(node.left);
		FrontShow(node.right);
	}

	public void BackShow(Node node) {
		if (null == node)
			return;
		BackShow(node.left);
		BackShow(node.right);
		System.out.print(node.value + "\t");
	}

	public boolean MyFind(Node node, int value) {
		if (null == node)
			return false;
		else if (node.value > value)
			return MyFind(node.left, value);
		else if (node.value < value)
			return MyFind(node.right, value);
		else
			return true;
	}

	public static void main(String[] args) {
		BinaryTree mytree = new BinaryTree();
		Random r = new Random();
		int tmp;
		for (int i = 0; i < 5; i++) {
			tmp = r.nextInt(maxsize);
			System.out.print(tmp + "->");
			mytree.Insert(tmp);
		}
		System.out.println();
		System.out.println("Front Show!");
		mytree.FrontShow(mytree.root);
		System.out.println("\nFront Show UnReverse!");
		FrontShow2(mytree.root);
		System.out.println("\nMid Show!");
		mytree.MidShow(mytree.root);
		System.out.println("\nMid Show UnReverse!");
		MidShow2(mytree.root);
		System.out.println("\nBack Show!");
		mytree.BackShow(mytree.root);
		System.out.println("\nBack Show UnReverse!");
		BackShow2(mytree.root);
		System.out
				.println("\nPlease input the data  to be found in the tree :");
		Scanner in = new Scanner(System.in);
		int inputnum = in.nextInt();
		in.close();
		System.out.println(mytree.MyFind(mytree.root, inputnum));
	}
}

package binarytree;

public class Node {
	public int value;
	public Node left;
	public Node right;
	public Node(int value)
	{
		this.value = value;
		left = right = null;
	}
	public Node()
	{
		
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值