java 实现二叉树前中后序遍历非递归算法

面试被问过,记录....

package ***;

import java.util.Stack;

/**
 * @author weichen CHEN created on 2018/1/8
 * @version 2018/1/8  weichen CHEN
 * 二叉树前中后序非递归算法
 */
public class BinarySearchTree {

    Stack s = new Stack();

    public void readFrontTree(){

        TreeNode p = new TreeNode("A");

        while(!s.isEmpty() || p != null){

            while(p!=null)
            {
                System.out.println(p.getValue());
                s.push(p);
                p=p.getLeftChild();
            }
            if(!s.empty())
            {
                p=(TreeNode) s.lastElement();
                s.pop();
                p=p.getRightChild();
            }
        }

    }

    public void readMiddleTree(){

        TreeNode p = new TreeNode("A");

        while(!s.isEmpty() || p != null){

            while(p!=null)
            {
                s.push(p);
                p=p.getLeftChild();
            }

            if(!s.empty())
            {
                p=(TreeNode) s.lastElement();
                System.out.println(p.getValue());
                s.pop();
                p=p.getRightChild();
            }
        }

    }

    public void readBackTree(){

        TreeNode p = new TreeNode("A");

        TreeNode curNode = null;
        TreeNode preNode = null;

        s.push(p);

        while(!s.isEmpty()){

            curNode = (TreeNode) s.lastElement();

            if((curNode.getLeftChild() == null || curNode.getRightChild() == null)
                    ||(preNode!=null&&(preNode==curNode.getLeftChild()||preNode==curNode.getRightChild()))){

                System.out.println(curNode.getValue());
                s.pop();
                preNode = curNode;
            }else{
                if(curNode.getRightChild()!=null){
                    s.push(curNode.getRightChild());
                }
                if(curNode.getLeftChild()!=null){
                    s.push(curNode.getLeftChild());
                }
            }

        }

    }

    public static void main(String[] args) {

        BinarySearchTree tt = new BinarySearchTree();

        System.out.println("       A");
        System.out.println("  A1       A2");
        System.out.println("A11 A12 A21 A22");
        System.out.println("============前序非递归遍历==================");
        tt.readFrontTree();
        System.out.println("==============end================");
        System.out.println("============中序非递归遍历==================");
        tt.readMiddleTree();
        System.out.println("==============end================");
        System.out.println("============后序非递归遍历==================");
        tt.readBackTree();
        System.out.println("==============end================");

    }

}
树结构实体(三层树)

package ***;

/**
 * @author weichen CHEN created on 2018/1/8
 * @version 2018/1/8  weichen CHEN
 */
public class TreeNode {

    String value;

    TreeNode leftChild;

    TreeNode rightChild;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public TreeNode getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(TreeNode leftChild) {
        this.leftChild = leftChild;
    }

    public TreeNode getRightChild() {
        return rightChild;
    }

    public void setRightChild(TreeNode rightChild) {
        this.rightChild = rightChild;
    }

    TreeNode(){
        new TreeNode();
    }

    TreeNode(String v){

        this.value = v;

        if(this.value.length() == 3){
            return;
        }

        this.leftChild = new TreeNode(v+"1");

        this.rightChild = new TreeNode(v+"2");



    }
}

欢迎讨论...


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值