Java先序序列构造二叉树

BinaryTree.java

package com.anjoyo.data_structures.tree;

import java.util.Scanner;

/** 
 * 先序序列构造二叉树
 * @author HLP
 *
 */
public class BinaryTree {
	static String[] arr = new String[100];
	static int index = 0;
	
	static {
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		String str;
		do {
			str = scanner.next();
			if ("over".equals(str)) {
				break;
			}
			arr[index++] = str;
		} while (true);
		index = 0;
	}
	
	public static TNode create() {
		TNode node = null;
		String data = arr[index++];
		if (! "null".equals(data)) {
			node = new TNode(data);
			node.lChild = create();
			node.rChild = create();
		}
		return node;
	}
	
	public static void print(TNode node) {
		if (node == null) {
			return;
		}else {
			System.out.print(node.data + " --> ");
			print(node.lChild);
			print(node.rChild);
		}
	}
	
	public static void main(String[] args) {
		TNode biTree = null;
		biTree = create();
		
		print(biTree);
	}
}

TNode.java

package com.anjoyo.data_structures.tree;

public class TNode {
	String data;//值
	TNode lChild;//左孩子
	TNode rChild;//右孩子
	
	public TNode(String data) {
		this.data = data;
		this.lChild = null;
		this.rChild = null;
	}
}



测试Test.java

package com.anjoyo.data_structures.tree;

class T {
	int data;
	
	public T(){}
	public T(int data) {
		this.data = data;
	}
}

public class Test {
	
	static T createT(){
		T t2 = new T(32);
//		t.data = 34;
//		t = t2;
		return t2;
	}
	
	public static void main(String[] args) {
		T t = new T();
//		createT(t);
		System.out.println(t);
		System.out.println(t.data);
		
		T t2 = null;
		t2 = createT();
		System.out.println(t2);
		System.out.println(t2.data);
	}
}


版权所有,转载请注明出处!http://blog.csdn.net/kuailebeihun_/article/details/22994755



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值