二叉树的遍历Java实现

使用Stack 遍历树而不递归。下面是使用堆栈遍历二叉树的算法。

1)创建一个空栈S.
2)以root身份初始化当前节点
3)将当前节点推送到S,并设置current = current-> left,直到current为NULL
4)如果current为NULL,堆栈不为空 
     a)从堆栈中弹出顶部元素。
     b)打印弹出的元素,设置current = popped_item-> right 
     c)转到步骤35)如果current为NULL,堆栈为空,遍历结束。

让我们考虑下面树的遍历例如

           1
          / \
        2    3
       / \
     4     5

步骤1 创建一个空堆栈:S = NULL

步骤2 初始化当前节点作为根节点:current  - > 1

步骤3 将当前节点入栈,并设置current = current-> left,直到current为NULL
     current - > 1
     Push 1:堆栈S  - > 1
     current - > 2
     push 2Stack S  - > 21
     current - > 4
     push 4Stack S  - > 421
     current = NULL

步骤44从S中弹出
     a)Pop 4Stack S  - > 21
     b)打印“4”
     c)current = NULL / *右边的4 * /并转到步骤3
因为current是NULL,第3步不做任何事情。 

步骤4 再次弹出。
     a)Pop 2Stack S  - > 1
     b)打印“2”
     c)current - > 5 / *右侧的2 * /并转到步骤3

步骤3 Push 5栈并使当前为NULL
     堆栈S  - > 51
     current = NULL

步骤4从S中弹出
     a)Pop 5Stack S  - > 1
     b)打印“5”
     c)current = NULL / *右边5 * /并转到步骤3
因为current是NULL,第3步不做任何事情

步骤4再次弹出。
     a)Pop 1:栈S  - > NULL
     b)打印“1”
     c)current - > 3 / *右边5 * /  

步骤3 Push 3到堆栈,并使当前为NULL
     堆栈S  - > 3
     current = NULL

步骤4从S中弹出
     a)Pop 3:栈S  - > NULL
     b)打印“3”
     c)current = NULL / *右边的3 * /  

遍历现在完成,因为堆栈S为空且当前为NULL

代码

// non-recursive java program for inorder traversal

/* importing the necessary class */
import java.util.Stack;

/* Class containing left and right child of current node and key value*/
class Node {

    int data;
    Node left, right;

    public Node(int item) {
        data = item;
        left = right = null;
    }
}

/* Class to print the inorder traversal */
class BinaryTree {

    Node root;

    void inorder() {
        if (root == null) {
            return;
        }

        //keep the nodes in the path that are waiting to be visited
        Stack<Node> stack = new Stack<Node>();
        Node node = root;

        //first node to be visited will be the left one
        while (node != null) {
            stack.push(node);
            node = node.left;
        }

        // traverse the tree
        while (stack.size() > 0) {

            // visit the top node
            node = stack.pop();
            System.out.print(node.data + " ");
            if (node.right != null) {
                node = node.right;

                // the next node to be visited is the leftmost
                while (node != null) {
                    stack.push(node);
                    node = node.left;
                }
            }
        }
    }

    public static void main(String args[]) {

        /* creating a binary tree and entering the nodes */
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
        tree.inorder();
    }
}
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/u013309870/article/details/62226290
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞