zoukankan      html  css  js  c++  java
  • 创建二叉排序树,二叉树的六种遍历

    package create;
    
    import java.util.Stack;
    /**
     * 创建二叉排序树,二叉树的六种遍历
     * @author root
     *
     */
    public class BiTree {
    
    	static class Node{
    		int value;
    		Node left = null;
    		Node right = null;
    		public Node(int value){
    			this.value = value;
    		}
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int []data = {6,5,3,4,2,8,7,9,1};
    		BiTree bt = new BiTree();
    		
    		Node root = bt.creatBiTree(data);
    		System.out.println(bt.getDepth(root));
    		
    	}
    	//创建二叉排序树
    	public Node creatBiTree(int []data){
    		Node root = new Node(data[0]);
    		for(int i=1; i<data.length; i++){
    			insert(root, data[i]);
    		}
    		return root;
    	}
    	//插入节点
    	public void insert(Node root, int value){
    		
    		if(value<root.value){
    			if(root.left==null){
    				root.left = new Node(value);
    			}else{
    				insert(root.left, value);
    			}
    		}else{
    			if(root.right==null){
    				root.right = new Node(value);
    			}else{
    				insert(root.right, value);
    			}
    		}
    	}
    	//得到树的深度
    	public int getDepth(Node root){
    		int depth = 0;
    		int left = 0;
    		int right = 0;
    		if(root!=null){
    			depth++;
    			left = getDepth(root.left);
    			right = getDepth(root.right);
    		}
    		return depth+(left>right?left:right);
    	}
    	//递归先序遍历
    	public void preOrderTraverse(Node root){
    		if(root!=null){
    			System.out.println(root.value);
    			preOrderTraverse(root.left);
    			preOrderTraverse(root.right);
    		}
    	}
    	//循环先序遍历
    	public void cyclePreOrderTraverse(Node root){
    		Stack<Node> stack = new Stack<Node>();
    		Node node = root;
    		while(node!=null||!stack.isEmpty()){
    			while(node!=null){
    				stack.push(node);
    				System.out.println(node.value);
    				node=node.left;
    			}
    			if(!stack.isEmpty()){
    				node = stack.pop();
    				node = node.right;
    			}
    		}
    		
    	}
    	//递归中序遍历
    	public void inOrderTraverse(Node root){
    		if(root!=null){
    			inOrderTraverse(root.left);
    			System.out.println(root.value);
    			inOrderTraverse(root.right);
    		}
    	}
    	//循环中序遍历
    	public void cycleInOrderTraverse(Node root){
    		Stack<Node> stack = new Stack<Node>();
    		Node node = root;
    		while(node!=null||!stack.isEmpty()){
    			while(node!=null){
    				stack.push(node);
    				node=node.left;
    			}
    			if(!stack.isEmpty()){
    				node = stack.pop();
    				System.out.println(node.value);
    				node = node.right;
    			}
    		}
    	}
    	//递归后序遍历
    	public void postOrderTraverse(Node root){
    		if(root!=null){
    			postOrderTraverse(root.left);
    			postOrderTraverse(root.right);
    			System.out.println(root.value);
    		}
    	}
    	//循环后序遍历
    	public void cyclePostOrderTraverse(Node root){
    		Stack<Node> stack = new Stack<Node>();
    		Node node = root;
    		stack.push(root);
    		Node pre = null;
    		while(!stack.isEmpty()){
    			node = stack.peek();
    			if(node.left==null && node.right==null || pre!=null && (pre==node.left || pre==node.right)){
    				System.out.println(node.value);
    				stack.pop();
    				pre = node;
    			}else{
    				if(node.right!=null){
    					stack.push(node.right);
    				}
    				if(node.left!=null){
    					stack.push(node.left);
    				}
    			}
    		}
    	}
    }
    

  • 相关阅读:
    zepto.js常用操作
    使用require.js
    iscroll.js文档
    EasyUI Resizable 可调整尺寸
    EasyUI Droppable 可放置
    EasyUI Draggable 可拖动
    EasyUI Parser 解析器
    EasyUI Easyloader 加载器
    Jquery EasyUI插件
    从MySQL随机选取数据
  • 原文地址:https://www.cnblogs.com/yan456jie/p/5369411.html
Copyright © 2011-2022 走看看