【数据结构与算法】十六 二叉树遍历 Breadth-First-Search 广度优先

【数据结构与算法】十六 二叉树遍历 Breadth-First-Search 广度优先

结合二叉树文章.
Breadth-First-Search 广度优先

package com.cn.mark.algorithm.binarytree;

public class BST<T extends Comparable<? super T>> {

    private static class Node<T> {

        private T value;
        private Integer position;
        private Integer height;
        private Node<T> left;
        private Node<T> right;

        public Node(T value, Integer position, Node<T> left, Node<T> right) {
            this.value = value;
            this.position = position;
            this.height = 0;
            this.left = left;
            this.right = right;
        }
    }

    private int height(Node<T> t) {
        return t == null ? -1 : t.height;
    }

    private Node<T> root;

    public void add(T value, Integer position) {
        root = add(value, position, root);
    }

    private Node<T> add(T value, Integer position, Node<T> node) {
        if (node == null) {
            return new Node<T>(value, position, null, null);
        }

        int comparaResult = value.compareTo(node.value);

        if (comparaResult > 0)
            node.right = add(value, position, node.right);
        else if (comparaResult < 0)
            node.left = add(value, position, node.left);
        else
            System.out.println("as same a value");

        node.height = Math.max(height(node.left), height(node.right)) + 1;
        return node;

    }

    public Integer search(Node<T> node, T value) {
        if (node == null)
            return -1;
        int comparaResult = value.compareTo(node.value);
        if (comparaResult == 0)
            return node.position;
        else if (comparaResult > 0)
            return search(node.right, value);
        else if (comparaResult < 0)
            return search(node.left, value);
        else
            return -1;
    }

    private void preorder(Node<T> node) {
        if (null == node)
            return;
        System.out.print(node.value);
        preorder(node.left);
        preorder(node.right);
    }

    private void midorder(Node<T> node) {
        if (null == node)
            return;
        midorder(node.left);
        System.out.print(node.value);
        midorder(node.right);
    }

    private void postorder(Node<T> node) {
        if (null == node)
            return;
        postorder(node.left);
        postorder(node.right);
        System.out.print(node.value);
    }

    private void printLevel(Node<T> node, int level) {
        if (node == null)
            return;
        if (level == 0) {
            System.out.print(" " + node.value);
        } else {
            printLevel(node.left, level - 1);
            printLevel(node.right, level - 1);
        }
    }

    public static void main(String[] args) {
        BST<Integer> bst = new BST<Integer>();
        int[] array = { 3, 4, 2, 1, 7, 5, 8, 9, 0, 6 };
        for (int i = 0; i < array.length; i++)
            bst.add(array[i], i);//
        System.out.println("the tree height : "+bst.height(bst.root));

        int height = bst.height(bst.root);

        for (int level = 0; level < height; level++) {
            bst.printLevel(bst.root, level);
            System.out.println("\n-------------------");
        }

        // System.out.println(bst.search(bst.root, 8));
        // System.out.println(bst.search(bst.root, 0));
        // System.out.println(bst.search(bst.root, 22));

        // bst.preorder(bst.root);
        // System.out.println();
        // bst.midorder(bst.root);
        // System.out.println();
        // bst.postorder(bst.root);
    }

}

时间复杂度 O(n2)

你可能感兴趣的:(数据结构,算法,遍历,二叉树,广度优先)