`
standalone
  • 浏览: 606461 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Print all binary search trees

阅读更多
Problem:

Given numbers 1,2,3...N, print all binary search trees that can be constructed with these N numbers.

Solution:


package alg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AllBST {
    
    
    class Node<T extends Comparable<?>> {
        Node<T> left, right;
        T data;

        public Node(T data) {
            this(data, null, null);
        }
        
        public Node(T data, Node<T> l, Node<T> r){
            this.data = data;
            this.left = l;
            this.right = r;
        }
    }

    class BTreePrinter {

        public <T extends Comparable<?>> void printNode(Node<T> root) {
            int maxLevel = maxLevel(root);
            printNodeInternal(Collections.singletonList(root), 1, maxLevel);
        }

        public <T extends Comparable<?>> void printNodeInternal(List<Node<T>> nodes, int level, int maxLevel) {
            if (nodes.isEmpty() || isAllElementsNull(nodes))
                return;

            int floor = maxLevel - level;
            int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
            int firstSpaces = (int) Math.pow(2, (floor)) - 1;
            int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;

            printWhitespaces(firstSpaces);

            List<Node<T>> newNodes = new ArrayList<Node<T>>();
            for (Node<T> node : nodes) {
                if (node != null) {
                    System.out.print(node.data);
                    newNodes.add(node.left);
                    newNodes.add(node.right);
                } else {
                    newNodes.add(null);
                    newNodes.add(null);
                    System.out.print(" ");
                }

                printWhitespaces(betweenSpaces);
            }
            System.out.println("");

            for (int i = 1; i <= endgeLines; i++) {
                for (int j = 0; j < nodes.size(); j++) {
                    printWhitespaces(firstSpaces - i);
                    if (nodes.get(j) == null) {
                        printWhitespaces(endgeLines + endgeLines + i + 1);
                        continue;
                    }

                    if (nodes.get(j).left != null)
                        System.out.print("/");
                    else
                        printWhitespaces(1);

                    printWhitespaces(i + i - 1);

                    if (nodes.get(j).right != null)
                        System.out.print("\\");
                    else
                        printWhitespaces(1);

                    printWhitespaces(endgeLines + endgeLines - i);
                }

                System.out.println("");
            }

            printNodeInternal(newNodes, level + 1, maxLevel);
        }

        private void printWhitespaces(int count) {
            for (int i = 0; i < count; i++)
                System.out.print(" ");
        }

        private  <T extends Comparable<?>> int maxLevel(Node<T> node) {
            if (node == null)
                return 0;

            return Math.max(maxLevel(node.left), maxLevel(node.right)) + 1;
        }

        private  <T> boolean isAllElementsNull(List<T> list) {
            for (Object object : list) {
                if (object != null)
                    return false;
            }

            return true;
        }

    } 
    
    
    public ArrayList<Node<Integer>> buildBST(int i, int j){
        ArrayList<Node<Integer>> list = new ArrayList<Node<Integer>>();
        if(i > j){
            return list;
        }else if(i==j){
            Node<Integer> n = new Node<Integer>(i);
            list.add(n);
            return list;
        }else {
            for(int k=i; k<=j; k++){
                ArrayList<Node<Integer>> leftSubTrees  = buildBST(i, k-1);
                ArrayList<Node<Integer>> rightSubTrees = buildBST(k+1, j);
                if(leftSubTrees.isEmpty()){
                    for(Node<Integer> r: rightSubTrees){
                        Node<Integer> subRoot = new Node<Integer>(k, null, r);
                        list.add(subRoot);
                    }
                }else if(rightSubTrees.isEmpty()){
                    for(Node<Integer> l: leftSubTrees){
                        Node<Integer> subRoot = new Node<Integer>(k, l, null);
                        list.add(subRoot);
                    }
                }else {
                    for(Node<Integer> l: leftSubTrees)
                        for(Node<Integer> r: rightSubTrees){
                            Node<Integer> subRoot = new Node<Integer>(k, l, r);
                            list.add(subRoot);
                        }
                }
            }
            return list;
        }                
    }    
    
    public int calculateAllBstCount(int nodeNum){
        int sum = 0;
        if(nodeNum <= 1){
            return 1;
        }
        for(int i = 1; i <= nodeNum; i++){
            int left = calculateAllBstCount(i-1);
            int right = calculateAllBstCount(nodeNum - i);
            sum += left * right;
        }
        return sum;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        AllBST bst = new AllBST();
        AllBST.BTreePrinter btp = bst.new BTreePrinter();
        ArrayList<Node<Integer>> trees = bst.buildBST(1, 6);
        int total = bst.calculateAllBstCount(6);
        System.out.println("Total: " + total);
        int index = 0;
        for(Node<Integer> root : trees){
            System.out.println("result " + index++);            
            btp.printNode(root);            
            System.out.println("");
        }        
        
    }

}




Result:
Total: 132
result 0
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       3
                                                        \
                                                         \
                                                          \
                                                           \
                                                           4
                                                            \
                                                             \
                                                             5
                                                              \
                                                              6


result 1
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       3
                                                        \
                                                         \
                                                          \
                                                           \
                                                           4
                                                            \
                                                             \
                                                             6
                                                            /
                                                            5


result 2
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           3
                            \
                             \
                             5
                            / \
                            4 6


result 3
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       3
                                                        \
                                                         \
                                                          \
                                                           \
                                                           6
                                                          /
                                                         /
                                                         4
                                                          \
                                                          5


result 4
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       3
                                                        \
                                                         \
                                                          \
                                                           \
                                                           6
                                                          /
                                                         /
                                                         5
                                                        /
                                                        4


result 5
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           4
                          / \
                         /   \
                         3   5
                              \
                              6


result 6
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           4
                          / \
                         /   \
                         3   6
                            /
                            5


result 7
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           5
                          / \
                         /   \
                         3   6
                          \
                          4


result 8
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           5
                          / \
                         /   \
                         4   6
                        /
                        3


result 9
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       6
                                                      /
                                                     /
                                                    /
                                                   /
                                                   3
                                                    \
                                                     \
                                                     4
                                                      \
                                                      5


result 10
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       6
                                                      /
                                                     /
                                                    /
                                                   /
                                                   3
                                                    \
                                                     \
                                                     5
                                                    /
                                                    4


result 11
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           6
                          /
                         /
                         4
                        / \
                        3 5


result 12
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       6
                                                      /
                                                     /
                                                    /
                                                   /
                                                   5
                                                  /
                                                 /
                                                 3
                                                  \
                                                  4


result 13
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               2
                                                \
                                                 \
                                                  \
                                                   \
                                                    \
                                                     \
                                                      \
                                                       \
                                                       6
                                                      /
                                                     /
                                                    /
                                                   /
                                                   5
                                                  /
                                                 /
                                                 4
                                                /
                                                3


result 14
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       3
                      / \
                     /   \
                    /     \
                   /       \
                   2       4
                            \
                             \
                             5
                              \
                              6


result 15
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       3
                      / \
                     /   \
                    /     \
                   /       \
                   2       4
                            \
                             \
                             6
                            /
                            5


result 16
       1
        \
         \
          \
           \
           3
          / \
         /   \
         2   5
            / \
            4 6


result 17
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       3
                      / \
                     /   \
                    /     \
                   /       \
                   2       6
                          /
                         /
                         4
                          \
                          5


result 18
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       3
                      / \
                     /   \
                    /     \
                   /       \
                   2       6
                          /
                         /
                         5
                        /
                        4


result 19
       1
        \
         \
          \
           \
           4
          / \
         /   \
         2   5
          \   \
          3   6


result 20
       1
        \
         \
          \
           \
           4
          / \
         /   \
         2   6
          \ /
          3 5


result 21
       1
        \
         \
          \
           \
           4
          / \
         /   \
         3   5
        /     \
        2     6


result 22
       1
        \
         \
          \
           \
           4
          / \
         /   \
         3   6
        /   /
        2   5


result 23
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      / \
                     /   \
                    /     \
                   /       \
                   2       6
                    \
                     \
                     3
                      \
                      4


result 24
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      / \
                     /   \
                    /     \
                   /       \
                   2       6
                    \
                     \
                     4
                    /
                    3


result 25
       1
        \
         \
          \
           \
           5
          / \
         /   \
         3   6
        / \
        2 4


result 26
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      / \
                     /   \
                    /     \
                   /       \
                   4       6
                  /
                 /
                 2
                  \
                  3


result 27
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      / \
                     /   \
                    /     \
                   /       \
                   4       6
                  /
                 /
                 3
                /
                2


result 28
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       2
                                        \
                                         \
                                          \
                                           \
                                           3
                                            \
                                             \
                                             4
                                              \
                                              5


result 29
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       2
                                        \
                                         \
                                          \
                                           \
                                           3
                                            \
                                             \
                                             5
                                            /
                                            4


result 30
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       6
                      /
                     /
                    /
                   /
                   2
                    \
                     \
                     4
                    / \
                    3 5


result 31
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       2
                                        \
                                         \
                                          \
                                           \
                                           5
                                          /
                                         /
                                         3
                                          \
                                          4


result 32
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       2
                                        \
                                         \
                                          \
                                           \
                                           5
                                          /
                                         /
                                         4
                                        /
                                        3


result 33
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       6
                      /
                     /
                    /
                   /
                   3
                  / \
                 /   \
                 2   4
                      \
                      5


result 34
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       6
                      /
                     /
                    /
                   /
                   3
                  / \
                 /   \
                 2   5
                    /
                    4


result 35
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       6
                      /
                     /
                    /
                   /
                   4
                  / \
                 /   \
                 2   5
                  \
                  3


result 36
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       6
                      /
                     /
                    /
                   /
                   4
                  / \
                 /   \
                 3   5
                /
                2


result 37
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       5
                                      /
                                     /
                                    /
                                   /
                                   2
                                    \
                                     \
                                     3
                                      \
                                      4


result 38
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       5
                                      /
                                     /
                                    /
                                   /
                                   2
                                    \
                                     \
                                     4
                                    /
                                    3


result 39
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       6
                      /
                     /
                    /
                   /
                   5
                  /
                 /
                 3
                / \
                2 4


result 40
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       5
                                      /
                                     /
                                    /
                                   /
                                   4
                                  /
                                 /
                                 2
                                  \
                                  3


result 41
                               1
                                \
                                 \
                                  \
                                   \
                                    \
                                     \
                                      \
                                       \
                                        \
                                         \
                                          \
                                           \
                                            \
                                             \
                                              \
                                               \
                                               6
                                              /
                                             /
                                            /
                                           /
                                          /
                                         /
                                        /
                                       /
                                       5
                                      /
                                     /
                                    /
                                   /
                                   4
                                  /
                                 /
                                 3
                                /
                                2


result 42
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               3
                        \
                         \
                          \
                           \
                           4
                            \
                             \
                             5
                              \
                              6


result 43
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               3
                        \
                         \
                          \
                           \
                           4
                            \
                             \
                             6
                            /
                            5


result 44
       2
      / \
     /   \
    /     \
   /       \
   1       3
            \
             \
             5
            / \
            4 6


result 45
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               3
                        \
                         \
                          \
                           \
                           6
                          /
                         /
                         4
                          \
                          5


result 46
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               3
                        \
                         \
                          \
                           \
                           6
                          /
                         /
                         5
                        /
                        4


result 47
       2
      / \
     /   \
    /     \
   /       \
   1       4
          / \
         /   \
         3   5
              \
              6


result 48
       2
      / \
     /   \
    /     \
   /       \
   1       4
          / \
         /   \
         3   6
            /
            5


result 49
       2
      / \
     /   \
    /     \
   /       \
   1       5
          / \
         /   \
         3   6
          \
          4


result 50
       2
      / \
     /   \
    /     \
   /       \
   1       5
          / \
         /   \
         4   6
        /
        3


result 51
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
                      /
                     /
                    /
                   /
                   3
                    \
                     \
                     4
                      \
                      5


result 52
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
                      /
                     /
                    /
                   /
                   3
                    \
                     \
                     5
                    /
                    4


result 53
       2
      / \
     /   \
    /     \
   /       \
   1       6
          /
         /
         4
        / \
        3 5


result 54
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
                      /
                     /
                    /
                   /
                   5
                  /
                 /
                 3
                  \
                  4


result 55
               2
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
                      /
                     /
                    /
                   /
                   5
                  /
                 /
                 4
                /
                3


result 56
       3
      / \
     /   \
    /     \
   /       \
   1       4
    \       \
     \       \
     2       5
              \
              6


result 57
       3
      / \
     /   \
    /     \
   /       \
   1       4
    \       \
     \       \
     2       6
            /
            5


result 58
   3
  / \
 /   \
 1   5
  \ / \
  2 4 6


result 59
       3
      / \
     /   \
    /     \
   /       \
   1       6
    \     /
     \   /
     2   4
          \
          5


result 60
       3
      / \
     /   \
    /     \
   /       \
   1       6
    \     /
     \   /
     2   5
        /
        4


result 61
       3
      / \
     /   \
    /     \
   /       \
   2       4
  /         \
 /           \
 1           5
              \
              6


result 62
       3
      / \
     /   \
    /     \
   /       \
   2       4
  /         \
 /           \
 1           6
            /
            5


result 63
   3
  / \
 /   \
 2   5
/   / \
1   4 6


result 64
       3
      / \
     /   \
    /     \
   /       \
   2       6
  /       /
 /       /
 1       4
          \
          5


result 65
       3
      / \
     /   \
    /     \
   /       \
   2       6
  /       /
 /       /
 1       5
        /
        4


result 66
       4
      / \
     /   \
    /     \
   /       \
   1       5
    \       \
     \       \
     2       6
      \
      3


result 67
       4
      / \
     /   \
    /     \
   /       \
   1       6
    \     /
     \   /
     2   5
      \
      3


result 68
       4
      / \
     /   \
    /     \
   /       \
   1       5
    \       \
     \       \
     3       6
    /
    2


result 69
       4
      / \
     /   \
    /     \
   /       \
   1       6
    \     /
     \   /
     3   5
    /
    2


result 70
   4
  / \
 /   \
 2   5
/ \   \
1 3   6


result 71
   4
  / \
 /   \
 2   6
/ \ /
1 3 5


result 72
       4
      / \
     /   \
    /     \
   /       \
   3       5
  /         \
 /           \
 1           6
  \
  2


result 73
       4
      / \
     /   \
    /     \
   /       \
   3       6
  /       /
 /       /
 1       5
  \
  2


result 74
       4
      / \
     /   \
    /     \
   /       \
   3       5
  /         \
 /           \
 2           6
/
1


result 75
       4
      / \
     /   \
    /     \
   /       \
   3       6
  /       /
 /       /
 2       5
/
1


result 76
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
        \
         \
          \
           \
           2
            \
             \
             3
              \
              4


result 77
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
        \
         \
          \
           \
           2
            \
             \
             4
            /
            3


result 78
       5
      / \
     /   \
    /     \
   /       \
   1       6
    \
     \
     3
    / \
    2 4


result 79
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
        \
         \
          \
           \
           4
          /
         /
         2
          \
          3


result 80
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       1               6
        \
         \
          \
           \
           4
          /
         /
         3
        /
        2


result 81
       5
      / \
     /   \
    /     \
   /       \
   2       6
  / \
 /   \
 1   3
      \
      4


result 82
       5
      / \
     /   \
    /     \
   /       \
   2       6
  / \
 /   \
 1   4
    /
    3


result 83
       5
      / \
     /   \
    /     \
   /       \
   3       6
  / \
 /   \
 1   4
  \
  2


result 84
       5
      / \
     /   \
    /     \
   /       \
   3       6
  / \
 /   \
 2   4
/
1


result 85
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       4               6
      /
     /
    /
   /
   1
    \
     \
     2
      \
      3


result 86
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       4               6
      /
     /
    /
   /
   1
    \
     \
     3
    /
    2


result 87
       5
      / \
     /   \
    /     \
   /       \
   4       6
  /
 /
 2
/ \
1 3


result 88
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       4               6
      /
     /
    /
   /
   3
  /
 /
 1
  \
  2


result 89
               5
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
       /               \
       4               6
      /
     /
    /
   /
   3
  /
 /
 2
/
1


result 90
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           3
                            \
                             \
                             4
                              \
                              5


result 91
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           3
                            \
                             \
                             5
                            /
                            4


result 92
               6
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           2
            \
             \
             4
            / \
            3 5


result 93
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           5
                          /
                         /
                         3
                          \
                          4


result 94
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       2
                        \
                         \
                          \
                           \
                           5
                          /
                         /
                         4
                        /
                        3


result 95
               6
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           3
          / \
         /   \
         2   4
              \
              5


result 96
               6
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           3
          / \
         /   \
         2   5
            /
            4


result 97
               6
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           4
          / \
         /   \
         2   5
          \
          3


result 98
               6
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           4
          / \
         /   \
         3   5
        /
        2


result 99
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      /
                     /
                    /
                   /
                   2
                    \
                     \
                     3
                      \
                      4


result 100
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      /
                     /
                    /
                   /
                   2
                    \
                     \
                     4
                    /
                    3


result 101
               6
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           5
          /
         /
         3
        / \
        2 4


result 102
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      /
                     /
                    /
                   /
                   4
                  /
                 /
                 2
                  \
                  3


result 103
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               1
                \
                 \
                  \
                   \
                    \
                     \
                      \
                       \
                       5
                      /
                     /
                    /
                   /
                   4
                  /
                 /
                 3
                /
                2


result 104
               6
              /
             /
            /
           /
          /
         /
        /
       /
       2
      / \
     /   \
    /     \
   /       \
   1       3
            \
             \
             4
              \
              5


result 105
               6
              /
             /
            /
           /
          /
         /
        /
       /
       2
      / \
     /   \
    /     \
   /       \
   1       3
            \
             \
             5
            /
            4


result 106
       6
      /
     /
    /
   /
   2
  / \
 /   \
 1   4
    / \
    3 5


result 107
               6
              /
             /
            /
           /
          /
         /
        /
       /
       2
      / \
     /   \
    /     \
   /       \
   1       5
          /
         /
         3
          \
          4


result 108
               6
              /
             /
            /
           /
          /
         /
        /
       /
       2
      / \
     /   \
    /     \
   /       \
   1       5
          /
         /
         4
        /
        3


result 109
       6
      /
     /
    /
   /
   3
  / \
 /   \
 1   4
  \   \
  2   5


result 110
       6
      /
     /
    /
   /
   3
  / \
 /   \
 1   5
  \ /
  2 4


result 111
       6
      /
     /
    /
   /
   3
  / \
 /   \
 2   4
/     \
1     5


result 112
       6
      /
     /
    /
   /
   3
  / \
 /   \
 2   5
/   /
1   4


result 113
               6
              /
             /
            /
           /
          /
         /
        /
       /
       4
      / \
     /   \
    /     \
   /       \
   1       5
    \
     \
     2
      \
      3


result 114
               6
              /
             /
            /
           /
          /
         /
        /
       /
       4
      / \
     /   \
    /     \
   /       \
   1       5
    \
     \
     3
    /
    2


result 115
       6
      /
     /
    /
   /
   4
  / \
 /   \
 2   5
/ \
1 3


result 116
               6
              /
             /
            /
           /
          /
         /
        /
       /
       4
      / \
     /   \
    /     \
   /       \
   3       5
  /
 /
 1
  \
  2


result 117
               6
              /
             /
            /
           /
          /
         /
        /
       /
       4
      / \
     /   \
    /     \
   /       \
   3       5
  /
 /
 2
/
1


result 118
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           2
            \
             \
             3
              \
              4


result 119
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           2
            \
             \
             4
            /
            3


result 120
               6
              /
             /
            /
           /
          /
         /
        /
       /
       5
      /
     /
    /
   /
   1
    \
     \
     3
    / \
    2 4


result 121
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           4
          /
         /
         2
          \
          3


result 122
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       1
        \
         \
          \
           \
           4
          /
         /
         3
        /
        2


result 123
               6
              /
             /
            /
           /
          /
         /
        /
       /
       5
      /
     /
    /
   /
   2
  / \
 /   \
 1   3
      \
      4


result 124
               6
              /
             /
            /
           /
          /
         /
        /
       /
       5
      /
     /
    /
   /
   2
  / \
 /   \
 1   4
    /
    3


result 125
               6
              /
             /
            /
           /
          /
         /
        /
       /
       5
      /
     /
    /
   /
   3
  / \
 /   \
 1   4
  \
  2


result 126
               6
              /
             /
            /
           /
          /
         /
        /
       /
       5
      /
     /
    /
   /
   3
  / \
 /   \
 2   4
/
1


result 127
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       4
      /
     /
    /
   /
   1
    \
     \
     2
      \
      3


result 128
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       4
      /
     /
    /
   /
   1
    \
     \
     3
    /
    2


result 129
               6
              /
             /
            /
           /
          /
         /
        /
       /
       5
      /
     /
    /
   /
   4
  /
 /
 2
/ \
1 3


result 130
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       4
      /
     /
    /
   /
   3
  /
 /
 1
  \
  2


result 131
                               6
                              /
                             /
                            /
                           /
                          /
                         /
                        /
                       /
                      /
                     /
                    /
                   /
                  /
                 /
                /
               /
               5
              /
             /
            /
           /
          /
         /
        /
       /
       4
      /
     /
    /
   /
   3
  /
 /
 2
/
1




分享到:
评论

相关推荐

    Multidimensional Binary Search Trees Used for Associative Searching

    1975年,来自斯坦福大学的Jon Louis Bentley在ACM杂志上发表的一篇论文:Multidimensional Binary Search Trees Used for Associative Searching 中正式提出和阐述的了把空间划分为多个部分的k-d树。

    Self-Adjusting Binary Search Trees (1985)-计算机科学

    Self-Adjusting Binary Search TreesDANIEL DOMINIC SLEATOR... On an n-node splay tree, all the standard search tree operations have an amortized time bound of @log n) per operation, where by “amortized t

    binary_trees-源码

    二元搜索树(Binary Search Tree, BST)是一种特殊的二元树,其中每个节点的值大于其左子树中的所有节点值,小于其右子树中的所有节点值。BST支持高效的查找、插入和删除操作,时间复杂度可达到O(log n)。 在`...

    折半查找 binarySearch

    A binary search algorithm (or binary chop) is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value, by comparing an ...

    你清楚Arrays.binarySearch()方法的返回值吗?

    在Java编程语言中,`Arrays`类是Java.util包下的一个非常重要的工具类,它提供了大量用于操作数组的静态方法,其中包括我们今天要讨论的`binarySearch()`方法。`Arrays.binarySearch()`方法允许我们在有序数组中查找...

    BinarySearch

    标题中的"BinarySearch"指的是二分查找算法,这是一种在有序数组中查找特定元素的搜索算法。它的基本思想是将数组分为三个部分:小于目标值的元素、等于目标值的元素和大于目标值的元素,然后逐步缩小搜索范围,直到...

    Optimal Binary Search Tree

    在Knuth的经典论文《Optimum Binary Search Trees》中,他首先回顾了二叉查找树的基本概念及工作原理,即通过比较节点值来决定搜索方向,从而实现快速查找。具体来说,当需要在一个二叉查找树中查找某个元素时,可以...

    java-leetcode题解之All Possible Full Binary Trees.java

    java基础 java_leetcode题解之All Possible Full Binary Trees.java

    matlab开发-BinarySearch

    在MATLAB中,二进制搜索(Binary Search)是一种高效查找算法,尤其适用于已排序的数据。这个"matlab开发-BinarySearch"项目提供了一个名为`bsearch.m`的MATLAB函数,用于在向量中执行二进制查找操作。下面我们将...

    Search in a Binary Search Tree.zip

    二叉搜索树(Binary Search Tree,BST)是一种特殊类型的二叉树,它的每个节点都包含一个键(key)、一个关联的值、一个指向左子树的引用和一个指向右子树的引用。在二叉搜索树中,对于任意节点,其左子树中的所有...

    Binary_Search_Data.rar_binary_binary search

    - 在`binarySearch`函数中实现上述的二分搜索逻辑,包括计算中间索引、比较元素和更新边界。 总的来说,二分搜索是计算机科学中一种重要的搜索算法,尤其适用于处理大量有序数据。在C语言中实现二分搜索,可以显著...

    matlab开发-Binarysearch

    在MATLAB环境中,二进制搜索(Binary Search)是一种高效的查找算法,主要应用于已排序的数组。本项目“matlab开发-Binarysearch”显然聚焦于如何在MATLAB中实现二进制搜索算法。该算法利用分治思想,通过不断缩小...

    optimal binary search tree

    最小成本二分检索树optimal binary optimal binary

    BinarySearch_C++_算法_折半查找_

    在这个“BinarySearch_C++_算法_折半查找_”的项目中,我们将会探讨如何使用C++实现这个算法。 首先,我们要明白二分查找的基本思想。假设我们有一个已排序的数组,我们需要查找一个特定值。我们从数组的中间开始...

    二叉搜索树(Binary Search Tree,BST)的实现与操作.docx

    二叉搜索树(Binary Search Tree,BST)的实现与操作.docx 二叉搜索树(Binary Search Tree,BST)的实现与操作.docx 二叉搜索树(Binary Search Tree,BST)的实现与操作.docx 二叉搜索树(Binary Search Tree,BST...

    Binary Search Tree

    Binary Search Tree 利用C++實現 Binary Search Tree

    陈越、何钦铭-数据结构作业16:Complete Binary Search Tree完全二叉搜索树

    Both the left and right subtrees must also be binary search trees. A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled ...

    二分查找 Binary Search(C++)

    int binarySearch(int arr[], int left, int right, int target) { while (left ) { int mid = left + (right - left) / 2; // 计算中间索引,避免整型溢出 if (arr[mid] == target) { // 如果中间元素就是目标...

    BinarySearch.java

    实现折半查找的程序 希望大家功能学习,共同进步

    python-leetcode题解之096-Unique-Binary-Search-Trees

    python python_leetcode题解之096_Unique_Binary_Search_Trees

Global site tag (gtag.js) - Google Analytics