我正在尝试用C实现AVL树。我在每个节点存储一个随机整数和一个随机字符串。我改变了我的代码并获得随机字符串。但现在我又遇到了另一个问题。在有序遍历期间,我获得与输出相同的字符串。
任何人都可以查看我的代码吗?
#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--;
while (length--) {
foo[i] = rand() % 94 + 33;
i++;
}
return;
}
int rand_range(int min, int max) {
return rand() % (max - min + 1) + min;
}
int main() {
srand(0);
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;
}
答案 0 :(得分:-1)
我认为这与传递给srand()方法的种子有关。
time()方法以秒为单位返回当前时间,因此在程序执行期间,time(0) + getpid()
将始终保持不变,因此它将在rand()
函数中生成相同的整数。