int main(int argc, const char * argv[]) { INT_TREE tree = (INT_TREE) malloc(sizeof(struct int_tree)); srand((unsigned)time(NULL)); for(int i = 0; i < 10; i++) { int low = rand() % 30; int high = rand() % 30; if(low > high) { int tmp = low; low = high; high = tmp; } INT_NODE node = int_new_node(low, high); interval_insert(tree, node); } print_interval_tree(tree, tree -> root); INT i = (INT) malloc(sizeof(struct interval)); i -> high = 18; i -> low = 15; INT_NODE node = interval_search(tree, i); printf("selected interval is: [%d, %d], max: %d, color %s", node -> interval -> low, node -> interval -> high, node -> max , node -> color == RED?"red":"black"); printf("\n"); return 0; }
void print_interval_tree(INT_TREE tree, INT_NODE node) { if(node != NULL) { printf("interval: [%d, %d] ", node -> interval -> low, node -> interval -> high); printf("max: %d ", node -> max); if(node -> color == BLACK) { printf("black\n"); }else { printf("red\n"); } print_interval_tree(tree, node -> left); print_interval_tree(tree, node -> right); } }
int main(int argc, char **argv) { int fd, i; interval_tree tree; struct stat st; if (argc < 2) { fprintf(stderr, "Usage: dump_intervals INTERVAL_FILE...\n"); return 1; } for (i = 1; i < argc; i++) { fd = open(argv[i], O_RDONLY); if (fd < 0) { perror(argv[i]); continue; } if (fstat(fd, &st) != 0) { perror(argv[i]); continue; } tree = interval_tree_create(1020, NULL); if (!interval_tree_read(tree, fd, st.st_size / sizeof(interval))) { fprintf(stderr, "%s: Could not read intervals\n", argv[i]); close(fd); interval_tree_destroy(tree); continue; } close(fd); printf("%s:\n", argv[i]); print_interval_tree(stdout, tree); interval_tree_destroy(tree); } return 0; }
void debug_interval_tree(interval_tree tree) { print_interval_tree(stderr, tree); }