-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad14jc.cpp
76 lines (64 loc) · 1.4 KB
/
zad14jc.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
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
#include<stdio.h>
#include<stdlib.h>
#include<stack>
typedef struct node *bin_tree;
struct node {
int val;
bin_tree left, right;
};
typedef struct _walk {
bin_tree current;
std::stack<bin_tree> parents;
} walk;
walk make_walk(bin_tree t){
return {t, {}};
}
void go_left(walk &w){
if(w.current && w.current->left){
w.parents.push(w.current);
w.current = w.current->left;
}
}
void go_right(walk &w){
if(w.current && w.current->right){
w.parents.push(w.current);
w.current = w.current->right;
}
}
void go_up(walk &w){
if(!w.parents.empty()){
w.current = w.parents.top();
w.parents.pop();
}
}
int lookup(walk &w){
return w.current->val;
}
bin_tree newNode(int val){
bin_tree nowy = (node*)malloc(sizeof(node));
nowy->val = val;
nowy->left = nowy->right = NULL;
return nowy;
}
int main(){
bin_tree t = newNode(1);
t->left = newNode(2);
t->left->left = newNode(4);
t->left->left->left = newNode(5);
t->right = newNode(3);
t->right->left = newNode(6);
t->right->right = newNode(7);
t->left->right = newNode(11);
t->right->left->right = newNode(12);
walk w = make_walk(t);
go_left(w);
go_left(w);
go_left(w);
go_right(w);
printf("%i\n", lookup(w));
go_up(w);
printf("%i\n", lookup(w));
go_up(w);
printf("%i\n", lookup(w));
return 0;
}