-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.cpp
49 lines (38 loc) · 826 Bytes
/
tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <stdio.h>
using namespace std;
struct node
{
int key;
int left_idx;
int right_idx;
};
bool fullread(int fd, node *p) {
ssize_t already_read = 0;
while (already_read != sizeof(node)) {
ssize_t read_bytes = read(fd, p + already_read, sizeof(node) - already_read);
if (read_bytes == 0 || read_bytes == -1) {
return 0;
}
already_read += read_bytes;
}
return 1;
}
void print_tree(int fd, int index) {
node p;
lseek(fd, index * sizeof(node), SEEK_SET);
fullread(fd, &p);
if (p.right_idx) {
print_tree(fd, p.right_idx);
}
printf("%d\n", p.key);
if (p.left_idx) {
print_tree(fd, p.left_idx);
}
}
int main(int argc, char* argv[]) {
int in_fd = open(argv[1], O_RDONLY);
print_tree(in_fd, 0);
}