https://youtu.be/kyXrVVu_00Q
Jul 20
C Code Build a Binary Tree.
#include <iostream>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
#define MaxWordSize 100
// Declare a structure that holds data in a node
typedef struct {
char word[MaxWordSize + 1];
} NodeData;
typedef struct treeNode {
NodeData data;
struct treeNode* left, * right;
} TreeNode, *TreeNodePtr;
typedef struct {
TreeNodePtr root;
} BinaryTree;
TreeNodePtr buildTree(FILE *in) {
char str[MaxWordSize + 1];
//read one word from the file
fscanf_s (in, "%s", str);
//if we see @ then return null (end of current recursion)
if (strcmp (str, "@") == 0) return NULL;
//allocate space for a TreeNode
TreeNodePtr p = (TreeNodePtr) malloc (sizeof(TreeNode));
// copy the data to the tree node
strcpy_s (p->data.word, str);
// build the left sub tree using recursion
p->left = buildTree(in);
// build the right sub tree using recursion
p->right = buildTree(in);
return p;
} //end buildTree
// traverse the tree in in-order basis
// print the treee content in in-order basis
void inOrder(TreeNodePtr node) {
//printf("%s ", node->data.word);
if (node != NULL) {
inOrder(node->left);
printf("%s ", node->data.word);
inOrder(node->right);
}
} //end inOrder
// main method
int main(){
// declare a binary tree
BinaryTree bt;
//open the file where tree data are kept
FILE *in = fopen("btree.in", "r");
// build the tree
bt.root = buildTree(in);
inOrder(bt.root);
}
Jul 20
A Binary Tree Declaration
#include <stdio.h>
#include <stdlib.h>
// Declare a structure that holds data in a node
typedef struct {
int num;
} NodeData;
typedef struct treeNode {
NodeData data;
struct treeNode* left, * right;
} TreeNode, *TreeNodePtr;
typedef struct {
TreeNodePtr root;
} BinaryTree;
// main method
int main()
{
// NULL Pointing binary tree
BinaryTree bt1;
bt1.root = NULL;
//Binary tree with one Root Node
TreeNodePtr p = (TreeNodePtr) malloc(sizeof(TreeNode));
p->data.num = 5000;
p->left = NULL;
p->right = NULL;
bt1.root = p;
//pointers in C
//https://www.geeksforgeeks.org/null-pointer-in-c/
}
Jul 20
Struct and Tree Node Examples
#include <iostream>
#include <string.h>
#pragma warning(disable : 4996)
#define MaxWordSize 100
// Declare a structure that holds data in a node
typedef struct {
int num;
} NodeDataInt;
// Declare a structure that holds data in a node
typedef struct {
char word[MaxWordSize + 1];
int freq;
} NodeDataChar;
// define what a node will look like
typedef struct treenodeInt {
NodeDataInt data;
struct treenode* left, * right;
} t2;
// define what a node will look like
typedef struct treenodeChar {
NodeDataChar data;
struct treenode *left, *right;
} t3;
// main method
int main()
{
// Node with integer data
NodeDataInt aNode;
aNode.num = 100;
std::cout << "aNode.num: " << aNode.num << "\n";
// Node with character data
NodeDataChar aNodeChar;
strcpy(aNodeChar.word, "Hello Word");
std::cout << "aNodeChar.word: " << aNodeChar.word << "\n";
//syntax error
//t2.data.num = 1000;
// example using tree node with int data
t2 aTNode;
aTNode.data.num = 1000;
std::cout << "aTNode.data.num: " << aTNode.data.num << "\n";
// example using tree node with char data
t3 aTNodeChar;
strcpy(aTNodeChar.data.word, "1000 Hello Node Char Data");
std::cout << "aTNodeChar.data.word: " << aTNodeChar.data.word << "\n";
}
Jul 20
Struct and Tree Node
// 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/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);
}
