db.Reviews.drop();
db.Reviews.insertMany([
{_id:1, rating:5, comment: "overall not bad"},
{_id:2, rating:2, comment: "overall not bad"},
{_id:3, rating:3, comment: "overall not bad"},
{_id:4, rating:5, comment: "overall not bad"},
{_id:5, rating:4, comment: "overall not bad"},
{_id:6, rating:5, comment: "overall not bad"}
]);
db.ProductRefModel.drop();
db.ProductRefModel.insertMany([
{
ProductId:1,
Description: "Bottle",
ReviewIds: [ 1, 2 ]
},
{
ProductId: 2,
Description: "Air Pump",
ReviewIds: [3,4]
}
]);
db.ProductRefModel.find();
db.ProductRefModel.aggregate([
{
$lookup: {
from: "Reviews",
localField: "ReviewIds",
foreignField: "_id",
as: "Rating Details"
}
}
]
);
db.Reviews.aggregate([
{
$lookup: {
from: "ProductRefModel",
localField: "_id",
foreignField: "ReviewIds",
as: "Product Details"
}
}
]
);
Aug 06
MongoDB: Lookup and Aggregate: Reference Model
Jul 28
Binary Tree Traversal using In Order: In C
// 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 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/
}