Build a Number Hash in C

Create a Hash Table to store numbers. Also, search numbers in that Hash Table. Write Code in C

define _CRT_SECURE_NO_WARNINGS

include

include

include

define MaxNumbers 50

// to divide with

define N 100

define Empty 0

define STORAGESIZE 100

int hashTable[STORAGESIZE + 1];

//initialize hash table
void initializeHashTable() {

for (int counter = 0; counter < STORAGESIZE; counter++) {
    hashTable[counter] = 0;
}

}

//find a number and return found/not-found
int searchHashTable(int numberToFind) {

int hashSearchPosition = numberToFind % N + 1;

if (hashTable[hashSearchPosition] == numberToFind) {
    printf("%d %s %d", numberToFind, " Found at ", hashSearchPosition );
    return 1;
}
else {

    for (int pos = hashSearchPosition + 1; pos < STORAGESIZE; pos++) {
        if (hashTable[pos] == numberToFind) {
            printf("%d %s %d", numberToFind, " Found at ", pos);
            return 1;

        }
    }

}

printf("Number Not Found");
return -1;

}

//find next position after a collission
int findNextAvailablePosition(int index) {

for (int pos = index + 1; pos < STORAGESIZE; pos++) {
    if (hashTable[pos] == 0) {
        return pos;
    }
}

return -1;

}

//insert data into hash table
int insertToHashTable(int l_numberToInsert) {

int hashPosition = l_numberToInsert % N + 1;

if ( hashTable[hashPosition] > 0) {
    //collision
    //so find the next position
    hashPosition = findNextAvailablePosition(hashPosition);
}

//error finding a position 
if (  (hashPosition >= STORAGESIZE) || ((hashPosition == -1)) ) {
    // indicates error
    return -1;
}

hashTable[hashPosition] = l_numberToInsert;

// 1 indicates success
return 1;

}

//flow of the work
int main() {

//initialize
initializeHashTable();

//read data from file
FILE *in = fopen("input.txt", "r");

int numberToInsert = 0; 

//convertWordToNumber()

//insertion
//insert data into the hash table
while ( fscanf(in, "%d", &numberToInsert) == 1 ) {
    insertToHashTable(numberToInsert);
}


//take input from user to search
printf("\n%s\n", "Give a number to search");
int numberToSearch = 56;
scanf("%d", &numberToSearch);

//find the number in the hash
searchHashTable(numberToSearch);

}

Build a Word Hash

Store Words in a Hash Table. Also, search a word in that Hash Table

define _CRT_SECURE_NO_WARNINGS

include

include

include

//#define MaxNumbers 100

define MaxWordLen 50

define Empty 0

define STORAGESIZE 15

// to divide with

define N 15

char hashTable[STORAGESIZE][MaxWordLen];

//initialize hash table
void initializeHashTable() {
for (int counter = 0; counter < STORAGESIZE; counter++) {
strcpy(hashTable[counter], “”);
}
}

// from Noel Kalicharan
// Advanced topics in C
int convertWordToNumber(char wordToInsert[]) {
/*
intj, wordNum = 0;
intweight = 3;
while (word[j] != ‘\0’) {
wordNum += weight * word[j++];
weight += 2;
}
location = wordNum % n + 1;

return location;

*/

int j = 0, wordNum = 0;
int weight = 3; 

while (wordToInsert[j] != '\0') {
    wordNum += weight * wordToInsert[j++];
    weight += 2;
}

return wordNum;

}

//find next position after a collission
int findSearchedWordPosition(int hashSearchPosition, char l_wordToSearch[]) {

int pos;
for (pos = hashSearchPosition + 1; pos < STORAGESIZE; pos++) {
    //if (hashTable[pos] == numberToFind) {
    if (hashTable[pos] && strcmp(hashTable[pos], l_wordToSearch) == 0) {
        printf("%s %s %d", l_wordToSearch, " Found at ", pos);
        return pos;
    }
}

if (pos >= STORAGESIZE) {
    for (int pos = 0; pos < hashSearchPosition; pos++) {
        if (hashTable[pos] && strcmp(hashTable[pos], l_wordToSearch) == 0) {
            printf("%s %s %d", l_wordToSearch, " Found at ", pos);
            return pos;

        }
    }
}


return -1;

}

//find a number and return found/not-found
int searchHashTable(char l_wordToSearch[]) {

int numberForTheWord = convertWordToNumber(l_wordToSearch);
int hashSearchPosition = numberForTheWord % N + 1;

if ( hashTable[hashSearchPosition] && (strcmp(hashTable[hashSearchPosition], l_wordToSearch) == 0)   ) {
    printf("%s %s %d", l_wordToSearch, " Found at ", hashSearchPosition);
    return 1;
}
else {

    int wordFoundPosition = findSearchedWordPosition(hashSearchPosition, l_wordToSearch);
    if (wordFoundPosition > 0) {
        printf("%s %s %d", l_wordToSearch, " Found at ", wordFoundPosition);
        return 1;
    }



}

printf("Word Not Found");
return -1;

}

//find next position after a collission
int findNextAvailablePosition(int index) {

int pos;
for (pos = index + 1; pos < STORAGESIZE; pos++ ) {
    //if (hashTable[pos] == 0) {
    if (strcmp(hashTable[pos], "") == 0){
        return pos;
    }
}

if (pos >= STORAGESIZE) {
    for (int pos = 0; pos < index; pos++) {
        //if (hashTable[pos] == 0) {
        if (strcmp(hashTable[pos], "") == 0) {
            return pos;
        }
    }
}


return -1;

}

//insert data into hash table
int insertToHashTable(int l_position_to_insert, char wordToInsert[]) {

//find position to insert
//remainder
int hashPosition = l_position_to_insert % N + 1;

char word[MaxWordLen];
//strcpy(word, hashTable[hashPosition]);

if (  strcmp(hashTable[hashPosition], "") != 0) {
    //collision
    //so find the next position
    hashPosition = findNextAvailablePosition(hashPosition);
}

//error finding a position 
if ((hashPosition > STORAGESIZE) || ((hashPosition == -1))) {
    // indicates error
    printf("\n%s %s\n", "Did not find a Position for ", wordToInsert);
    return -1;
}

strcpy(hashTable[hashPosition], wordToInsert);

// 1 indicates success
return 1;

}

//flow of the work
int main() {

//initialize
initializeHashTable();

//read data from file
FILE *in = fopen("input.txt", "r");

char wordToInsert[MaxWordLen];


//insertion
//insert data into the hash table
while (fscanf(in, "%s", &wordToInsert) == 1) {
    int positionToInsert = convertWordToNumber(wordToInsert);
    insertToHashTable(positionToInsert, wordToInsert);
}

//take input from user to search
//int numberToSearch;
char wordToSearch[MaxWordLen];
printf("\nPlease type the word to search\n");
scanf("%s", wordToSearch);

//find the number in the hash
searchHashTable(wordToSearch);

}

Input: Tree Traversal Data Output: Build the Tree

Problem:

Ref: Book on Data Structure and Algorithm in C

•From In-order Output to Build the Tree

•Take In-order Traversal Output Data

•And Build the Tree

•Take the middle (N) or so as the root

•Keep going/taking alternate left (nodeLabels) (K D) from there

•Make those also roots/parents (left sub tree)

•The last may be at the last left in this flow

•Then from the last (left last of data)

•Take alternate nodeLabels (P, E) and make them the right child (in left side tree)

•From middle Keep going/taking alternate right node labels (A L)

•Make center/parent/root (right subtree)

•From the end reverse back with the alternate nodes (T F)

•Make these as left children

From Pre-order Tree Traversal Output to Build the Tree
First one becomes the root such as N
Some immediate ones (D G) also becomes parents up until middle (you can choose) – left sub tree parens

Then put some immediate ones (K P) as right children (in left subtree) to come to root

Then take alternate (E F L) to have the parents on the right (Goes Right, right subtree)

come back from right, take remaining (A, T), make them left children (bottom to up direction)

•Input: Post order Tree Traversal data, Output: Tree

•Last one (E) becomes the root

•Some immediate (backward) right ones ones (L A T) up until middle (you can choose)  also becomes parents (downward right side parent)

•– Then put some immediate ones (backward) ( N F ) as left  children to come to root

Then take alternate (K P) to have the parents on the left side of the tree

•Come back from the last, with the remaining ones (G D)

•and make right children (left sub tree)

Input: Post order Tree Traversal data, Output: Tree

• Last one (E) becomes the root

• Some immediate (backward) right ones ones (L A T) up until middle (you can choose)  also becomes parents (downward right side parent)

• – Then put some immediate ones (backward) ( N F ) as left  children to come to root

Then take alternate (K P) to have the parents on the left side of the tree

• Come back from the last, with the remaining ones (G D)

• and make right children (left sub tree)

From Pre-order Tree Traversal Output to Build the Tree


From Pre-order Tree Traversal Output to Build the Tree
First one becomes the root such as N
Some immediate ones (D G) also becomes parents up until middle (you can choose) – left sub tree parens

Then put some immediate ones (K P) as right children (in left subtree) to come to root

Then take alternate (E F L) to have the parents on the right (Goes Right, right subtree)

come back from right, take remaining (A, T), make them left children (bottom to up direction)

Build the Tree from Tree Traversal Output

From In-order Output to Build the Tree

•Take In-order Traversal Output Data

•And Build the Tree

•Take the middle (N) or so as the root

•Keep going/taking alternate left (nodeLabels) (K D) from there

•Make those also roots/parents (left sub tree)

•The last may be at the last left in this flow

•Then from the last (left last of data)

•Take alternate nodeLabels (P, E) and make them the right child (in left side tree)

•From middle Keep going/taking alternate right node labels (A L)

•Make center/parent/root (right subtree)

•From the end reverse back with the alternate nodes (T F)

•Make these as left children

Laravel Artisan Overview, Commands and Examples

https://youtu.be/qy5xHEmeeTw

Kolb’s Experiential Learnng Model:কোলবের এক্সপেরিয়েনশিয়াল লার্নিং মডেল

https://youtu.be/O7kq9kL36t4

Keyboard Shortcuts for Visual Studio

https://youtu.be/qedaBquEChg

key concepts for system design

https://youtu.be/hvIyfbfYUq0