欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java二叉树的四种遍历(递归与非递归)

程序员文章站 2022-04-24 13:02:20
目录一、先序遍历与后序遍历先序遍历根节点,再遍历左子树,再遍历右子树。后序遍历先遍历左子树,再遍历右子树,再遍历根节点。先序遍历递归实现:public static void preorderbyre...

一、先序遍历与后序遍历

先序遍历根节点,再遍历左子树,再遍历右子树。

后序遍历先遍历左子树,再遍历右子树,再遍历根节点。

先序遍历递归实现:

public static void preorderbyrecursion(treenode root) {
    // 打印节点值
    system.out.println(root.value);
    preorder(root.left);
    preorder(root.right);
}

先序遍历的非递归实现:

非递归实现需要借助栈这样一个数据结构,实际上递归实现也是依靠栈,只不过是隐式的。

  1. 先将根节点压入栈中。
  2. 弹出栈中的节点,将弹出节点的右子节点压入栈中,再将弹出节点的左子树压入栈中。
  3. 重复步骤2,直到栈为空。
public static void preorder(treenode root) {
    if (root == null) {
        return;
    }
    stack<treenode> stack = new stack<>();
    stack.push(root);
    while (!stack.empty()) {
        treenode node = stack.pop();
        // 打印节点值
        system.out.print(node.value + " ");
        if (node.right != null) {
            stack.push(node.right);
        }
        if (node.left != null) {
            stack.push(node.left);
        }
    }
}

后序遍历递归实现:先序遍历反过来,就不赘述了。

public static void postorderbyrecursion(treenode root) {
    postorderbyrecursion(root.left);
    postorderbyrecursion(root.right);
    system.out.println(root.value);
}

后序遍历非递归实现:后序遍历就是先序遍历反过来,所以需要两个栈,多出来的栈用来反向输出。

public static void postorder(treenode root) {
    if (root == null) {
        return;
    }
    stack<treenode> s1 = new stack<>();
    stack<treenode> s2 = new stack<>();
    s1.push(root);
    while (!s1.empty()) {
        treenode node = s1.pop();
        s2.push(node);
        if (node.left != null) {
            s1.push(node.left);
        }
        if (node.right != null) {
            s1.push(node.right);
        }
    }
    while (!s2.empty()) {
        system.out.println(s2.pop().value);
    }
}

二、中序遍历

中序遍历先遍历左子树,再遍历根节点,再遍历右子树。

递归遍历:

public static void inorderbyrecursion(treenode root) {
    if (root == null) {
        return;
    }
    inorderbyrecursion(root.left);
    // 打印节点值
    system.out.println(root.value);
    inorderbyrecursion(root.right);
}

非递归遍历:

  1. 将二叉树的左侧“边”从上到下依次压入栈中。
  2. 从栈中弹出节点
  3. 对以弹出节点的右子节点为根节点的子树,重复步骤1。
  4. 重复2、3步骤,直到栈为空。
public static void inorder(treenode root) {
    if (root == null) {
        return;
    }
    stack<treenode> stack = new stack<>();
    treenode cur = root;

    while (cur != null) {
        stack.push(cur);
        cur = cur.left;
    }

    while (!stack.empty()) {
        treenode node = stack.pop();
        system.out.println(node.value);
        cur = node.right;
        while (cur != null) {
            stack.push(cur);
            cur = cur.left;
        }
    }
}

三、层序遍历

层序遍历顾名思义就是一层一层,从左到右的遍历二叉树。需要用到队列这一数据结构。

  1. 将根节点推入队列。
  2. 从队列中取出一个节点。
  3. 先将取出节点的左子节点推入队列,再将取出节点的右子节点推入队列。
  4. 重复2、3步骤直到队列中无节点可取。
public static void floororder(treenode root) {
    if (root == null) {
        return;
    }
    queue<treenode> queue = new linkedlist<>();
    queue.add(root);
    while (!queue.isempty()) {
        treenode node = queue.poll();
        system.out.println(node.value);
        if (node.left != null) {
            queue.add(node.left);
        }
        if (node.right != null) {
            queue.add(node.right);
        }
    }
}

到此这篇关于java二叉树的四种遍历(递归与非递归)的文章就介绍到这了,更多相关java二叉树的四种遍历内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!