// Ref: typedef and struct
// https://www.w3resource.com/c-programming-exercises/c-snippets/difference-between-typedef-struct-and-struct-definitions-with-example.php#google_vignette
// https://www.tutorialspoint.com/cprogramming/c_pointers.htm
// https://www.geeksforgeeks.org/typedef-in-c/
#pragma warning(disable : 4996)
#include <iostream>
#include <string.h>
// Declare a structure that holds data in a node
typedef struct {
int num;
} NodeData;
// define what a node will look like
typedef struct treenode {
NodeData data;
struct treenode *left, *right;
} t1;
// main method
int main()
{
std::cout << "Hello World 2!\n";
}
Jul 20
Struct and Tree Node
Jul 20
Struct/Record Examples in C Programming Language
#pragma warning(disable : 4996)
#include <iostream>
#include <string.h>
//struct
struct student {
char name[50];
int age;
float height;
};
struct student s1 = { " Nina Chase", 12, 1.55 };
struct student s2 = { " Shyann Morris", 12, 1.65 };
//typedef struct
typedef struct {
char name[50];
int age;
float height;
} studentType;
studentType s11 = { " Nina Chase", 12, 1.55 };
studentType s22 = { " Shyann Morris", 12, 1.65 };
//typedef struct
typedef struct studentTypeStart {
char name[50];
int age;
float height;
} studentTypeEnd;
studentTypeEnd s33 = { " Shyann Morris", 12, 1.65 };
struct studentTypeStart s44 = { " struct studentTypeStart Shyann Morris", 12, 1.65 };
studentTypeStart s55 = { " Shyann Morris studentTypeStart", 12, 1.65 };
studentTypeEnd *stuPtr;
// main method
int main()
{
std::cout << "Hello World 2!\n";
std::cout << s11.name << "\n";
std::cout << s33.name << "\n";
std::cout << s55.name << "\n";
std::cout << s44.name << "\n";
}
Jul 20
Print an Adjacency List Graph
// print the graph
void printGraph(Graph G) {
for (int j = 1; j <= G->numV; j++) {
printf("%s: ", G->vertex[j].id);
GEdgePtr p = G->vertex[j].firstEdge;
while (p != NULL) {
printf("%s %d ", G->vertex[p->child].id, p->weight);
p = p->nextEdge;
}
printf("\n");
}
}
Jul 20
Build an Adjacency List Graph from Node Labels in a File
//build the graph
void buildGraph(FILE* in, Graph G) {
int j, k, numEdges, weight;
char name[MaxWordSize], nodeID[MaxWordSize], adjID[MaxWordSize];
// read the names of the vertices
// and store them in the graph array
for (j = 1; j <= G->numV; j++) {
fscanf(in, "%s", name);
G->vertex[j] = newGVertex(name);
strcpy(G->vertex[j].id, name);
}
// process edge data for each vertex
for (j = 1; j <= G->numV; j++) {
// information about the parent vertex
fscanf(in, "%s %d", nodeID, &numEdges);
// information about each edge from the parent vertex
for (k = 1; k <= numEdges; k++) {
fscanf(in, "%s %d", adjID, &weight);
addEdge(nodeID, adjID, weight, G);
}
}
}
Jul 20
Nodes: Vertices and Edges. Nodes for them
define MaxWordSize 100
#define MaxWordSize 100
//edge of a graph
typedef struct gEdge{
// child is the location of the child vertex
int child, weight;
struct gEdge *nextEdge;
} GEdge, *GEdgePtr;
typedef struct{
char id[MaxWordSize];
int parent, cost, discover, finish, inDegree;
GEdgePtr firstEdge;
} GVertex;
//main method
int main() {
return 0;
}
Jul 20
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);
}
Jul 20
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);
}
Jul 19
Input: Tree Traversal Data Output: Build the Tree
•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)
Jul 19
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)
Jul 19
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)