二叉树非递归排序

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;

/**
 * Created by renshuang on 10/11/13.
 */
public class TestTree {


    /**
     * @param args
     */
    public static void main(String args[]) {
        TestTree tree = new TestTree();
        tree.add(11);
        tree.add(8);
        tree.add(14);
        tree.add(6);
        tree.add(9);
        tree.add(15);
        tree.add(18);

        tree.add(10);
        tree.add(8);

        int arr[] = {5, 7, 6, 9, 11, 10, 8}; // Yes

        //   int  arr[] = {7, 4, 6, 5}; // Not

        System.out.println(verfyBst(arr, 0, arr.length));
        tree.prePrint(tree.root);
        //    tree.midPrint(tree.root);
    }


    public void breadFirst() {
        Queue<Node> queue = new ArrayDeque<Node>();
        if (root != null) {
            queue.add(root);
        }
        while (!queue.isEmpty()) {
            Node current = queue.poll();
            System.out.println(current.data);
            if (current.left != null) {
                queue.add(current.left);
            }
            if (current.right != null) {
                queue.add(current.right);
            }
        }

    }


    private Node root;

    /**
     *    11
     *   /   \
     *  8     14
     * /  \   / \
     * 6    9     15
     * /     \      \
     * 8    10      18
     *
     * @param data
     */
    public void add(int data) {
        if (root == null) {
            root = new Node(data, null, null);
        } else {
            Node current = root;
            Node parent = null;
            while (current != null) {
                parent = current;
                if (current.data > data) {
                    current = current.left;
                } else {
                    current = current.right;
                }
            }
            if (parent != null) {
                if (parent.data > data) {
                    parent.left = new Node(data, null, null);
                } else {
                    parent.right = new Node(data, null, null);
                }
            }
        }
    }

    /**
     * 前序遍历  跟广度优先遍历唯一不同的是这个用到了stack
     *
     * @param root
     */
    public void prePrint(Node root) {
        Stack<Node> stack = new Stack<Node>();
        if (root != null) {
            stack.add(root);
        }
        while (!stack.isEmpty()) {
            Node current = stack.pop();
            System.out.println(current.data);
            if (current.right != null) {
                stack.push(current.right);
            }
            if (current.left != null) {
                stack.push(current.left);
            }
        }
    }

    /**
     * 算法思想:三种算法的思想都是让root的Left的Left的Left全都入栈。所以第一个while循环的逻辑,都是相同的。
     * 下面详细分析第2个while循环,这是一个出栈动作,只要栈不为空,就始终要弹出栈顶元素,
     * 由于我们之前入栈的都是Left节点,所以每次在出栈的时候,我们都要考虑Right节点是否存在。
     * 因为前序/后序/中序遍历顺序的不同,所以在具体的实现上有略为区别。
     *
     * @param root
     */
    public void midPrint(Node root) {
        Stack<Node> stack = new Stack<Node>();
        Node current = root;
        /**
         *  把当前所有左子树入栈
         */
        while (current != null) {
            stack.push(current);
            current = current.left;
        }

        while (stack.size() > 0) {
            /**
             * 出站打印当前内容
             */
            current = stack.pop();
            System.out.println(current.data);
            /**
             * 如果当前节点有右子树则把当前右孩子如栈,
             * 再判断当前右孩子有没有左孩子
             */
            if (current.right != null) {

                stack.push(current.right);
                /*
                   必须下移右孩否泽 出问题,例如节点9
                 */
                current = current.right;
                while (current != null) {
                    if (current.left != null) {
                        stack.push(current.left);
                    }
                    current = current.left;
                }
            }

        }
    }


    public void midTest(Node root) {
        Stack<Node> stack = new Stack<Node>();
        Node current = root;
        while (current != null) {
            stack.add(current);
            current = current.left;
        }
        while (stack.size() > 0) {
            current = stack.pop();
            System.out.println(current.data);

            if (current.right != null) {
                stack.push(current.right);
                current = current.right;
                while (current != null) {
                    if (current.left != null) {
                        stack.push(current.left);
                    }
                    current = current.left;
                }
            }
        }
    }


    public static boolean verfyBst(int[] data, int start, int end) {
        if (data == null || start > end) {
            return false;
        }
        if (start == end) {
            return true;
        }

        int root = data[end - 1];

        int i = start;
        for (; i < end - 1; i++) {

            if (data[i] >= root) {
                break;
            }
        }

        int j = i;
        for (; j < end - 1; j++) {
            if (data[j] < root) {
                return false;
            }
        }
        return verfyBst(data, start, i) && verfyBst(data, i, end - 1);


    }






    class Node {
        int data;
        Node left;
        Node right;

        public Node() {

        }

        public Node(int data, Node left, Node right) {
            this.data = data;
            this.right = right;
            this.left = left;
        }


    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值