简体   繁体   中英

Hashtable & BST Implementation

I'm working on an assignment that can accept commands from keyboard to insert people into a hashtable. After someone is inserted into the hastable, they can be "friended" with another person in the table. The way I have to store who is friends with who is a binary search tree. What I have to do is for the hashtable the first part of the node would be the person's name, then then next is a pointer to the bst for that person's friends, and finally the end is a pointer to the next node for chaining if there is a collision. Here is a visual example... 在此处输入图片说明

I have been able to insert people into my table, but the problem that I can not figure out is how to access the BST and add in friends for that person. Here is my code...

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

// Structures
struct linkedList{
    char *name;
    struct linkedList *next;
    struct linkedList *tree;
};

typedef struct linkedList list;

struct hashTable{
    int size;
    list **table;
};

typedef struct hashTable hash;

struct bst{
    char *val;
    struct bst *l;
    struct bst *r;
};

int main(){
char input[50];
char *ch, cmd_str[50], name[30];

// Make hash table for names
hash *profiles;
profiles = createHashTable(1001);

while(1){
    // Get keyboard input
    fgets(input, 50, stdin);
    input[strlen(input)-1] = '\0';

    // parse the input
    ch = strtok(input, " ");
    strcpy(cmd_str,ch);



    if(strcmp("CREATE", cmd_str) == 0){
        ch = strtok(NULL, " \n");
        insertPerson(profiles, ch);
    }
    else if(strcmp("FRIEND", cmd_str) == 0){
        ch = strtok(NULL, " \n");
        strcpy(name, ch);
        ch = strtok(NULL, " \n");
        friendPerson(profiles, name, ch);
    }
    else if(strcmp("UNFRIEND", cmd_str) == 0){
        ch = strtok(NULL, " \n");

    }
    else if(strcmp("LIST", cmd_str) == 0){
        ch = strtok(NULL, " \n");
        printFriends(profiles, ch);
    }
    else if(strcmp("QUERY", cmd_str) == 0){

    }
    else if(strcmp("BIGGEST-FRIEND-CIRCLE", cmd_str) == 0){

    }
    else if(strcmp("INFLUENTIAL-FRIEND", cmd_str) == 0){

    }
    else if(strcmp("EXIT", cmd_str) == 0){
        printf("\nExiting...\n");
        return 0;
    }
    else{
        printf("\nBad Command.\n");
    }
}
}

// Creates Hash Table
hash *createHashTable(int size){
int i;
hash *new_table;

if((new_table = malloc(sizeof(hash))) == NULL)
    return NULL;

if((new_table->table = malloc(sizeof(list *) * size)) == NULL)
    return NULL;

for(i=0; i < size; i++)
    new_table->table[i] = NULL;

new_table->size = size;

return new_table;
}

// hashing function
int keyHash(char *name){
    int c;
    unsigned long key;

    while(c = *name++)
        key = ((key<<5) + key) + c;

    return key%1000;
 }

// insert a person into the hash table
void insertPerson(hash *profiles, char *name){
    struct linkedList *item = (struct linkedList*)malloc(sizeof(struct linkedList));
    int hash_val = keyHash(name);

    item->name = name;
    item->next = NULL;
    item->tree = new_tree;

    // Collision case
    if(profiles->table[hash_val] != NULL){
        while(profiles->table[hash_val]->next != NULL){
            profiles->table[hash_val] = profiles->table[hash_val]->next;
        }
        profiles->table[hash_val]->next = item;
    }
    // Empty cell
    else{
        profiles->table[hash_val] = item;
    }

}

// friend two people inside the hash table
void friendPerson(hash *profiles, char *name, char *_friend){
    int hash1 = keyHash(name);
    int hash2 = keyHash(_friend);

    // check if the names are already in system
    if(!profiles->table[hash1]){
        printf("%s is not yet in the system", name);
        return;
    }
    if(!profiles->table[hash2]){
        printf("%s is not yet in the system", _friend);
        return;
    }

    // add first friend
    if(strcmp(profiles->table[hash1]->name, name) == 0){
        insertBST(profiles->table[hash1]->tree, _friend);
    }
    else{
        while(profiles->table[hash1]->next != NULL){
            if(strcmp(profiles->table[hash1]->name, name) == 0)){
                break;
            }
            profiles->table[hash1] = profiles->table[hash1]->next;
        }
        insertBST(profiles->table[hash1]->tree, _friend);
    }

    // add second friend
    if(strcmp(profiles->table[hash2]->name, _friend) == 0){
        insertBST(profiles->table[hash2]->tree, name);
    }
    else{
        while(profiles->table[hash2]->next != NULL){
            if(strcmp(profiles->table[hash2]->name, name) == 0)){
                break;
            }
            profiles->table[hash2] = profiles->table[hash1]->next;
        }
        insertBST(profiles->table[hash2]->tree, name);
    }
}

// creates a new bst node
struct bst *newBSTNode(char *name){
    struct bst *temp = (struct bst* )malloc(sizeof(struct bst));
    temp->val = strdup(name);
    strcpy(temp->val, name);
    temp->l = temp->r = NULL;
    return temp;
}

// Inserts the a friend into a BST
struct bst *insertBST(struct bst *node, char *name){
    if(!node)
        return newBSTNode(name);
    else{
        if(strcmp(name, node->val) < 0){
            node->l = insertBST(node->l, name);
        }
        else if(strcmp(name, node->val) > 0){
            node->r = insertBST(node->r, name);
        }
    }

    return node;
}

// Inorder print of names
void inorder(struct bst *root){
    if(!root){
         inorder(root->l);
         printf("%s ", root->val);
         inorder(root->r);
    }
}

// Sends to function to print names
void printFriends(hash *profiles, char *name){
    int hash_val = keyHash(name);
    inorder(profiles->table[hash_val]->tree);
}

How would I be able to access the BST of said person? The struct bst *tree = profiles->table[hash1]->tree; was my previous attempt, but it was more of a shot in the dark. Thanks in advance!

Update: Okay, so I have been able to add friends (I think) and now I'm trying to print them with void printFriends() . However when I run the function nothing prints out. Would anyone know where I'm messing it up? I've updated the code above.

Your linkedList stores the name and a tree pointer. But the type of tree is linkedList . Change that to struct bst * . You'll have to reorder the declarations, or insert a forward declaration.

Implement a search. You check if the hash buckets are empty, but you don't search for the matching name.

Once you find a matching name in the bucket, the same node contains the aforementioned tree pointer. Insert the friend in the tree. Depending on your logic, you may or may not want to insert a backreference in the other person's tree. (If Alice is friends with Bob, does that make Bob friends with Alice automatically? Or wait for confirmation?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM