// traverse the tree in in-order basis
// print the treee content in in-order basis
void inOrder(NodePtr node) {
if (node != NULL) {
inOrder(node->left);
printf("%s ", node->data.word);
inOrder(node->right);
}
}
Jul 28
Binary Tree Traversal using In Order: In C
Jul 28
Binary Tree Traversal in Post Order: In C
// traverse the tree in post-order basis
// print the treee content in post-order basis
void postOrder(NodePtr node) {
if (node != NULL) {
postOrder(node->left);
postOrder(node->right);
printf("%s ", node->data.word);
}
}
Jul 28
Pre Order Traversal of a Binary Tree in C
// pre order traversal
void preOrder(NodePtr node) {
if (node != NULL) {
printf(“%s “, node->data.word);
preOrder(node->left);
preOrder(node->right);
}
}
Jul 28
Steps to Create a Binary Search Tree
If binary tree is empty, create a node, and assign data, point to it
Otherwise : point to root node
Compare the new – data – to – insert with the current node data
We maintain a pointer say current to point to the node under visit
If data matched : nothing to do
If data does not match :
keep traversing left if new data is smaller than current node data(until match or reach leaf / null).
To keep traversing to the left : current = current->left
Then create a new node, and point the left of curr node to this new node
keep traversing right if new data is bigger than current node data(until match or reach leaf / null).
To keep traversing to the right : current = current->right
Then create a new node, and point the right of current node to this new node
Jul 28
Steps to Search in a Hash Table Built Using Graph: Chaining
•Search the Hash
•Read a number/word
•From the user
•Find the position for this number/word
•in the graph (vertical array of nodes)
•By dividing with N or similar
•Check if any vertex node is there
•In that array position (array of graph vertices)
•If no vertex is there
•The word is not in the hash
•If a vertex is there
•Match with the vertex label, if matched you found the word/number
•If no match with the vertex labels
•traverse through the edges
•Until you find a match
•How to Build
•Build the Hash
•Read a number/word from file
•Find the position for this number/word
•in the graph (vertical array of nodes)
•By dividing with N or similar
•Check if any vertex node is there
•In that array position (array of graph vertices)
•If no vertex is there
•Create a vertex node, assign the number/word to that node
•Insert into that position
•If a vertex is there
•If no edge so far, create an edge node, assign number/word
•Else: traverse through the edges
•Go to the end of the edge list
•Create an edge vertex, assign the number/word to that edge node
•
Jul 28
Steps to Build a Hash Table: Using Graph: Chaining
•Build the Hash
•Read a number/word from file
•Find the position for this number/word
•in the graph (vertical array of nodes)
•By dividing with N or similar
•Check if any vertex node is there
•In that array position (array of graph vertices)
•If no vertex is there
•Create a vertex node, assign the number/word to that node
•Insert into that position
•If a vertex is there
•If no edge so far, create an edge node, assign number/word
•Else: traverse through the edges
•Go to the end of the edge list
•Create an edge vertex, assign the number/word to that edge node
•
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";
}