Beispiel #1
0
 public BSTree(int[] a)
 {
     a    = a.Distinct().ToArray();
     root = new NonEmptyNode(a[0]);
     root.setLeft(EmptyNode.getInstance());
     root.setRight(EmptyNode.getInstance());
     for (int i = 1; i < a.Length; i++)
     {
         insert(a[i]);
     }
 }
Beispiel #2
0
        public static Node sortedArrayToBST(int[] a, int start, int end)
        {
            if (start > end)
            {
                return(factory.createEmptyNode());
            }
            int  mid  = (start + end) / 2;
            Node root = factory.createNonEmptyNode(a[mid]);

            root.setLeft(sortedArrayToBST(a, start, mid - 1));
            root.setRight(sortedArrayToBST(a, mid + 1, end));

            return(root);
        }
Beispiel #3
0
 public BSTree(int[] a, bool isBalancedTree)
 {
     a = a.Distinct().ToArray();
     if (isBalancedTree)
     {
         Array.Sort(a);
         root = BSTBuilder.sortedArrayToBST(a, 0, a.Length - 1);
     }
     else
     {
         root = new NonEmptyNode(a[0]);
         root.setLeft(EmptyNode.getInstance());
         root.setRight(EmptyNode.getInstance());
         for (int i = 1; i < a.Length; i++)
         {
             insert(a[i]);
         }
     }
 }