二叉树的先根、中根、后根的递归与非递归调用方法,全!精简版代码!

本文详细介绍了二叉树的遍历算法,包括先序、中序和后序遍历的递归与非递归实现。特别地,后序非递归遍历采用双栈方法,提供了一个清晰的实现思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整理了一下这几种遍历的精简版代码,思路简介明确,其中后序非递归方法用到了双栈,代码如下:



import LinkedStack;
import BinaryNode;
import org.junit.Test;

public class PosIterator<T> {
    private BinaryNode root;

    //递归的算法   ---先根遍历
    public void preRead(BinaryNode<Integer> p) {
        if (p != null) {
            System.out.println(p.data);
            preRead(p.left);
            preRead(p.right);
        }
    }

    //递归的算法   ---中根遍历
    public void inRead(BinaryNode<Integer> p) {
        if (p != null) {
            inRead(p.left);
            System.out.println(p.data);
            inRead(p.right);
        }
    }

    //递归的算法   ---后根遍历
    public void posRead(BinaryNode<Integer> p) {
        if (p != null) {
            posRead(p.left);
            posRead(p.right);
            System.out.println(p.data);
        }
    }

    //非递归算法   ---先根遍历
    public void preOrderTraverse() {
        System.out.println("根遍历(非递归):   ");
        LinkedStack<BinaryNode<T>> stack = new LinkedStack<BinaryNode<T>>();
        BinaryNode<T> p = this.root;
        while (p != null || !stack.isEmpty()) {
            if (p != null) {
                stack.push(p);
                p = p.left;
            }
            else {
                System.out.print("^");
                p = stack.pop();
                System.out.print(p.data);//访问数据。
                p = p.right;
            }
        }
        System.out.println();
    }

    //非递归算法   ---中根遍历
    public void inOrderTraverse() {
        System.out.println("中根遍历(非递归):   ");
        LinkedStack<BinaryNode<T>> stack = new LinkedStack<BinaryNode<T>>();
        BinaryNode<T> p = this.root;
        while (p != null || !stack.isEmpty()) {
            if (p != null) {
                System.out.print(p.data);//访问数据。
                stack.push(p);
                p = p.left;
            }
            else {
                System.out.print("^");
                p = stack.pop();
                p = p.right;
            }
        }
        System.out.println();
    }

    //非递归算法   ---后根遍历  --双栈
    public void posOrderTraverse(BinaryNode<T> root) {
        System.out.println("后根遍历(非递归):   ");
        LinkedStack<BinaryNode<T>> stack1 = new LinkedStack<BinaryNode<T>>(); //创建立空栈1
        LinkedStack<BinaryNode<T>> stack2 = new LinkedStack<BinaryNode<T>>(); //创建立空栈1
        stack1.push(root);
         while (!stack1.isEmpty()) {
             BinaryNode<T> p = stack1.pop();
             stack2.push(p);
             if (p.right != null) {
                 stack1.push(p.right);
             }
             if (p.left != null) {
                 stack1.push(p.left);
             }
         }

         while (!stack2.isEmpty()) {
             System.out.print(stack2.pop().data);
         }
         System.out.println();
    }

    @Test
    public void testIt() {
        this.root = new BinaryNode<String>("A");
        this.root.left = new BinaryNode("B");
        this.root.left.left = new BinaryNode("D");
        this.root.left.left.right = new BinaryNode("G");
        this.root.right = new BinaryNode("C");
        this.root.right.left = new BinaryNode("E");
        this.root.right.right = new BinaryNode("F");
        this.root.right.right.left = new BinaryNode("H");

        this.posOrderTraverse(this.root);
        this.inOrderTraverse();
        this.preOrderTraverse();
    }

}

附赠引用到的两个类的代码:


public class BinaryNode<T> {
    public T data;
    public BinaryNode<T> left, right;

    public BinaryNode(T data, BinaryNode<T> left, BinaryNode<T> right) {
        this.data = data;
        this.left = left;
        this.right = right;
    }

    public BinaryNode(T data) {
        this(data,null,null);
    }

    public String toString() {
        return this.data.toString();
    }

    public boolean isLeaf() {
        return this.left == null && this.right == null;
    }
}
package dataDistructure;

import java.util.LinkedList;
import java.util.List;

public class LinkedStack<T> {
    private List<T> list;

    public LinkedStack() {
        this.list = new LinkedList<T>();
    }

    public boolean isEmpty() {
        return this.list.isEmpty();
    }

    public void push(T t) {
        this.list.add(t);
    }

    public T peek() {
        return this.list.get(this.list.size()-1);
    }

    public T pop() {
        T t = this.list.get(this.list.size()-1);
        this.list.remove(this.list.size()-1);
        return t;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

请保持优秀。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值