二叉排序树 の 实现&遍历

看了书上关于二叉树的知识,决定用java来实现一下二叉树。

实现的是二叉排序树,就是小的在左边,大的在右边那个。

然后实现了,中序遍历,先序遍历,后序遍历,层序遍历

然后实现了一下按行打印二叉树。

感觉萌萌哒。


首先,我想说,某些书上的代码是错的,编译都通不过,也出了书。 这算不算误人子弟微笑


然后我想说,为什么学校给买的教材永远都是那么难?! 看都看不懂,一点儿都不接地气骂人


最后我想说,代码还是要自己敲一遍才理解的深刻啊。

以前备考的时候准备这几个遍历,头都大了,每次都算不对,

其实代码一写,真的是一目了然。

以前觉得很难的东西,自己做过了,突然觉得没啥难的,都不知道要讲什么。

就这样吧,贴代码:


/**
 * 
 */
package dianer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/** 
* * @author LilyLee
 * @date  2017年3月19日
 * @Version 
 *
 */
public class BinaryTree {

	public Node root;
	public BinaryTree(){
		root=null;
	}
	
	public static void main(String[] args) {
		BinaryTree biTree=new BinaryTree();
		int [] data={2,8,7,4,9,3,1,6,7,5};
		biTree.root=biTree.buildTree(data);		
		System.out.println("中序遍历结果:");
		biTree.inOrder();
		System.out.println("\n先序遍历结果:");
		biTree.preOrder();
		System.out.println("\n后序遍历结果:");
		biTree.postOrder();
		System.out.println("\n层序遍历结果:");
		biTree.layerTranverse();
		System.out.println("\n 按行打印结果:");
		biTree.layerPrint();
		
	}
	
	public void inOrder(){
		this.inOrder(this.root);
	}
	
	public void preOrder(){
		this.preOrder(this.root);
	}
	
	public void postOrder(){
		this.postOrder(this.root);
	}
	public void layerTranverse(){
		this.layerTranverse(this.root);
	}
	
/*
//将data插入到二叉树中	
public void insert(int data){
	Node newNode=new Node(data);
	if(root==null)
		root=newNode;
	else{
		Node current=root;
		Node parent=null;
		while(true){//寻找插入位置
			parent=current;
			if(data>current.data){ 
				current=current.left;			
				if(current==null){
				parent.left=newNode;
				return;
				}
			else{
				current=current.right;
				if(current==null){
					parent.right=newNode;
					return;}
				}
						
			}
		}
	}
}	
	
*/

	
	
//将数值输入构建二叉树
public Node buildTree(int[] data){
	Node root=new Node(data[0]);
	for(int i=1;i<data.length;i++){
		root.insert(data[i]);
	}
	return root;
}

//中序遍历方法的递归实现
public void inOrder(Node localRoot){
	if(localRoot!=null){
		inOrder(localRoot.left);
		System.out.print(localRoot.data+" ");
		inOrder(localRoot.right);
	}
}

//先序遍历方法的递归实现
public void preOrder(Node localRoot){
	if(localRoot!=null){
		System.out.print(localRoot.data+" ");
		preOrder(localRoot.left);
		preOrder(localRoot.right);
	}
}

//后序遍历的递归实现
public void postOrder(Node localRoot){
	if(localRoot!=null){
		postOrder(localRoot.left);
		postOrder(localRoot.right);
		System.out.print(localRoot.data+" ");
	}
}

//层序遍历二叉树
 /*这个代码是面试宝典上的,我觉得不太好,打印出来的顺序确实是对的
  * 但是不能反应出“层序”的感觉*/
public void layerTranverse(Node localRoot){
	if(root==null) { System.out.println("Null tree!"); return;}
	Queue<Node>q=new LinkedList<Node>();
	q.add(this.root);
	while(!q.isEmpty()){
		Node n=q.poll();
		System.out.print(n.data+" ");
		if(n.left!=null)
			q.add(n.left);
		if(n.right!=null)
			q.add(n.right);
		
	}
	
}

//层序遍历二叉树,又名, 把二叉树打印成多行
/*《解法来自剑指offer》
* 这里使用队列来实现二叉树的层序遍历,
* 先将根节点放入队列,然后每次都从队列中取出一个节点来打印该数值。
* 如果这个节点有子节点,则把它的子节点放到队列尾,
* 直到队列为空
* */
public void layerPrint(){
	
	LinkedList <Node> queue =new LinkedList<Node>();
	if(root==null){System.out.println("Null tree"); return;}
	
	Node current=root;
	queue.offer(current); // offer就是队列的add,不过对于队列长度未知的情况更适用
	int count;//用于记录 本层 已经打印的个数
	int last;//记录 本层 一共有多少个
	
	while(!queue.isEmpty()){
		count=0;
		last=queue.size();
		ArrayList<Integer>list=new ArrayList<Integer>();
		while(count<last){
			current=queue.pop();// 根节点出队
			list.add(current.data);//准备打印根节点
			count++;
			if(current.left!=null)
				queue.offer(current.left);
			if(current.right!=null)
				queue.offer(current.right);
		}
		for(int i=0;i<list.size();i++)
			System.out.print(list.get(i)+" ");
		System.out.println(" ");
	}
	
	
}


	class Node{
		public int data;
		public Node left;
		public Node right;
		public Node(int data){
			this.data=data;
			this.left=null;
			this.right=null;
		}
		public void insert(int data){
			if(data<this.data){
				if(this.left==null){
					this.left=new Node(data);
					
				}
				else {
					this.left.insert(data);
				}
				
			}else if(data>=this.data){
				if(this.right==null){
					this.right=new Node(data);
				}else{
					this.right.insert(data);
					
				}			}
			
		}
		
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值