Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- typedef struct Node
- {
- int32_t key;
- int32_t left_ind;
- int32_t right_ind;
- } Node;
- int
- read_node(int fd, int n, Node *nd)
- {
- return ((lseek(fd, sizeof(Node) * n, SEEK_SET) != -1) &&
- (read(fd, nd, sizeof(*nd)) == sizeof(*nd)));
- }
- int
- rec(int fd, int n)
- {
- Node nd;
- if (!read_node(fd, n, &nd)) {
- return 0;
- }
- if (nd.right_ind != 0) {
- rec(fd, nd.right_ind);
- }
- printf("%d\n", nd.key);
- if (nd.left_ind != 0) {
- rec(fd, nd.left_ind);
- }
- return 1;
- }
- int
- main(int argc, char *argv[])
- {
- int fd = -1;
- if ((fd = open(argv[1], O_RDONLY)) == -1 ||
- !rec(fd, 0)) {
- fprintf(stderr, "Error error\n");
- return 1;
- }
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement