• 创建二叉排序树,二叉树的六种遍历


    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);
    				}
    			}
    		}
    	}
    }
    

  • 相关阅读:
    finder的隐藏文件&IOS虚拟机地址
    IOS的UI总结
    ios系统的中arm指令集
    mac下删除svn账号
    PNG图片压缩工具
    让finder显示路径
    Serilog高级玩法之用Serilog记录所选终结点附加属性
    如何利用Serilog的RequestLogging来精简ASP.NET Core的日志输出
    关于C#异步编程你应该了解的几点建议
    C#异步编程入门看这篇就够了
  • 原文地址:https://www.cnblogs.com/yan456jie/p/5369411.html
Copyright © 2020-2023  润新知