二叉树 先序遍历 中序遍历 后续遍历 java实现

来源:互联网 发布:单片机呼吸灯程序 编辑:程序博客网 时间:2024/06/06 03:01

二叉树是一种非常重要的数据结构,也是平时面试的时候面试官喜欢出的问题之一。关于二叉树的概念,就不做过多解释,估计各种课本网络上各种资料都充斥着关于二叉树的原理介绍。我们是实战派,原理不在啰嗦,重点看代码,看看怎样实现一棵二叉树,并分别用实现先序遍历,中序遍历以及后续遍历。

package leilei.bit.edu.tree;import java.util.Stack;public class BinTree<T> {    private Node<T> root;    public BinTree(Node<T> root) {        this.root = root;    }    public Node<T> getRoot() {        return root;    }    public void setRoot(Node<T> root) {        this.root = root;    }    //内部节点类    private static class Node<T>    {        public T data;        public Node<T> left;        public Node<T> right;        public Node(T data,Node<T> left,Node<T> right) {            this.data = data;            this.left = left;            this.right = right;        }    }    /*     * 递归先序遍历     */    public void preorder(Node<T> root) {        if (root != null) {            System.out.print(root.data + " ");            preorder(root.left);            preorder(root.right);        }    }    /*     * 递归中序遍历     */    public void midorder(Node<T> root) {        if (root != null) {            midorder(root.left);            System.out.print(root.data + " ");            midorder(root.right);        }    }    /*     * 递归后序遍历     */    public void postorder(Node<T> root) {        if (root != null) {            postorder(root.left);            postorder(root.right);            System.out.print(root.data + " ");        }    }    /*     * 非递归先序     */    public void preorder_no_recursive(Node<T> root) {        if (root == null)            return;        Stack<Node<T>> stack = new Stack<Node<T>>();        stack.push(root);        while(!stack.isEmpty()) {            while(stack.peek() != null) {                System.out.print(stack.peek().data + " ");                stack.push(stack.peek().left);            }            Node<T> p = stack.pop();            if(!stack.isEmpty()) {                p = stack.pop();                stack.push(p.right);            }        }    }    public static void main(String[] args) {        Node<String> G = new Node<String>("G",null,null);        Node<String> F = new Node<String>("F",null,null);        Node<String> E = new Node<String>("E",null,null);        Node<String> D = new Node<String>("D",F,null);        Node<String> B = new Node<String>("B",D,E);        Node<String> C = new Node<String>("C",null,G);        Node<String> root = new Node<String>("A",B,C);        BinTree<String> tree = new BinTree<String>(root);        System.out.println("The preorder is:");        tree.preorder(tree.root);        System.out.println("\nThe midorder is:");        tree.midorder(tree.root);        System.out.println("\nThe postorder is:");        tree.postorder(tree.root);        System.out.println("\nThe no_recursive is:");        tree.preorder_no_recursive(tree.root);    }}

代码运行结果

The preorder is:A B D F E C G The midorder is:F D B E A C G The postorder is:F D E B G C A The no_recursive is:A B D F E C G 

另外发现一个写二叉树不错的链接,有空可以详细看看里面的代码:
https://github.com/yuzhangcmu/LeetCode/blob/master/tree/TreeDemo.java

0 0
原创粉丝点击