现在的位置: 首页 > 综合 > 正文

二叉排序树的简单实现

2014年10月24日 ⁄ 综合 ⁄ 共 580字 ⁄ 字号 评论关闭
struct treenode
{
         double cnt;
         char data[32];
         treenode*  left;
         treenode* right;
         treenode()
         {
                   cnt=1.0; left=right=NULL;
         }
}*root;
int n;
void build_binary_search_tree(char *ss)
{
     treenode *ptr=root;
          while (true)
          {
         if(strcmp(ptr->data,ss) == 0)
                    {
                             ptr->cnt += 1;
                             return ;
                    }
                    else if (strcmp(ptr->data,ss) > 0)
                    {
                             if( !ptr->left )
                             {  
                                      ptr->left=new treenode();
                                      strcpy(ptr->left->data,ss);
                                      return ;
                             }
                             else ptr=ptr->left;
                    }
                    else
                    {
                            if(!ptr->right)
                             {
                                      ptr->right = new treenode();
                                      strcpy(ptr->right->data,ss);
                                      return ;
                             }
                             else ptr=ptr->right;
                    }
          }
          return ;

}

void LDR(treenode* ptr)
{
     if( ptr != NULL)
          {
                    LDR(ptr->left);
                    printf("%s %.4lf\n",ptr->data,100*ptr->cnt/n);
                    LDR(ptr->right);
          }

}

抱歉!评论已关闭.