Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Node
- {
- double key;
- struct Node *left;
- struct Node *right;
- char *str;
- } Node;
- int tree_find(struct Node *root, double key, char **pstr);
- Node *init_node(double key)
- {
- Node *node = calloc(1, sizeof(*node));
- node->key = key;
- node->left = NULL;
- node->right = NULL;
- node->str = calloc(100, sizeof(*(node->str)));
- return node;
- }
- int main(void)
- {
- Node *node = init_node(10.000000001);
- node->left = init_node(-89.99999999);
- node->right = init_node(110.0);
- strcpy(node->str, "node");
- strcpy(node->left->str, "left node");
- strcpy(node->right->str, "right node");
- char *ans = NULL;
- printf("%d\t", tree_find(node, 10.0, &ans));
- printf("%s\n", !ans ? "(null)" : ans);
- printf("1\tnode\n\n");
- ans = NULL;
- printf("%d\t", tree_find(node, -90.0, &ans));
- printf("%s\n", !ans ? "(null)" : ans);
- printf("1\tleft node\n\n");
- ans = NULL;
- printf("%d\t", tree_find(node, 110.0, &ans));
- printf("%s\n", !ans ? "(null)" : ans);
- printf("1\tright node\n\n");
- ans = NULL;
- printf("%d\t", tree_find(node, -190.0, &ans));
- printf("%s\n", !ans ? "(null)" : ans);
- printf("0\t(null)\n\n");
- ans = NULL;
- printf("%d\t", tree_find(node, 210.0, &ans));
- printf("%s\n", !ans ? "(null)" : ans);
- printf("0\t(null)\n\n");
- ans = NULL;
- printf("%d\t", tree_find(node, -50.0, &ans));
- printf("%s\n", !ans ? "(null)" : ans);
- printf("0\t(null)\n\n");
- ans = NULL;
- printf("%d\t", tree_find(node, 50.0, &ans));
- printf("%s\n", !ans ? "(null)" : ans);
- printf("0\t(null)\n\n");
- ans = NULL;
- free(node->left->str);
- free(node->right->str);
- free(node->str);
- free(node->left);
- free(node->right);
- free(node);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement