-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001_bstreelink.cpp
79 lines (70 loc) · 1.44 KB
/
001_bstreelink.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
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*
This file should be compiled by g++, NOT gcc.
Because C++ feature [reference] is used.
*/
typedef struct bstree {
int value;
struct bstree * left;
struct bstree * right;
}Bstree, *pBstree;
pBstree startWalk(pBstree root);
void walkTree(pBstree &head, pBstree &tail, pBstree root);
void walkLink(pBstree head);
int main(void)
{
Bstree e4 = { 4,NULL,NULL };
Bstree e8 = { 8,NULL,NULL };
Bstree e12 = { 12,NULL,NULL };
Bstree e16 = { 16,NULL,NULL };
Bstree e6 = { 6, &e4, &e8 };
Bstree e14 = { 14, &e12, &e16 };
Bstree e10 = { 10, &e6, &e14 };
pBstree head = startWalk(&e10);
walkLink(head);
getchar();
return 0;
}
pBstree startWalk(pBstree root) {
pBstree head = NULL;
pBstree tail = NULL;
if (root == NULL) {
return NULL;
}
walkTree(head, tail, root);
return head;
}
void walkTree(pBstree &head, pBstree &tail, pBstree root) {
if (root->left == NULL) {
head = root;
}
else {
walkTree(head, tail, root->left);
root->left = tail;
tail->right = root;
}
if (root->right == NULL) {
tail = root;
}
else {
walkTree(root->right,tail, root->right);
root->right->left = root;
}
}
void walkLink(pBstree head) {
int i = 1;
pBstree end;
while (head) {
printf("%d----%d\n",i,head->value);
i++;
end = head;
head = head->right;
}
while (end){
i--;
printf("%d----%d\n",i,end->value);
end = end->left;
}
}