作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用 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;
}
最佳答案
我认为这与传递给 srand() 方法的种子有关。
time() 方法返回当前时间(以秒为单位),因此在程序执行过程中,time(0) + getpid()
始终保持不变,因此会生成相同的整数在 rand()
函数中。
关于c - 在C中重复生成相同值的随机字符串和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28144207/
我是一名优秀的程序员,十分优秀!