Binary Keresés fa belsejében bináris keresési fa

szavazat
0

Van egy házi amely kérdezni tőlem, hogy hozzon létre egy struct bináris keresési fa, ahol a csomópont a bináris keresési fa egy másik bináris keresési fa. Az első BST van a vezetéknevek a diákok és a másik pedig az első nevek és id. Akkor is, ha valaki ugyanazt a családi egy másik hallgató Nem szabad létrehozni egy „névre” node de azt kell, hogy hozzon létre benne a meglévő „névre” node másik „első nevét és Id” csomópontot. Ahhoz, hogy konkrétabb:

typedef struct nameANDid{ //name and id nodes
    char first[20];
    int ID;
    struct nameANDid *nleft;
    struct nameANDid *nright;
}yohoho;
typedef struct node{  //surname nodes
   char last[20];  
   struct nameANDid yohoho;  
   struct node *left;
   struct node *right;
}node;

A fő probléma az, hogy hozzon létre egy másik nameANDid csomópont minden keresztnev találtam, mert a következő kód hozhatok 2 BST egyet a családnevek és egy másikat a neveket, de szeretnék olyan lenni, mint például: Ha van ilyen diákok

 Stallone Sylvester 11111111
 Stallone Noah      22222222
 Norris   Chuck     33333333
 Hogan    Hulk      44444444
 Hogan    Daniel    55555555

Azt szeretné tárolni őket, mint ez: .........

 Stallone Sylvester 11111111
          Noah      22222222
 Norris   Chuck     33333333
 Hogan    Hulk      44444444
          Daniel    55555555

Ahelyett, hogy ezt veszem valami hasonló: ...........

 Stallone  Sylvester 11111111.
           Noah      22222222 
           Chuck     33333333
           Hulk      44444444 
           Daniel    55555555

 Norris  Sylvester 11111111.
           Noah      22222222 
           Chuck     33333333
           Hulk      44444444 
           Daniel    55555555
 Hogan    Sylvester 11111111.
           Noah      22222222 
           Chuck     33333333
           Hulk      44444444 
           Daniel    55555555

Adom itt néhány funkció annak érdekében, hogy pontosabb

A terhelési funkció betölti a neveket a txt dokumentumot.

void loadData(struct node *temp){      
int i;
FILE *fp;
fp=fopen(FILENAME,r);
if (fp == NULL) printf(File does not exist\n);
for (i=0; i<5; i++){                
    fscanf(fp,%s,&temp->last);
    fscanf(fp,%s,&temp->yohoho.first);
    fscanf(fp,%d,&temp->yohoho.ID);                 
    top=add_node(top,temp);  //this function create a surname node        
    }        
fclose(fp);     
    printf(\n\nFile loaded\n);  
}

hol

        struct node temp;//just  a node pointer
        struct node *top=NULL; //shows the top of the tree

A addNode funkció: ...

      struct node * add_node (struct node *top, struct node *temp){  
           struct node *newNode;  
           if (top == NULL){    
           newNode=(struct node *)malloc(sizeof(struct node));
           temp->left=NULL;
           temp->right=NULL;     
           if (memcpy(newNode,temp,sizeof(struct node)) == NULL){
               printf(Node addition failed\n);
               return NULL;}
           else {             
               topname=add_node_nameANDid(topname,&temp->yohoho); //Call the add_node_nameANDid to create a new name node in the other tree                           
               return newNode;}
            }
           else {   
               if (stricmp(temp->last,top->last) < 0){ //Insert node surname left
                     top->left=add_node(top->left,temp);}
               else if (stricmp(temp->last,top->last) == 0){         
                     topname=add_node_nameANDid(topname,&temp->yohoho);  //Call the add_node_nameANDid to create a new name node in the other tree   if i have the same surname        
               }
               else {
                     top->right=add_node(top->right,temp);           
               }
               return top;
             } 
             return NULL;
         }

És add_node_nameANDid () függvény olyan, mint az előző funkciót, de van néhány változót változott:

      struct nameANDid * add_node_nameANDid (struct nameANDid *topname, struct nameANDid *temp2){
        struct nameANDid *newNode_nameANDid;     
        if (topname == NULL){ 
            newNode_nameANDid=(struct nameANDid *)malloc(sizeof(struct nameANDid));
            temp2->nleft=NULL;
            temp2->nright=NULL;
            if (memcpy(newNode_nameANDid,temp2,sizeof(struct nameANDid)) == NULL){
                   printf(Node addition failed\n);
                   return NULL;}
            else {                 
                   return newNode_nameANDid;}
            }
        else {   
             if (stricmp(temp2->first,topname->first) <= 0){       
                  topname->nleft=add_node_nameANDid(topname->nleft,temp2);}
        else {         
                  topname->nright=add_node_nameANDid(topname->nright,temp2);}  
        return topname;
        } 
     return NULL;
    }

Elnézést a hatalmas forráskódot, hogy én csak feltölteni, de ez nagyon nehéz lenne megmagyarázni anélkül, hogy ez.

Azt hiszem, hogy van két probléma, de nem az a tudás, hogy megoldja őket.

ELSŐ: Azt kell létrehozni a különböző keresztnev BST minden névre csomópont, és azt hiszem, hogy én nem teszek ilyet, de nem tudom, hogyan kell csinálni, hogy ...

Valami javaslat?

A kérdést 29/05/2011 12:22
a forrás felhasználó
Más nyelveken...                            


1 válasz

szavazat
2

Adtam példát végrehajtásának alábbi megjegyzést megmagyarázni, hogyan közelítették meg ezt. Meg kell tudni használni a gondolatok, hogy módosítsa a módja a kód működik. Vegye figyelembe, hogy ez nem egy tökéletes kivitelezés, le a fejem tetején, látom a következő problémákat.

  1. A rekurzív , ami azt jelenti, a mélység fa képes kezelni korlátozza a méret a verem a célgép. Kétféle módon lehet támadni ezt, vagy:
    1. Legyen iteratív . Ez azt jelenti, használat for/ whilekörök helyett funkciók nevezik magukat - ez lehetővé tenné, hogy annyi csomópontok, mint a gép memóriája képes kezelni (megoldja a problémát).
    2. Frissítés add_name_to_treekezelni betoldások egy kiegyensúlyozott bináris fa (de ez csak segít a kérdés, a stack limit még mindig ott van).
  2. Nem tudja kezelni a két ember pontosan ugyanaz a neve, de más id után - az első, aki bekerül a fa, az összes többi ember ugyanazt a nevet nem veszi figyelembe.

Fogom hagyni, hogy ezt a gyakorlat, hogy kezelni ezeket a helyzeteket.


#include <stdio.h>
#include <string.h>

/* a single struct type for storing all tree elements */
typedef struct _node
{
    char name[50];
    int id;
    struct _node *subname;
    struct _node *left;
    struct _node *right;
} node;

/* creates a new node structure for the specified name and id */
node *create_node(const char *name, int id)
{
    node *newNode = (node*)malloc(sizeof(node));
    memset(newNode, 0, sizeof(*newNode));

    newNode->id = id;
    strncpy(newNode->name, name, sizeof(newNode->name));

    return newNode;
}

/* inserts the name/id pair into the tree specified by root.
   note that root is passed as a pointer to a pointer, so that
   it can accept NULL if no tree exists yet, and return to the 
   caller the node the node that contains the name.  Note that
   id is ignored if "name" already exists, i'll leave it as an
   excersice for you to handle situations with the same name
   with multiple id's */
node *add_name_to_tree(node **root, const char *name, int id)
{
    if (*root == NULL)
    {
        *root = create_node(name, id);
        return *root;
    }

    const int cmp = strcmp(name, (*root)->name);

    if (cmp < 0)
    {
        return add_name_to_tree(&(*root)->left, name, id);
    }
    else if (cmp > 0)
    {
        return add_name_to_tree(&(*root)->right, name, id);
    }
    else
    {
        return *root;
    }
}

/* adds the specified first/last name and id combo to the tree
   specified by root */
node *add_name(node *root, const char *first, const char *last, int id)
{
    /* this call will return the node that holds the last name,
       we can then use its "subname" tree root to insert the first name */
    node *last_node = add_name_to_tree(&root, last, 0);

    /* use the "subname" of the node that stores the last name as the 
       root of the tree that stores first names */
    add_name_to_tree(&last_node->subname, first, id);
    return root;
}

/* just to demonstrate why I use the same node type for first/last names,
   its because it allows you to support any number of names, see
   below - an add function that adds people with a middle name to the tree
   */
node *add_with_middle_name(node *root, const char *first, 
                           const char *middle, const char *last, int id)
{
    node *last_node = add_name_to_tree(&root, last, 0);
    node *mid_node = add_name_to_tree(&last_node->subname, middle, 0);
    add_name_to_tree(&mid_node->subname, first, id);
    return root;
}

/* recursively traverse the name tree, printing out the names */
void print_names(node *names, int level)
{
    const int indent = 10;

    if (names == NULL)
    {
        printf("\n");
    }

    if (names->left)
    {
        print_names(names->left, level);
    }

    if (names->subname)
    {
        printf("%*c %s \n", (indent * level), ' ', names->name);
        print_names(names->subname, level + 1);
        printf("\n");
    }
    else
    {
        printf("%*c %-*s %d\n", 
               (indent * level), ' ', 
               indent, names->name, names->id);
    }

    if (names->right)
    {
        print_names(names->right, level);
    }
}

int main()
{
    node *names = NULL;

    names = add_name(names, "Sylvester", "Stallone", 11111111);
    names = add_name(names, "Noah", "Stallone", 22222222);
    names = add_name(names, "Chuck", "Norris", 33333333);
    names = add_name(names, "Hulk", "Hogan", 44444444);
    names = add_name(names, "Daniel", "Hogan", 55555555);

    names = add_with_middle_name(names, "Peter", "Michael", 
                                 "Zachson", 66666666);

    print_names(names, 0);

    return 0;
}
Válaszolt 29/05/2011 23:46
a forrás felhasználó

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more