java实现二叉树的非递归遍历

package binaryTree;

public class Node {
    int value;
    Node leftChild;
    Node rightChild;

    //构造函数
    Node(int value){
        this.value = value;
    }

    public void display(){
        System.out.print(this.value + "\t");
    }

    public int getValue() {
        return value;
    }

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

    public Node getLeftChild() {
        return leftChild;
    }

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

    public Node getRightChild() {
        return rightChild;
    }

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

}


package binaryTree;

import java.util.Stack;

public class BinaryTree {

    private Node root = null;

    BinaryTree (int value){
        root = new Node(value);
        root.leftChild = null;
        root.rightChild = null;
    }

    //查找数据
    public Node findKey(int value){
        Node current = root;
        while(true){
            if(value == current.value){
                return current;
            }else if(value < current.value){
                current = current.leftChild;
            }else{
                current = current.rightChild;
            }

            if(current ==null){
                return null;
            }
        }
    }

    //插入数据
    public String insert(int value){
        String error = null;
        Node node = new Node(value);
        if(root ==null){
            root = node;
        }else{
            Node current = root;
            Node parent = null;
            while(true){
                if(value < current.value){
                    parent = current;
                    current = current.leftChild;
                    if(current == null){
                        parent.leftChild = node;
                        break;
                    }
                }else if(value > current.value){
                    parent = current;
                    current = current.rightChild;
                    if(current == null){
                        parent.rightChild = node;
                        break;
                    }
                }else{
                    error = "already exists the same value in the tree.";
                }
            }
        }

        return error;
    }

    //中序遍历的递归实现
    public void inOrderTraverse() {
        System.out.println("中序遍历:");
        inOrderTraverse(root);
        System.out.println();
    }

    private void inOrderTraverse(Node node) {
        // TODO Auto-generated method stub
        if(node ==null)
            return ;
        inOrderTraverse(node.leftChild);
        node.display();
        inOrderTraverse(node.rightChild);

    }

    /*
     * 中序遍历的非递归实现:
     * 
     * */
    public void inOrderByStack(){
        System.out.println("中序遍历的非递归实现:");
        Stack<Node> stack = new Stack<>();
        Node current = root;
        while(current != null || !stack.isEmpty()){
            while(current != null){
                stack.push(current);
                current = current.leftChild;
            }
            if(!stack.isEmpty()){
                current = stack.pop();
                current.display();
                current = current.rightChild;
                }
            }
        System.out.println();
        }


    //前序遍历的递归实现
    public void preOrderTraverse(){
        System.out.println("前序遍历:");
        preOrderTraverse(root);
        System.out.println();
    }

    private void preOrderTraverse(Node node) {
        // TODO Auto-generated method stub
        if(node == null)
            return ;
        node.display();
        preOrderTraverse(node.leftChild);
        preOrderTraverse(node.rightChild);
    }

    //前序遍历的非递归实现
    public void preOrderByStack(){
        System.out.println("中序遍历的非递归实现:");
        Stack<Node> stack = new Stack<>();
        Node current = root;
        while(current != null || !stack.isEmpty()){
            while(current != null){
                current.display();
                stack.push(current);
                current = current.leftChild;
            }
            if(!stack.isEmpty()){
                current = stack.pop();
                current = current.rightChild;
            }

        }
        System.out.println();

    }

    //后续遍历的递归实现
    public void postOrderTraverse(){
        System.out.println("后续遍历:");
        postOrderTraverse(root);
        System.out.println();
    }

    private void postOrderTraverse(Node node) {
        // TODO Auto-generated method stub
        if(node == null){
            return ;
        }
        postOrderTraverse(node.leftChild);
        postOrderTraverse(node.rightChild);
        node.display();
    }

    //后续遍历的非递归实现:
    //这个自己写出来有难度,得多联系几次。
    public void postOrderByStack(){
        System.out.println("后序遍历的非递归实现:");
        Stack<Node> stack = new Stack<>();
        Node current = root;
        Node preNode = null;
        while(current !=null || !stack.isEmpty()){
            while(current != null){
                stack.push(current);
                current = current.leftChild;
            }
            if(!stack.isEmpty()){
                current = stack.peek().rightChild;
                if(current == null || current == preNode){
                    current = stack.pop();
                    current.display();
                    preNode = current;
                    current = null;
                }
            }
        }
        System.out.println();
    }


}


package binaryTree;

public class Demo {

    public static void main(String[] args) {
        BinaryTree btree = new BinaryTree(7);
        int [] a = {5,4,6,10,9,12};

        for(int i=0;i<a.length;i++){
            btree.insert(a[i]);
        }

//      btree.inOrderTraverse();
//      btree.preOrderTraverse();
//      btree.postOrderTraverse();
        //btree.inOrderByStack();
        //btree.preOrderByStack();
        btree.postOrderByStack();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值