C二进制搜索树。实施问题

时间:2016-07-16 21:05:35

标签: c data-structures binary-search-tree

我正在创建一个二进制搜索树,其中包含一个字符串作为键和2个整数。然后,我需要在一个程序中使用该树,该程序需要两个人得分,而得分较高的人在win int中获得+1,而输家在输入int中获得+1。到目前为止它只适用于2个不同的对手。如果我输入超过2个独特的对手,它只会显示最近的2个。

这是二叉树

#include"bst.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

/*
 * struct Player{
 * char * name;
 * int wins;
 * int losses;
 * struct Player * left;
 * struct Player * right;
 */

void bst_delete(struct Player * tree)
{
    if(tree !=0){
    bst_delete(tree->left);
    bst_delete(tree->right);
    free(tree);
    }
}

struct Player * bst_insert(struct Player * node, char * name, int wins, int losses)
{
    struct Player * temp;
    temp=node;
    if(temp==NULL)
    {
        temp=(struct Player *)malloc(sizeof(struct Player));
        temp->name=strdup(name);
        temp->wins=wins;
        temp->losses=losses;
        temp->left=temp->right=NULL;
        return temp;
    }
    if(strcmp(name,temp->name)<0)
    {
        temp->left=bst_insert(temp->left,name,wins,losses);
    }
    else if(strcmp(name,temp->name)>0)
    {
        temp->right=bst_insert(temp->left,name,wins,losses);
    }
    return temp;
}

struct Player * bst_find(struct Player * node, char * name)
{
    int cmp;
    if(node==NULL)
        return NULL;

    cmp=strcmp(name,node->name);
    if(cmp<0)
        return bst_find(node->left,name);
    if(cmp>0)
        return bst_find(node->right,name);
    return node;


/*  struct Player * temp = node;
    if(temp==NULL)
        return NULL;
    int cmp=strcmp(name,temp->name);
    while(cmp!=0){
        cmp=strcmp(name,temp->name);
        if(temp==NULL)
            return temp;
        if(cmp<0)
            temp=temp->left;
        if(cmp>0)
            temp=temp->right;

    }
    return temp;
*/
}

void bst_print(struct Player * tree)
{
    if(tree==NULL)
    {
       return;
    }
    bst_print(tree->left);
    printf("%s,", tree->name);
    printf("%d,", tree->wins);
    printf("%d \n", tree->losses);
    bst_print(tree->right);
}

这是使用树的程序

#include"bst.h"
#include<stdio.h>
#include<stdlib.h>



void init();

struct Player * tree;

int main()
{
    init();
    bst_print(tree);
    return 0;
}

void init()
{
    int x,y,ch;
    char str[20];
    char str2[20];

    while(scanf(" %s", str)!=EOF){
    ch=scanf(" %d", &x);
    if(ch==EOF && ch==0)
    {
        fprintf(stderr,"Enter a number \n");
        return;
    }


    scanf(" %s", str2);
    ch=scanf(" %d", &y);
    if(ch==EOF && ch==0)
    {
        fprintf(stderr,"Enter a number \n");
        return;
    }

    struct Player * node = tree;

    if(x>y){
        if((bst_find(tree,str)==NULL) && (bst_find(tree,str2)==NULL)){
          tree=bst_insert(tree,str,1,0);
          tree=bst_insert(tree,str2,0,1);
          }
        else if((bst_find(tree,str)!=NULL) && (bst_find(tree,str2)==NULL)){
          tree=bst_insert(tree,str2,0,1);
          node=bst_find(tree,str);
          node->wins +=1;
          }
        else if((bst_find(tree,str)==NULL) && (bst_find(tree,str2)!=NULL)){
          tree=bst_insert(tree,str,1,0);
          node=bst_find(tree,str2);
          node->losses +=1;
          }
        else/*((bst_find(tree,str)!=NULL) && (bst_find(tree,str2)!=NULL))*/{
          node=bst_find(tree,str);
          node->wins +=1;
          node=bst_find(tree,str2);
          node->losses +=1;
          }
    }

    else{
        if((bst_find(tree,str)==NULL) && (bst_find(tree,str2)==NULL)){
          tree=bst_insert(tree,str,0,1);
          tree=bst_insert(tree,str2,1,0);
          }
        else if((bst_find(tree,str)!=NULL) && (bst_find(tree,str2)==NULL)){
          tree=bst_insert(tree,str2,1,0);
          node=bst_find(tree,str);
          node->losses +=1;
          }
        else if((bst_find(tree,str)==NULL) && (bst_find(tree,str2)!=NULL)){
          tree=bst_insert(tree,str,0,1);
          node=bst_find(tree,str2);
          node->wins +=1;
          }
        else/*((bst_find(tree,str)!=NULL) && (bst_find(tree,str2)!=NULL))*/{
          node=bst_find(tree,str2);
          node->wins +=1;
          node=bst_find(tree,str);
          node->losses +=1;
          }
    }
    }
}

例如,如果我输入以下内容:

  

a 5 b 6

     

a 5 b 9

然后点击ctrl d然后它会打印:

  

a,0,2

     

B,2,0。

没关系,但如果我输入:

  

a 5 b 6

     

a 5 b 9

     

a 6 c 3

它只输出:

  

a,1,2

     

C,0,1

什么时候应该输出:

  

a,1,2

     

b,2,0

     

C,0,1

0 个答案:

没有答案