This repository was archived by the owner on Aug 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_tree.c
More file actions
84 lines (76 loc) · 2.24 KB
/
Copy pathprint_tree.c
File metadata and controls
84 lines (76 loc) · 2.24 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "print_tree.h"
#define UNUSED(x) (void)(x)
void print_in_dot(t_tree *node, int i, int *count, FILE *fp)
{
if (node == NULL)
return;
int node_id = ++(*count);
if (i != -1){
fprintf(fp, " node%d -> node%d;\n", i, node_id);
}
fprintf(fp, " node%d [label=\"%s\"];\n", node_id, (char *)node->value);
print_in_dot(node->left, node_id, count, fp);
print_in_dot(node->right, node_id, count, fp);
}
void print_in_json(t_tree *node, FILE *fp, int indent)
{
if (node == NULL)
return;
for (int i = 0; i < indent; i++) fprintf(fp, " ");
fprintf(fp, "{\n");
for (int i = 0; i < indent + 1; i++) fprintf(fp, " ");
fprintf(fp, "\"value\": \"%s\",\n", (char *)node->value);
for (int i = 0; i < indent + 1; i++) fprintf(fp, " ");
fprintf(fp, "\"left\": ");
if (node->left) {
fprintf(fp, "\n");
print_in_json(node->left, fp, indent + 2);
for (int i = 0; i < indent + 1; i++) fprintf(fp, " ");
} else {
fprintf(fp, "null");
}
fprintf(fp, ",\n");
for (int i = 0; i < indent + 1; i++) fprintf(fp, " ");
fprintf(fp, "\"right\": ");
if (node->right) {
fprintf(fp, "\n");
print_in_json(node->right, fp, indent + 2);
for (int i = 0; i < indent + 1; i++) fprintf(fp, " ");
} else {
fprintf(fp, "null");
}
fprintf(fp, "\n");
for (int i = 0; i < indent; i++) fprintf(fp, " ");
fprintf(fp, "}");
}
void print_tree(t_tree *root, const char *filename, OutputFormat format, TraversalOrder order)
{
// still be able to define order
UNUSED(order);
FILE *fp;
int count = 0;
if (filename != NULL) {
fp = fopen(filename, "w");
if (fp == NULL) {
fprintf(stderr, "error opening file %s: %s\n", filename, strerror(errno));
exit(1);
}
} else {
fp = stdout;
}
if (format == FORMAT_DOT) {
fprintf(fp, "digraph {\n");
print_in_dot(root, -1, &count, fp);
fprintf(fp, "}\n");
} else if (format == FORMAT_JSON) {
print_in_json(root, fp, 0);
fprintf(fp, "\n");
}
if (fp != stdout) {
fclose(fp);
}
}