Skip to content

Commit

Permalink
Merge pull request #18 from giannhskp/add_comments
Browse files Browse the repository at this point in the history
Added comments on binary tree
  • Loading branch information
giannhskp committed Dec 17, 2021
2 parents ebfc43a + d9d8ce9 commit 4c03aed
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 72 deletions.
73 changes: 41 additions & 32 deletions BinaryTree/binaryTree.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@


typedef struct tree_n{
Vector v;
struct tree_n *left;
struct tree_n *right;
Vector v; // curve that is stored in the tree node
struct tree_n *left; // pointer to left child
struct tree_n *right; // pointer to right child
}tree_node;
typedef tree_node *TreeNode;


typedef struct tree_head_node{
int height;
int count;
TreeNode root;
int height; // height of the tree
int count; // how many curves where inserted at te tree creation
TreeNode root; // pointer to the root tree node
}treeHeadNode;
typedef treeHeadNode *Tree;
typedef treeHeadNode *Tree;

TreeNode getRoot(Tree tree){
return tree->root;
Expand All @@ -41,11 +41,11 @@ Vector getTnVector(TreeNode tn){
return tn->v;
}

TreeNode createTreeNode(Vector v){
TreeNode newNode = malloc(sizeof(tree_node));
newNode->v = copyVector(v);
newNode->left=NULL;
newNode->right=NULL;
TreeNode createTreeNode(Vector v){ // create a tree node
TreeNode newNode = malloc(sizeof(tree_node)); // allocate space for the node
newNode->v = copyVector(v); // copy the given curve and store it at the node
newNode->left=NULL; // assign left child to NULL
newNode->right=NULL; // assign left child to NULL
return newNode;
}

Expand All @@ -55,7 +55,7 @@ void deleteTreeNode(TreeNode tn,int freeVector){
free(tn);
}

Tree initializeTree(){
Tree initializeTree(){ // initialize an empty tree
Tree tree = malloc(sizeof(treeHeadNode));
tree->height=-1;
tree->count=0;
Expand All @@ -64,29 +64,33 @@ Tree initializeTree(){
}

TreeNode recursiveTreeCreateFromList(List *list,int height,int *leafCount){
if(height==0){
if((*list)==NULL) return NULL;
TreeNode tn = createTreeNode(getVector(*list));
(*list) = getNext((*list));
// recursively creates a tree that contains all the curves in the given list at it's leafs
if(height==0){ // if we reached at the leaf level
if((*list)==NULL) return NULL; // if all the curves of the list were assigned to a leaf, dont create a node
TreeNode tn = createTreeNode(getVector(*list)); // create a new node, and assing the curve to the node
(*list) = getNext((*list)); // got the next curve that will be assigned to a leaf
(*leafCount)++;
return tn;
}
TreeNode tn = createTreeNode(NULL);
tn->left = recursiveTreeCreateFromList(list,height-1,leafCount);
tn->right = recursiveTreeCreateFromList(list,height-1,leafCount);
// if we are at an inner node
// inner nodes are empty in the beggining, as the mean curves will be stored afterwards
TreeNode tn = createTreeNode(NULL); // create an empty node
tn->left = recursiveTreeCreateFromList(list,height-1,leafCount); // create left child at the next level, height is reduced by 1
tn->right = recursiveTreeCreateFromList(list,height-1,leafCount); // create right child at the next level, height is reduced by 1
return tn;
}


Tree createTreeFromList(List list,int count){
if(count==0) return NULL;
// create a tree that contains all the curves of the list at it's leaves
if(count==0) return NULL; // if list has no items
List tempList = list;
int height = ceil(log2(count));
Tree tree = initializeTree();
tree->height = height;
int height = ceil(log2(count)); // compute the height of the tree that is needed in order to store all the curves at the leaves
Tree tree = initializeTree(); // initialize an empty tree
tree->height = height; // store the height of the tree
int leafCount = 0;
tree->root = recursiveTreeCreateFromList(&tempList,height,&leafCount);
tree->count=leafCount;
tree->root = recursiveTreeCreateFromList(&tempList,height,&leafCount); // create all the tree nodes
tree->count=leafCount; // store the count of the leaves that contain a curve
return tree;
}

Expand Down Expand Up @@ -129,6 +133,7 @@ Tree createTreeFromHt(HashTable ht,int count){
}

void printTreeRecursive(TreeNode tn,int current_height){
// prints the tree | for debbuging reasons
if(tn==NULL) return;
if(current_height==0){
if(getTnVector(tn)==NULL) return;
Expand All @@ -141,6 +146,7 @@ void printTreeRecursive(TreeNode tn,int current_height){
}

void printTreeDFS(Tree tree){
// prints the tree | for debbuging reasons
int height = tree->height;
printf("------------------------- PRINT TREE -------------------------\n");
printf("ACTIVE LEAVES = %d\n",tree->count);
Expand All @@ -151,15 +157,17 @@ void printTreeDFS(Tree tree){
}

void destroyTreeRecursive(TreeNode tn){
// destroys the tree and free's all the allocated memory
if(tn==NULL) return;
destroyTreeRecursive(getLeft(tn));
destroyTreeRecursive(getRight(tn));
if((getTnVector(tn)!=NULL))
deleteVector(getTnVector(tn));
free(tn);
destroyTreeRecursive(getLeft(tn)); // delete left child
destroyTreeRecursive(getRight(tn)); // delete right child
if((getTnVector(tn)!=NULL)) // if it contains a curve
deleteVector(getTnVector(tn)); // delete the curve
free(tn); // delete the node
}

void destroyTree(Tree tree){
// destroys the tree and free's all the allocated memory
destroyTreeRecursive(getRoot(tree));
free(tree);
}
Expand Down Expand Up @@ -190,7 +198,8 @@ void computeMeanCurvesRecursive(TreeNode tn){
}

Vector treeFindMeanCurve(Tree tree){
if(tree==NULL) {printf("TREE EMPTY!\n"); return NULL; }
// called by the cluster function in order to compute the mean curve of the curves that are inserted at the leaf nodes of the given tree
if(tree==NULL) { return NULL; }
computeMeanCurvesRecursive(getRoot(tree));
return copyVector(getTnVector(getRoot(tree)));
}
11 changes: 8 additions & 3 deletions Clustering/clustering.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ void lloyds(Vector* clusters,Vector *oldClusters,Vector* vectors,List* clustersL
}else{
newCenter=listMeanOfCluster(clustersList[i],dim);
}
if(newCenter==NULL){
// this cluster hasn't been formed, let as centroid the previous one
newCenter=copyVector(oldClusters[i]);
}
}
else{
// this cluster hasn't been formed, let as centroid the previous one
Expand Down Expand Up @@ -131,7 +135,6 @@ void clusteringLloyds(List vecList,int numOfClusters,FILE* fptr,int dim){
if(count==MAX_RECENTER_ITERATIONS)
break;
count++;
printf("Iteration %d\n",count);
if(!firstIter){
Vector *temp = oldClusters;
oldClusters=clusters;
Expand Down Expand Up @@ -246,7 +249,6 @@ void reverseAssignmentLSH(LSH lsh,Vector *vectors,Vector *clusters,Vector *oldCl
radius*=2; // doubled the radius for the next range search
loopCounter++;
}
printf("ITERATION %d | ASSIGNED = %d\n",iteration,assignCounter);
int remainderCounter = 0;
// finally one big percentage of vectors has been assigned into clusters
// the remaining vectors will be assigned based on the nearest centroid at the corresponding cluster
Expand Down Expand Up @@ -324,6 +326,9 @@ void clusteringLSH(List vecList,int numOfClusters,int l,FILE* fptr,int dim,int m
grids = NULL;
}else if(method == METHOD_FRECHET){
grids = initializeGrids(delta,l,2);
}else{
// never actually happens
grids = NULL;
}
for(int i=0;i<numOfVecs;i++){
initializeClusterInfo(vectors[i]);
Expand Down Expand Up @@ -415,7 +420,7 @@ void clusteringLSH(List vecList,int numOfClusters,int l,FILE* fptr,int dim,int m
}
if (grids!=NULL)
deleteGrids(grids,2);

free(props);
free(vectors);
free(oldClusters);
Expand Down
1 change: 1 addition & 0 deletions LSH/lsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ Vector filterMeanCurve(Vector v,int finalDim){
// which increases each loop as it multiplies by 5 (the initial value of epsilon is 0.001).
// so if |a-b|<=epsilon AND |b-c|<=epsilon then the corresponding b point will be removed.
// Finally, function returns the mean curve that comes up from this process.
if(v==NULL){return NULL;} // never actually happens
int dim = getDim(v);
if(dim<=finalDim){
return copyVector(v);
Expand Down
37 changes: 2 additions & 35 deletions mainCube.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,6 @@ int m;
int probes;
int w;

// static int wValueCalculation(List list,int numberOfVectorsInFile,int dim){
// long double sumDist = 0.0;
// int count=0;
// double persentageToCheck;
// if(numberOfVectorsInFile<=1000){
// persentageToCheck = 0.1;
// }else if(numberOfVectorsInFile<=10000){
// persentageToCheck = 0.001;
// }else if (numberOfVectorsInFile<=100000){
// persentageToCheck = 0.0001;
// }else{
// persentageToCheck = 0.000001;
// }
// int stopBound = persentageToCheck*numberOfVectorsInFile*numberOfVectorsInFile;
// while(list!=NULL){
// List nested = list;
// while(nested!=NULL){
// if(count>stopBound){
// return floor(sumDist/count);
// }
// sumDist += distance_metric(getVector(list),getVector(nested));
// count++;
// nested = getNext(nested);
// }
// list=getNext(list);
// }
// return floor(sumDist/count);
// }



void vectorTimeSeriesHypecube(char* arg_inputFile,char* arg_queryFile,int arg_new_dimension,int arg_M,int arg_probes,char* arg_outputFile,int distanceTrueOff) {
srand(time(NULL));
Expand Down Expand Up @@ -109,12 +79,9 @@ void vectorTimeSeriesHypecube(char* arg_inputFile,char* arg_queryFile,int arg_ne
printf("Number of vectors in input file: %d\n",numberOfVectorsInFile);

printf("Finding optimal value of w based on the input file\n");
// begin = clock();
// w = wValueCalculation(list,numberOfVectorsInFile,dim);
// w /= W_DIVIDER;

w=600;
// end = clock();
// time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

printf("Found value of w = %d\n",w );

HyperCube hc;
Expand Down
4 changes: 2 additions & 2 deletions parsing/parsingLSH.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ void readQueryFileLSH_DiscreteFrechet(char* queryFile,char* outputFile,LSH lsh,L
total_true_time += time_spent_true;
query_count++;
}
fprintf(fptr, "tApproximateAverage: %f seconds\n",time_spent_lsh);
fprintf(fptr, "tTrueAverage: %f seconds\n",time_spent_true);
// fprintf(fptr, "tApproximateAverage: %f seconds\n",time_spent_lsh);
// fprintf(fptr, "tTrueAverage: %f seconds\n",time_spent_true);
fprintf(fptr, "\n");
fflush(fptr);
deleteVector(vecTmp);
Expand Down

0 comments on commit 4c03aed

Please sign in to comment.