簡體   English   中英

哈希表和BST實施

[英]Hashtable & BST Implementation

我正在做一個可以接受鍵盤命令將人插入哈希表的任務。 將某人插入hastable后,可以將他們與表中的其他人“成為朋友”。 我必須存儲誰與誰是二叉搜索樹的朋友的方式。 對於哈希表,我要做的是節點的第一部分是該人的名字,然后接下來是該人朋友的bst指針,最后是如果要鏈接,則指向下一個要鏈接的節點的指針是一次碰撞。 這是一個視覺示例... 在此處輸入圖片說明

我已經可以在表格中插入人,但是我不知道的問題是如何訪問BST並為該人添加朋友。 這是我的代碼...

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

我將如何訪問該人的BST? struct bst *tree = profiles->table[hash1]->tree; 是我以前的嘗試,但更多的是在黑暗中進行拍攝。 提前致謝!

更新:好的,所以我已經可以添加朋友了(我認為),現在我嘗試使用void printFriends()打印他們。 但是,當我運行該功能時,不會打印任何內容。 有人知道我在哪里搞砸嗎? 我已經更新了上面的代碼。

您的linkedList存儲nametree指針。 但是tree的類型是linkedList 將其更改為struct bst * 您必須重新排列聲明的順序,或插入前向聲明。

實施搜索。 您檢查哈希存儲桶是否為空,但不搜索匹配的名稱。

在存儲桶中找到匹配的名稱后,同一節點將包含上述tree指針。 將朋友插入樹中。 根據您的邏輯,您可能會或可能不想在其他人的樹中插入反向引用。 (如果Alice是Bob的朋友,這會自動使Bob與Alice成為朋友嗎?還是等待確認?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM