树的四种遍历:前序遍历,中序遍历,后序遍历,层级遍历 使用栈实现(Java语言实现)

首先我们定义一下树的数据结构

@Data
public class BiTree {
    private Integer data;//数据
    private BiTree lchild;//结点左孩子
    private BiTree rchild;//结点右孩子
    public BiTree() {
    }
    public BiTree(Integer data) {
        this.data = data;
    }
}

有了树的结构那么我们可以初始化树了,我们初始化得树的结构如下图:

 

//初始化树的的方法
private static BiTree init() {
        BiTree biTree = new BiTree(1);
        BiTree tow = new BiTree(2);
        BiTree three = new BiTree(3);
        BiTree four = new BiTree(4);
        BiTree five = new BiTree(5);
        BiTree six = new BiTree(6);
        BiTree seven = new BiTree(7);
        biTree.setLchild(tow);
        biTree.setRchild(three);
        tow.setLchild(four);
        tow.setRchild(five);
        three.setLchild(six);
        three.setRchild(seven);
        return biTree;
    }

 

栈实现树的前序遍历:

 

public class PreOrderWithStack  {
    private static int top = -1;

    private static BiTree[] stack = new BiTree[20];

    private BiTree getTop() {
        return stack[top];
    }

    private void push(BiTree tree) {
        stack[++top] = tree;
    }

    private void pop() {
        if (top > -1)
            top--;
    }
    private void print(BiTree node) {
        System.out.print(node.getData() + " ");
    }

    public void preOrder(BiTree biTree) {
        push(biTree);//将根节点入栈
        while (top != -1) {//栈不为空时
            BiTree p = getTop();//取出栈顶元素
            pop();//指针下移一位
            while (p != null) {
                print(p);//答应根节点
                if (p.getRchild() != null) {//将右孩子压栈
                    push(p.getRchild());
                }
                p = p.getLchild();//处理左孩子
            }
        }

    }
}

栈实现树的中序遍历:

public class InOrderWithStack {
    public static int top = -1;
    public static BiTree[] stack = new BiTree[20];
    private BiTree getTop() {
        return stack[top];
    }
    private void push(BiTree tree) {
        stack[++top] = tree;
    }
    private void pop() {
        if (top > -1)
            top--;
    }
    private void print(BiTree node) {
        System.out.print(node.getData() + " ");
    }

    /**
     * 中序遍历用栈实现方法一
     * @param tree
     */
    public void doInOrderMethodOne(BiTree tree) {
        BiTree p = tree;
        while (p != null || top != -1) {
            if (p != null) {
                push(p);
                p = p.getLchild();
            } else {
                p = getTop();
                pop();
                print(p);
                p = p.getRchild();
            }
        }
    }

    /**
     * 中序遍历用栈实现方法二
     * @param tree
     */
    public void doInOrderMethodTwo(BiTree tree) {
        push(tree);
        while (top != -1) {
            BiTree p = getTop();
            while (p != null) {
                push(p.getLchild());
                p = getTop();
            }
            pop();
            if (top != -1) {
                p = getTop();
                pop();
                print(p);
                push(p.getRchild());
            }
        }
    }
}

栈实现树的后序遍历:

public class PostOrderWithStack {
    private static int top = -1;
    private static SNode[] sNodes = new SNode[20];
    private void push(SNode sNode) {
        sNodes[++top] = sNode;
    }
    private static SNode getTop() {
        return sNodes[top];
    }
    private void pop() {
        if (top > -1)
            top--;
    }
    private void print(BiTree node) {
        System.out.print(node.getData() + " ");
    }
    public void inOrder(BiTree tree) {
        BiTree p = tree;
        while (p != null || top != -1) {
            while (p != null) {
                SNode sNode = new SNode(p, 0);
                push(sNode);
                p = p.getLchild();
            }
            SNode top = getTop();
            pop();
            if (top.getTag() == 0) {
                top.setTag(1);
                push(top);
                p = top.getP().getRchild();
            } else {
                print(top.getP());
            }
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class SNode {
        private BiTree p;
        private int tag;//用来标记该节点是否已经处理过
    }
}

层级遍历

public class Hierarchy {
    private static int front = 0;
    private static int rear = 0;
    private static BiTree[] queue = new BiTree[20];
    private static void enQueue(BiTree node) {//入队函数
        queue[rear++] = node;
    }
    private static BiTree deQueue() { //出队函数
        return queue[front++];
    }
    private static void print(BiTree node) {
        System.out.print(node.getData() + " ");
    }
    public static void hierarchy(BiTree tree) {
        enQueue(tree);
        while (front < rear) {
            BiTree biTree = deQueue();
            print(biTree);
            if (biTree.getLchild() != null) {
                enQueue(biTree.getLchild());
            }
            if (biTree.getRchild() != null) {
                enQueue(biTree.getRchild());
            }
        }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

指挥官飞飞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值