Created
November 15, 2015 06:04
-
-
Save aaronjwood/2780f31768691b1d69ed to your computer and use it in GitHub Desktop.
Binary search tree implementation in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <time.h> | |
typedef struct node Node; | |
struct node { | |
int value; | |
Node *left; | |
Node *right; | |
}; | |
Node* insert(Node *root, int v) { | |
if (root == NULL) { | |
root = malloc(sizeof (Node)); | |
root->value = v; | |
root->left = NULL; | |
root->right = NULL; | |
} else if (v < root->value) { | |
root->left = insert(root->left, v); | |
} else { | |
root->right = insert(root->right, v); | |
} | |
return root; | |
} | |
void traverse(Node *root) { | |
if (root == NULL) { | |
return; | |
} | |
traverse(root->left); | |
traverse(root->right); | |
} | |
int main(int argc, char** argv) { | |
Node *root = NULL; | |
const int SIZE = 2000000; | |
int *a = malloc(sizeof (int)*SIZE); | |
clock_t start, end; | |
printf("Generating random array with %d values...\n", SIZE); | |
start = clock(); | |
for (int i = 0; i < SIZE; i++) { | |
a[i] = rand() % 10000; | |
} | |
end = clock(); | |
printf("Done. Took %f seconds\n\n", (double) (end - start) / CLOCKS_PER_SEC); | |
printf("Filling the tree with %d nodes...\n", SIZE); | |
start = clock(); | |
for (int i = 0; i < SIZE; i++) { | |
root = insert(root, a[i]); | |
} | |
end = clock(); | |
free(a); | |
printf("Done. Took %f seconds\n\n", (double) (end - start) / CLOCKS_PER_SEC); | |
printf("Traversing all %d nodes in tree...\n", SIZE); | |
start = clock(); | |
traverse(root); | |
end = clock(); | |
free(root); | |
printf("Done. Took %f seconds\n\n", (double) (end - start) / CLOCKS_PER_SEC); | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`void freeForBtree(Node * root){
if(root==NULL)
{
return;
}
free(root);
else{
freeForBtree(root->left);
freeForBtree(root->right);
}
}'
this is the free routine I have write for your program, I hope it's not wrong,hhh