您的位置:首页 > 编程语言 > Java开发

二叉树的基本结构以及java实现

2015-04-09 10:09 495 查看
一、二叉树的结构

二叉树是每个节点最多有两个子树的树结构

二、几种类型的二叉树

(1)完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第h层有叶子结点,并且叶子结点都是从左到右依次排布,这就是完全二叉树
(2)满二叉树——除了叶结点外每一个结点都有左右子叶且叶子结点都处在最底层的二叉树。
(3)平衡二叉树——平衡二叉树又被称为***L树(区别于***L算法),它是一棵二叉排序树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

三、二叉树的实现代码

package javaTest;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class BinaryTree {

	private int[] array={1,2,3,4,5,6,7,8,9};
	private static List<Node> nodeList=null;
	
	
	
	private static class Node{
		
		public int data;
		public Node leftChild;
		public Node rightChild;
		Node(int newDate){
			data=newDate;
			leftChild=null;
			rightChild=null;
		}
	}
	
	
	public void createBinTree(){
		nodeList=new LinkedList<Node>();
		//将数组转换成Node组成的一个list
		for(int nodeIndex = 0;nodeIndex<array.length;nodeIndex++){
			nodeList.add(new Node(array[nodeIndex]));
//			System.out.println(array[nodeIndex]);
//			System.out.println(nodeList.get(nodeIndex).data);
		}
		
		for(int i=0;i<array.length/2-1;i++){
			nodeList.get(i).leftChild=nodeList.get(2*i+1);
			nodeList.get(i).rightChild=nodeList.get(2*i+2);
			
		}
		
		int lastParentIndex=array.length/2-1;
		nodeList.get(lastParentIndex).leftChild=nodeList.get(2*lastParentIndex+1);
		if(array.length%2==1){
			nodeList.get(lastParentIndex).rightChild=nodeList.get(2*lastParentIndex+2);
		}
	}
	 /** 
     * 先序遍历 
     *  
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
     *  
     * @param node 
     *            遍历的节点 
     */  
    public static void preOrderTraverse(Node node) {  
        if (node == null)  
            return;  
        System.out.print(node.data + " ");  
        preOrderTraverse(node.leftChild);  
        preOrderTraverse(node.rightChild);  
    }  
    public static void nrPreOrderTraverse(Node node){
    	Stack<Node> stack=new Stack<Node>();
    
    	while(node!=null||!stack.isEmpty()){
    		while(node!=null){
    			System.out.print(node.data+" ");
    			stack.push(node);
    			node=node.leftChild;
    		}
    		node=stack.pop();
    		
    		node=node.rightChild;
    	}
    }
    /** 
     * 中序遍历 
     *  
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
     *  
     * @param node 
     *            遍历的节点 
     */  
    public static void inOrderTraverse(Node node) {  
        if (node == null)  
            return;  
        inOrderTraverse(node.leftChild);  
        System.out.print(node.data + " ");  
        inOrderTraverse(node.rightChild);  
    }  
    public static void nrInOrderTraverse(Node node){
    	Stack<Node> stack=new Stack<Node>();
    
    	while(node!=null||!stack.isEmpty()){
    		while(node!=null){
    			stack.push(node);
    			node=node.leftChild;
    		}
    		node=stack.pop();
    		System.out.print(node.data+" ");
    		node=node.rightChild;
    	}
    }
  
    /** 
     * 后序遍历 
     *  
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
     *  
     * @param node 
     *            遍历的节点 
     */  
    public static void postOrderTraverse(Node node) {  
        if (node == null)  
            return;  
        postOrderTraverse(node.leftChild);  
        postOrderTraverse(node.rightChild);  
        System.out.print(node.data + " ");  
    }  
    public static void nrPostOrderTraverse(Node node){
    	Stack<Node> stack=new Stack<Node>();
    	Node preNode = null;//表示最近一次访问的节点  
    
    	while(node!=null||!stack.isEmpty()){
    		while(node!=null){
    			stack.push(node);
    			node=node.leftChild;
    		}
    		node=stack.peek();
    		
    		if(node.rightChild==null||node.rightChild==preNode){
    			System.out.print(node.data+" ");
    			node=stack.pop();
    			preNode=node;
    			node=null;
    		}else{
    			node=node.rightChild;
    		}
    	}
    }
    
    public static void main(String[] args) {
		BinaryTree tree=new BinaryTree();
		tree.createBinTree();
		Node root=nodeList.get(0);
//		System.out.println(root.data);
		 System.out.println("先序遍历:");  
	        preOrderTraverse(root);  
	        System.out.println();  
	  
	        System.out.println("中序遍历:");  
	        inOrderTraverse(root);  
	        System.out.println();  
	  
	        System.out.println("后序遍历:");  
	        postOrderTraverse(root); 
	        System.out.println();
	        System.out.println("非递归中序遍历:");  
	        nrInOrderTraverse(root);
	        System.out.println();
	        System.out.println("非递归先序遍历:");  
	        nrPreOrderTraverse(root);  
	        System.out.println();  
	  
	        System.out.println("非递归后序遍历:");  
	        nrPostOrderTraverse(root); 
	        System.out.println();
	        
		
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: