二叉树的创建和非递归先中后遍历

二叉树的创建和非递归遍历

import org.junit.Test;

import java.util.Stack;


public class createTree {
    Node root = null;

    /**
     * 创建
     * @param data
     */
    public void add1(int data){
        Node newNode = new Node(data);
        if(root == null){
            root = newNode;
            return ;
        }
        Node current = root;
        Node parent;
        while(true){
            parent = current;
            if(data<current.data){
                current = current.left;
                if(current == null){
                    parent.left = newNode;
                    return ;
                }
            }else{
                current = current.right;
                if(current == null){
                    parent.right = newNode;
                    return;
                }
            }
        }
    }

    /**
     * 非递归先序遍历
     */
    public void preOrder(Node root){
        if(root != null){
            Stack<Node> stack = new Stack<>();
            stack.add(root);
            while (!stack.isEmpty()){
                root = stack.pop();
                System.out.print(root.data+" ");
                if(root.right!=null){
                    stack.add(root.right);
                }
                if(root.left != null){
                    stack.add(root.left);
                }
            }
        }
        System.out.println();
    }

    /**
     * 非递归中序遍历
     * @param root
     */
    public void inOrder(Node root){
        if(root != null){
            Stack<Node> stack = new Stack<>();
            while(!stack.isEmpty() || root!=null){
                if(root != null){
                    stack.push(root);
                    root = root.left;
                }else{
                    root = stack.pop();
                    System.out.print(root.data+" ");
                    root = root.right;
                }
            }
        }
        System.out.println();
    }

    /**
     * 非递归后续遍历
     * @param root
     */
    public void posOrder(Node root){
        if(root != null){
            Stack<Node> s1 = new Stack<>();
            Stack<Node> s2 = new Stack<>();
            s1.push(root);
            while(!s1.isEmpty()){
                root = s1.pop();
                s2.push(root);
                if(root.left != null){
                    s1.push(root.left);
                }
                if(root.right != null){
                    s1.push(root.right);
                }
            }
            while(!s2.isEmpty()){
                System.out.print(s2.pop().data+" ");
            }
        }
        System.out.println();
    }
 //最普通的创建方式,测试时比较实用
    public Node add(){
        Node no1 = new Node(1);
        Node no2 = new Node(2);
        Node no3 = new Node(3);
        Node no4 = new Node(4);
        Node no5 = new Node(5);
        Node no6 = new Node(6);
        Node no7 = new Node(7);
        no1.left = no2;
        no1.right = no3;
        no2.left = no4;
        no2.right = no5;
        no3.left = no6;
        no3.right = no7;
        return no1;
    }

    @Test
    public void test(){
        Node temp1 = add();
        Node temp2 = add();
        Node temp3 = add();
        preOrder(temp1);
        inOrder(temp2);
        posOrder(temp3);
    }
}
那位大哥有按层创建二叉树的方式和思想,能不能分享一下,在此万分感谢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值