बाइनरी खोजें द्विआधारी खोज वृक्ष के अंदर पेड़

वोट
0

मैं एक होमवर्क जो मुझ से द्विआधारी खोज पेड़ की एक struct बनाने के लिए जहां द्विआधारी खोज वृक्ष के अपने नोड एक और द्विआधारी खोज वृक्ष है पूछना है। पहले BST छात्रों के उपनाम है और एक दूसरे को पहले के नाम और आईडी है। किसी अन्य छात्र मैं एक और उपनाम नोड तैयार नहीं करना चाहिए साथ ही उपनाम है, लेकिन मैं मौजूदा उपनाम नोड के अंदर एक और पहला नाम और पहचान नोड बनाने के लिए इसके अलावा, अगर। अधिक विशिष्ट होना:

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;

अगर मैं इन छात्रों है: मेरी मुख्य समस्या यह है हर firstname मैं निम्नलिखित कोड के साथ क्योंकि पाया के लिए एक अलग nameANDid नोड बनाने के लिए कैसे मैं कुलनाम के लिए 2 BST एक और एक अन्य नाम के लिए बनाने, लेकिन मैं उदाहरण के लिए की तरह बनना चाहते हैं

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

मैं उन्हें इस तरह संग्रहीत करना चाहते हैं: .........

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

इस के बजाय मैं कुछ की तरह ले: ...........

 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

मैं आदेश अधिक विशिष्ट में यहाँ कुछ कार्यों डाल देंगे

लोड समारोह एक txt दस्तावेज़ से नाम लोड करता है।

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);  
}

कहा पे

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

addnode समारोह है: ...

      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;
         }

और add_node_nameANDid () फ़ंक्शन पिछले समारोह की तरह है लेकिन यह कुछ चर बदल गया है:

      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;
    }

कि मैं सिर्फ अपलोड लेकिन यह इस बिना समझाने के लिए काफी मुश्किल होगा विशाल स्रोत कोड के लिए खेद है।

मुझे लगता है कि मैं दो समस्याएं हैं, लेकिन मैं ज्ञान उन्हें हल करने की जरूरत नहीं है।

सबसे पहले: मैं प्रत्येक उपनाम नोड के लिए अलग firstname BST बनाने के लिए और मुझे लगता है कि मैं ऐसा नहीं है कि लेकिन मुझे लगता है कि ऐसा करने के लिए पता नहीं कैसे ...

कोई सुझाव?

29/05/2011 को 12:22
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
2

मैं इनमें से एक उदाहरण दिया गया, समझाने के लिए कि कैसे मैं इस का दरवाजा खटखटाया टिप्पणी की दी है। आप जिस तरह से अपने कोड काम करता है को संशोधित करने के मेरे विचारों का उपयोग करने में सक्षम होना चाहिए। ध्यान दें कि इसके नहीं एक आदर्श कार्यान्वयन, मेरे सिर के ऊपर से, मैं निम्न समस्याओं देख सकते हैं।

  1. इसकी पुनरावर्ती है, जो पेड़ इसे संभाल कर सकते हैं की गहराई का मतलब लक्षित मशीन पर ढेर के आकार के द्वारा सीमित है। आप दो तरह से इस पर हमला कर सकते हैं, या तो कर रहे हैं:
    1. यह सुनिश्चित पुनरावृत्ति । यही कारण है, उपयोग for/ whileखुद को कार्यों के बजाय लूप - के रूप में अपने मशीनों स्मृति संभाल कर सकते हैं (इससे समस्या का समाधान) इस रूप में कई नोड्स के लिए अनुमति होगी।
    2. अद्यतन add_name_to_treeएक के लिए सम्मिलन को संभालने के लिए संतुलित द्विआधारी पेड़ (लेकिन यह सिर्फ मुद्दा मदद करता है, ढेर सीमा अभी भी वहाँ है)।
  2. यह बिल्कुल ही नाम के दो लोगों को नहीं संभाल सकता है, लेकिन अलग पहचान-पत्र - के बाद पहले व्यक्ति पेड़ में जोड़ा जाता है, एक ही नाम के बाद के सभी लोगों को अनदेखा किया जाता है।

आप इन स्थितियों को संभालने के लिए के लिए मैं एक व्यायाम के रूप में छोड़ देगा।


#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;
}
29/05/2011 को 23:46
का स्रोत उपयोगकर्ता

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