码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的递归和非递归实现

时间:2016-07-01 22:51:16      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.Stack;

class Node {    
    private char key;    
    private Node left, right;    
    
    public Node(char key) {    
        this(key, null, null);    
    }    
    
    public Node(char key, Node left, Node right) {    
        this.key = key;    
        this.left = left;    
        this.right = right;    
    }    
    
    public char getKey() {    
        return key;    
    }    
    
    public void setKey(char key) {    
        this.key = key;    
    }    
    
    public Node getLeft() {    
        return left;    
    }    
    
    public void setLeft(Node left) {    
        this.left = left;    
    }    
    
    public Node getRight() {    
        return right;    
    }    
    
    public void setRight(Node right) {    
        this.right = right;    
    }    
}    

public class BinaryTree {
    protected Node root;
    
    public BinaryTree(Node root) {
        this.root = root;
    }
    
    public Node getRoot() {
        return root;
    }
    
    /** 构造树 */    
    public static Node init() {    
        Node a = new Node(‘A‘);    
        Node b = new Node(‘B‘, null, a);    
        Node c = new Node(‘C‘);    
        Node d = new Node(‘D‘, b, c);    
        Node e = new Node(‘E‘);    
        Node f = new Node(‘F‘, e, null);    
        Node g = new Node(‘G‘, null, f);    
        Node h = new Node(‘H‘, d, g);    
        return h;// root    
    }   
    
    /** 访问节点 */    
    public static void visit(Node p) {    
        System.out.print(p.getKey() + " ");    
    }  
    
    /** 递归实现前序遍历 */    
    protected static void preorder(Node p) {    
        if (p != null) {    
            visit(p);    
            preorder(p.getLeft());    
            preorder(p.getRight());    
        }    
    }    
    
    /** 递归实现中序遍历 */    
    protected static void inorder(Node p) {    
        if (p != null) {    
            inorder(p.getLeft());    
            visit(p);    
            inorder(p.getRight());    
        }    
    }    
    
    /** 递归实现后序遍历 */    
    protected static void postorder(Node p) {    
        if (p != null) {    
            postorder(p.getLeft());    
            postorder(p.getRight());    
            visit(p);    
        }    
    }    
    
    //前序非递归实现
    protected static void iterativePreorder(Node p) {    
        Stack<Node> stack = new Stack<Node>();    
        Node node = p;    
        while (node != null || stack.size() > 0) {    
            while (node != null) {//压入所有的左节点,压入前访问它。左节点压入完后pop访问右节点。像这样算法时思考规律性的东西在哪。不管哪个节点都要压所节点判断右节点。    
                visit(node);    
                stack.push(node);    
                node = node.getLeft();    
            }    
            if (stack.size() > 0) {//    
                node = stack.pop();    
                node = node.getRight();    
            }    
        }    
    } 
    
  //中序非递归实现
    protected static void iterativeInorder(Node p) {
        Stack<Node> stack = new Stack<Node>();
        Node node = p;
        while(node != null || stack.size() > 0) {
            while (node != null) {
                stack.push(node);
                node = node.getLeft();
            }
            if (stack.size() > 0) {
                node = stack.pop();
                visit(node);
                node = node.getRight();
            }
        }
     }
    
  //后序非递归实现   
    protected static void iterativePostorder(Node p) {    
        Node q = p;    
        Stack<Node> stack = new Stack<Node>();    
        while (p != null) {    
            // 左子树入栈    
            for (; p.getLeft() != null; p = p.getLeft())    
                stack.push(p);    
            // 当前节点无右子或右子已经输出    
            while (p != null && (p.getRight() == null || p.getRight() == q)) {    
                visit(p);    
                q = p;// 记录上一个已输出节点    
                if (stack.empty())    
                    return;    
                p = stack.pop();    
            }    
            // 处理右子    
            stack.push(p);    
            p = p.getRight();    
        }    
    }    
    
    public static void main(String[] args) {    
        BinaryTree tree = new BinaryTree(init());   
        System.out.print(" 递归前序遍历 \n");    
        preorder(tree.getRoot());  
        System.out.println();
        System.out.print(" 递归中序遍历 \n");    
        inorder(tree.getRoot());  
        System.out.println();
        System.out.print(" 递归后序遍历 \n");    
        postorder(tree.getRoot());  
        System.out.println();
        
        System.out.print(" 非递归前序遍历 \n");   
        iterativePreorder(tree.getRoot());
        System.out.println();
        System.out.print(" 非递归中序遍历 \n");   
        iterativeInorder(tree.getRoot());
        System.out.println();
        System.out.print(" 非递归后序遍历 \n");   
        iterativePostorder(tree.getRoot());
    }
}

 

二叉树的递归和非递归实现

标签:

原文地址:http://www.cnblogs.com/myseries/p/5634295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!