I am trying to implement the AVL tree in C. I am storing a random integer and a random string at each node. If I manually give random integers as input and generate random strings using the function random_string_generator (in my code), then I get random strings. But if I generate both random integers and strings using functions rand_range and random_string_generator (written in my code), then I get same strings as output.
When I use random_string_generator function n times in a loop I get same string n times.
Can anyone please check my code?
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
time_t t;
struct node {
int val;
char *str;
struct node *left_child;
struct node *right_child;
};
struct node* LL_rotation(struct node *root) {
struct node *children;
children = root -> left_child;
root -> left_child = children -> right_child;
children -> right_child = root;
return children;
}
struct node* RR_rotation(struct node *root) {
struct node *children;
children = root -> right_child;
root -> right_child = children -> left_child;
children -> left_child = root;
return children;
}
struct node* LR_rotation(struct node *root) {
struct node *children;
children = root -> left_child;
root -> left_child = RR_rotation(children);
return LL_rotation(root);
}
struct node* RL_rotation(struct node *root) {
struct node *children;
children = root -> right_child;
root -> right_child = LL_rotation(children);
return RR_rotation(root);
}
int height(struct node *target) {
int h = 0, lh, rh, maxh;
if (target != NULL) {
lh = height(target -> left_child);
rh = height(target -> right_child);
if (rh > lh) {
maxh = rh;
} else {
maxh = lh;
}
h = maxh + 1;
}
return h;
}
int height_difference(struct node *target) {
int lh, rh, b;
lh = height(target -> left_child);
rh = height(target -> right_child);
b = lh - rh;
return b;
}
struct node* decide_rotation(struct node *target) {
int diff = height_difference(target);
int res;
if (diff > 1) {
res = height_difference(target -> left_child);
if (res > 0) {
target = LL_rotation(target);
} else {
target = LR_rotation(target);
}
} else if (diff < -1) {
res = height_difference(target -> right_child);
if (res > 0) {
target = RL_rotation(target);
} else {
target = RR_rotation(target);
}
}
return target;
}
struct node* insert(struct node *target, int data, char *info) {
if (target == NULL) {
target = (struct node*) malloc(sizeof(struct node));
target -> val = data;
target -> str = info;
target -> left_child = NULL;
target -> right_child = NULL;
return target;
} else if (data > target -> val) {
target -> right_child = insert(target -> right_child, data, info);
target = decide_rotation(target);
} else if (data < target -> val) {
target -> left_child = insert(target -> left_child, data, info);
target = decide_rotation(target);
}
return target;
}
void ioTraverse(struct node *tree) {//in-order traversal
if (tree == NULL) {
return;
} else {
ioTraverse(tree -> left_child);
printf("%d %s\n", tree -> val, tree -> str);
ioTraverse(tree -> right_child);
}
}
void random_string_generator(char foo[], int length) {
int i = 0;
length--;
srand((unsigned int) time(0) + getpid());
while (length--) {
foo[i] = rand() % 94 + 33;
i++;
srand(rand());
}
return;
}
int rand_range(int min, int max) {
srand((unsigned int) time(&t));
srand(rand());
return rand() % (max - min + 1) + min;
}
int main() {
printf("Enter the no. of elements:\n");
int n, ch, i, val, idx = 1;
scanf("%d", &n);
struct node *tree = NULL;
for (i = 0; i < n; i++) {
char res[123];
random_string_generator(res, 6);
printf("%s\n", res);
}
for (i = 0; i < n; i++) {
char foo[123];
val = rand_range(idx, idx + 100);
//scanf("%d", &val);
random_string_generator(foo, 6);
//scanf("%s", &foo);
printf("%s\n", foo);
tree = insert(tree, val, foo);
idx += rand_range(200, rand_range(240, 260));
//ioTraverse(tree);
}
ioTraverse(tree);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire