最优二叉搜索树

时间:2011-09-30 23:06:09

标签: data-structures binary-search-tree

我在最佳二叉搜索树上进行了分配,并且在执行此操作时出现了一些问题。我发现许多在线链接很有帮助(仅限Google搜索),但我想知道......

为什么必须对键进行初始排序?

如果在未对键进行排序时获得较低的成本(对于最佳BST),这是否意味着我的代码中必定存在错误?

最佳BST必须完整/完美吗? (使用完整和完美的维基百科定义)

  

完美的二叉树是一个完整的二叉树,其中所有叶子处于相同的深度或相同的水平。 [1](这也模糊地称为完整的二叉树。)

     

完整的二叉树是一个二叉树,其中除了可能是最后一个级别之外,每个级别都被完全填充,并且所有节点都尽可能地离开。 [2]

对于最后一个问题,我认为最佳树必须是完整/完美的,但在线的一些小程序让我相信不然。我无法理解为什么......

2 个答案:

答案 0 :(得分:1)

  

为什么必须对键进行初始排序?

他们没有。实际上,除非你使用自平衡树,否则最好是以随机顺序将键添加到树中,因为树最终会更平衡。

  

如果在未对键进行排序时获得较低的成本(对于最佳BST),这是否意味着我的代码中必定存在错误?

除非您编写自平衡树(您的自平衡算法不起作用),否则不会。

  

最佳BST必须完整/完美吗?

是。为了尽可能快地搜索给定树,所有树的节点必须均匀分布;即树必须尽可能短。

答案 1 :(得分:0)

void OptimalBinsearchtree_output(float R[21][20],int i, int j, int r1, char  *dir)
{
          int t;
          if (i <= j)
          {
                         t =(int)R[i][j];
                         fprintf(wp,"%s is %s child of %s\n", name[t], dir, name[r1]);
                         OptimalBinsearchtree_output(R,i, t - 1, t, "left");
                         OptimalBinsearchtree_output(R,t + 1, j, t, "right");
          }
}

void OptimalBinarySearchTree(int n, const float p[],float *minavg)
{
          int i, j, k, diagonal,l,pos;
          float R[21][20];
          float min = 0;
          float A[21][20],sum=0;
          printf("\n");
          for (i = 1; i <=n; i++) 
          {
                         A[i][i - 1] = 0;
                         R[i][i - 1] = 0;
                         A[i][i] = p[i];
                         R[i][i] = i;
                         fprintf(wp,"A[%d][%d]=%4f\tA[%d][%d]=%4f\t",i,i-1,A[i][i-1],i,i,A[i][i]);
                         fprintf(wp,"R[%d][%d]=%4f\tR[%d][%d]=%4f\n", i, i - 1, R[i][i - 1], i, i, R[i][i]);
          }

          A[n+1][n] = 0;
          R[n+1][n] = 0;
          for (diagonal = 1; diagonal <= n - 1; diagonal++)
          {
                         for (i = 1; i <= n - diagonal; i++)
                         {
                                       min = 0;
                                       sum = 0;
                                       j = i + diagonal;
                                       for (l = i; l <=j; l++)
                                       {
                                                      sum = sum + p[l];
                                       }
                                       A[i][j] = sum;
                                       for (k = i; k <= j; k++)
                                       {
                                                      sum = A[i][k - 1] + A[k + 1][j];
                                                      if (min == 0)
                                                      {
                                                                    min = sum;
                                                                    pos = k;
                                                      }
                                                      else if (sum<min)
                                                      {
                                                                    min = sum;
                                                                    pos = k;
                                                      }
                                       }
                                       A[i][j] += min;
                                       R[i][j] = pos;
                         }
          }

          *minavg = A[1][n];
          printf("\n");
          for (i = 1; i <= n; i++)
          {
                         for (j = 0; j <= n; j++)
                         {
                                       printf("%0.3f ", R[i][j]);
                         }
                         printf("\n");
          }
          for (i = 1; i <= n; i++)
          {
                         for (j = 0; j <= n; j++)
                         {
                                       printf("%0.3f ", A[i][j]);
                         }
                         printf("\n");
          }
          fprintf(wp,"\n\n");
          fprintf(wp,"%s is the root of the tree\n",name[(int)R[1][n]]);
          int r1 = (int)R[1][n];
          OptimalBinsearchtree_output(R,1, r1 - 1, r1, "left");
          OptimalBinsearchtree_output(R,r1 + 1, n, r1, "right");

}


void removeall()
{
          nodeptr node,temp;
          node = head;
          while (node->next != NULL)
          {
                         temp = node;
                         node = node->next;
          }
          if (node == node->next)
          {
                         node->next = NULL;                     
                         temp->next = NULL;
                         free(node);
                         return;
          }
          node->next = NULL;
          temp->next = NULL;
          free(node);
}

void print()
{
          nodeptr curr = NULL, temp = NULL;
          curr = head;
          gl_index = 1;
          while (curr != NULL)
          {

                         curr->index = gl_index;
                         gl_p[gl_index] = curr->val;
                         strcpy(name[gl_index], curr->str);
                         gl_index++;
                         wp=fopen("Output.txt","w+");
                         fprintf(wp,"%s\t%f\t%d\n", curr->str, curr->val, curr->index);
                         curr = curr->next;
          }
}


void generatenode()
{


          int i, j;
          nodeptr temp = NULL;
          char a[20];

          while (!feof(fp))
          {
                         nodeptr curr = NULL, prev = NULL;
                         temp = (struct node*)malloc(sizeof(struct node));
                         fscanf(fp, "%s", &temp->str);
                         fgets(a, 20, fp);
                         temp->index = gl_index;
                         b = atof(a);
                         int flag = 0;
                         temp->val = b;
                         gl_p[gl_index] = temp->val;
                         gl_index++;
                         temp->next = NULL;
                         if (head == NULL)
                         {
                                       head = temp;
                                       curr = head;
                         }
                         else
                         {
                                       curr = head;
                                       while (!(strcmp(temp->str, curr->str) < 0))
                                       {
                                                      if(curr->next==NULL)
                                                      {
                                                                    curr->next = temp;
                                                                    curr = curr->next;
                                                                    temp->next = NULL;
                                                                    flag = 0;
                                                                    break;
                                                      }
                                                      else
                                                      {
                                                                    flag = 1;
                                                                    prev = curr;
                                                                    curr = curr->next;
                                                      }
                                       }
                                       if (curr == head)
                                       {
                                                      temp->next = curr;
                                                      head = temp;
                                       }
                                       else
                                       {
                                                      if (flag == 1)
                                                      {
                                                      prev->next = temp;
                                                      temp->next = curr;
                                                      }
                                       }
                                       flag = 0;
                         }
          }
}