二叉树的实现与遍历

业余学习整理
public class BinaryTreeNode<T> {
	
	private T data;
	private BinaryTreeNode<T> leftChild;
	private BinaryTreeNode<T> rightChild;
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	public BinaryTreeNode<T> getLeftChild() {
		return leftChild;
	}
	public void setLeftChild(BinaryTreeNode<T> leftChild) {
		this.leftChild = leftChild;
	}
	public BinaryTreeNode<T> getRightChild() {
		return rightChild;
	}
	public void setRightChild(BinaryTreeNode<T> rightChild) {
		this.rightChild = rightChild;
	}
	
}

public class BinaryTree<T> {
	
	private BinaryTreeNode<T> root;
	
	public BinaryTree(){
		this.root = null;
	}
	
	public BinaryTree(BinaryTreeNode<T> root,BinaryTreeNode<T> leftChild,BinaryTreeNode<T> rightChild){
		this.root = new BinaryTreeNode<T>();
		this.root.setLeftChild(leftChild);
		this.root.setRightChild(rightChild);
	}
	
	public BinaryTreeNode<T> getRoot() {
		return root;
	}

	public void setRoot(BinaryTreeNode<T> root) {
		this.root = root;
	}
	
	public void print(){
		this.print(this.root);
	}
	
	private void print(BinaryTreeNode<?> node){
		if(null == node){
			return;
		}
		System.out.print(node.getData());
		this.print(node.getLeftChild());
		this.print(node.getRightChild());
	}
	
	public interface BinaryTreeNodeSort{
		public void run(BinaryTreeNode<?> node);
		public void print(Object data);
	}
	
	public static abstract class AbsBinaryTreeNodeSort implements BinaryTreeNodeSort{
		@Override
		public void print(Object data) {
			if(null == data){
				return;
			}
			System.out.print(data);
		}
	}
	
	public enum Traversal{
		//分层
		LEVEL(new AbsBinaryTreeNodeSort(){
			@Override
			public void run(BinaryTreeNode<?> node) {
				if(null == node){
					return;
				}
				Queue<BinaryTreeNode<?>> queue = new LinkedList<BinaryTreeNode<?>>();
				queue.add(node);
				while(!queue.isEmpty()){
					BinaryTreeNode<?> n = queue.remove();
					this.print(n.getData());
					if(null != n.getLeftChild()){
						queue.add(n.getLeftChild());
					}
					if(null != n.getRightChild()){
						queue.add(n.getRightChild());
					}
				}
			}
		}),
		/**
		 * N currentNode ,L leftNode ,R rightNode
		 */
		NLR(new AbsBinaryTreeNodeSort(){
			@Override
			public void run(BinaryTreeNode<?> node) {
				if(null == node){return;}
				this.print(node.getData());
				this.run(node.getLeftChild());
				this.run(node.getRightChild());
			}
		}),
		LNR(new AbsBinaryTreeNodeSort(){
			@Override
			public void run(BinaryTreeNode<?> node) {
				if(null == node){return;}
				this.run(node.getLeftChild());
				this.print(node.getData());
				this.run(node.getRightChild());
			}
		}),
		LRN(new AbsBinaryTreeNodeSort(){
			@Override
			public void run(BinaryTreeNode<?> node) {
				if(null == node){return;}
				this.run(node.getLeftChild());
				this.run(node.getRightChild());
				this.print(node.getData());
			}
		}),
		NRL(new AbsBinaryTreeNodeSort(){
			@Override
			public void run(BinaryTreeNode<?> node) {
				if(null == node){return;}
				this.print(node.getData());
				this.run(node.getRightChild());
				this.run(node.getLeftChild());
			}
		}),
		RNL(new AbsBinaryTreeNodeSort(){
			@Override
			public void run(BinaryTreeNode<?> node) {
				if(null == node){return;}
				this.run(node.getRightChild());
				this.print(node.getData());
				this.run(node.getLeftChild());
			}
		}),
		RLN(new AbsBinaryTreeNodeSort(){
			@Override
			public void run(BinaryTreeNode<?> node) {
				if(null == node){return;}
				this.run(node.getRightChild());
				this.run(node.getLeftChild());
				this.print(node.getData());
			}
		});
		private Traversal(BinaryTreeNodeSort sort){
			this.sort = sort;
		}
		private BinaryTreeNodeSort sort;
		public BinaryTreeNodeSort getSort() {
			return sort;
		}
		public void setSort(BinaryTreeNodeSort sort) {
			this.sort = sort;
		}
		public void print(BinaryTreeNode<?> node){
			this.sort.run(node);
		}
	}

	public static <I> BinaryTreeNode<I> createTreeNode(I data,BinaryTreeNode<I> leftChild,BinaryTreeNode<I> rightChild){
		BinaryTreeNode<I> node = new BinaryTreeNode<I>();
		node.setData(data);
		if(null != leftChild){
			node.setLeftChild(leftChild);
		}
		if(null != rightChild){
			node.setRightChild(rightChild);
		}
		return node;
	}
	
	public static void main(String[] args) {
		BinaryTreeNode<String> d = BinaryTree.createTreeNode("D",null,null);
		BinaryTreeNode<String> e = BinaryTree.createTreeNode("E",null,null);
		BinaryTreeNode<String> f = BinaryTree.createTreeNode("F",null,null);
		BinaryTreeNode<String> b = BinaryTree.createTreeNode("B",d,e);
		BinaryTreeNode<String> c = BinaryTree.createTreeNode("C",null,f);
		BinaryTreeNode<String> a = BinaryTree.createTreeNode("A",b,c);
		
		BinaryTree<String> tree = new BinaryTree<String>();
		tree.setRoot(a);
		
		tree.print();
		System.out.println();
		System.out.print("LEVEL:");
		Traversal.LEVEL.print(tree.getRoot());
		System.out.println();
		System.out.print("NLR:");
		Traversal.NLR.print(tree.getRoot());
		System.out.println();
		System.out.print("LNR:");
		Traversal.LNR.print(tree.getRoot());
		System.out.println();
		System.out.print("LRN:");
		Traversal.LRN.print(tree.getRoot());
		System.out.println();
		System.out.print("NRL:");
		Traversal.NRL.print(tree.getRoot());
		System.out.println();
		System.out.print("RNL:");
		Traversal.RNL.print(tree.getRoot());
		System.out.println();
		System.out.print("RLN:");
		Traversal.RLN.print(tree.getRoot());
		System.out.println();
		
		
	}
	
}
//打印结果
ABDECF
LEVEL:ABCDEF
NLR:ABDECF
LNR:DBEACF
LRN:DEBFCA
NRL:ACFBED
RNL:FCAEBD
RLN:FCEDBA


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值