Java数据结构--- 二叉树(二叉排序树,层次遍历二叉树节点)



Java 二叉树数据结构


今天就写一下二叉树的常用操作,明天准备写一些图中的常用函数!以后每天都要更新博客,从此要养成,积累的好习惯。俗话说,滴水穿石,对于以前自己没有写博客的习惯,感觉很可惜,做过很多工作,但就是因为没有及时记下来,所以干什么忘什么,基本很多工作干完了一段时间也就忘了,感觉很可惜。既然决定了写博客,那以后每天多少都要写点,也作为自己一天工作的总结。


本文包括二叉树的常用函数,包括二叉排序树的建立,层次遍历二叉树节点,找最大,最小节点,先序,中序,后序遍历等常用操作!


<pre name="code" class="java">package com.robin.binaryTree;

public class Node {

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

}


 
 
</pre><pre code_snippet_id="627062" snippet_file_name="blog_20150324_3_1929932" name="code" class="java" style="text-align: left;"><pre name="code" class="java">package com.robin.binaryTree;

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

public class BinaryTree {

	public Node root;
	
	//构造函数
	public BinaryTree(){
		this.root = null;
	}
	
	
	public void buildTree(int[] data){
		for(int i = 0; i < data.length; i++){
			insertNode(data[i]);
		}
	}
	
	public void insertNode(int data){
		
		Node newNode = new Node(data);
		
		if(this.root == null){
			root = new Node(data);
		}else{
			Node current = root;
			Node parent;
			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 void preOrder(){
		this.preOrder(this.root);
	}

	public void preOrder(Node localRoot){
		if(localRoot != null){
			System.out.print(localRoot.data + " ");
			this.preOrder(localRoot.left);
			this.preOrder(localRoot.right);
			
		}
	}
	
	public void inOrder(){
		this.inOrder(this.root);
	}
	
	public void inOrder(Node localRoot){
		if(localRoot != null){
			this.inOrder(localRoot.left);
			System.out.print(localRoot.data + " ");
			this.inOrder(localRoot.right);
			
		}
	}
	
	
	
	public void postOrder(){
		this.postOrder(this.root);
	}
	
	public void postOrder(Node localRoot){
		if(localRoot != null){
			this.postOrder(localRoot.left);
			this.postOrder(localRoot.right);
			System.out.print(localRoot.data + " ");
		}
	}
	
	public void layoutOrder(){
		this.layoutOrder(this.root);
	}
	
	public void layoutOrder(Node localRoot){
		
		if(localRoot == null){
			return;
		}
		
		Queue<Node> queue = new LinkedList<Node>();
		
		queue.add(localRoot);
		
		while(!queue.isEmpty()){
			Node node = queue.poll();
			System.out.print(node.data +" ");
			if(node.left != null)
				queue.add(node.left);
			if(node.right != null)
				queue.add(node.right);
		}
		
		System.out.println();
	}
	
	public void findMin(){
		this.findMin(this.root);
	}
	
	public void findMin(Node node){
		
		Node current = node;
		
		if(node == null){
			return;
		}
		
		while(current.left != null){
			current = current.left;
		}
		
		System.out.println("the min data is:"+current.data);
	}
	
	public void findMax(){
		this.findMax(this.root);
	}
	
	public void findMax(Node node){
		
		Node current = node;
		
		if(node == null){
			return;
		}
		
		while(current.right != null){
			current = current.right;
		}
		
		System.out.println("the max data is:"+current.data);
	}
	
	public static void main(String[] args) {

		BinaryTree bt = new BinaryTree();
		
		int[] data = {2,8,7,4,9,3,1,6,7,5};
		
		bt.buildTree(data);
		
		bt.preOrder();
		
		System.out.println();
		
		bt.inOrder();
		
		System.out.println();
		
		bt.postOrder();
		
		System.out.println();
		
		bt.layoutOrder();
		
		bt.findMin();
		
		bt.findMax();
	}

}


 
 
 
 

你可能感兴趣的:(java,数据结构,二叉树,遍历,面试题)