forked from detro/coding-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst_left-right_print.node.js
85 lines (73 loc) · 1.87 KB
/
bst_left-right_print.node.js
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
85
function bst_left_print(root) {
var max_printed_level = -1;
function bst_left_print_closure(root, level) {
level = level || 0;
if (level > max_printed_level) {
console.log("level: " + level + " - " + root.value);
max_printed_level = level;
}
if (root.left) {
bst_left_print_closure(root.left, level+1);
}
if (root.right) {
bst_left_print_closure(root.right, level+1);
}
}
bst_left_print_closure(root);
}
function bst_right_print(root) {
var max_printed_level = -1;
function bst_right_print_closure(root, level) {
level = level || 0;
if (level > max_printed_level) {
console.log("level: " + level + " - " + root.value);
max_printed_level = level;
}
if (root.right) {
bst_right_print_closure(root.right, level+1);
}
if (root.left) {
bst_right_print_closure(root.left, level+1);
}
}
bst_right_print_closure(root);
}
function make_node(val, left_child, right_child) {
return {
value : val,
left : left_child,
right : right_child
};
}
var r;
r = make_node(12,
make_node(21,
make_node(33,
null,
make_node(44,
null,
null
)
)
),
make_node(1,
make_node(4,
null,
null
),
make_node(5,
null,
make_node(8,
null,
make_node(9,
null,
null
)
)
)
)
);
console.log("*** LEFT PRINT ***");
bst_left_print(r);
console.log("*** RIGHT PRINT ***");
bst_right_print(r);