|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +// code 1 |
| 4 | +struct bnode { |
| 5 | + int v; // label |
| 6 | + struct bnode *lc, *rc; // left & right children |
| 7 | +}; |
| 8 | + |
| 9 | +// code 3 |
| 10 | +void trav_pre(struct bnode *t) { |
| 11 | + if (t != NULL) { |
| 12 | + printf("%d", t->v); |
| 13 | + trav_pre(t->lc); |
| 14 | + trav_pre(t->rc); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/* |
| 19 | +// code 4 |
| 20 | +void trav_in(struct bnode *t) { |
| 21 | + if (t != NULL) { |
| 22 | + trav_in(t->lc); |
| 23 | + printf("%d", t->v); |
| 24 | + trav_in(t->rc); |
| 25 | + } |
| 26 | +} |
| 27 | +*/ |
| 28 | + |
| 29 | +/* |
| 30 | +// code 5 |
| 31 | +void trav_pre(struct bnode *t) { |
| 32 | +start: |
| 33 | + if (t != NULL) { |
| 34 | + printf("%d", t->v); |
| 35 | + trav_pre(t->lc); |
| 36 | + t = t->rc; |
| 37 | + goto start; |
| 38 | + } |
| 39 | +} |
| 40 | +*/ |
| 41 | + |
| 42 | +/* |
| 43 | +// code 6 |
| 44 | +void trav_pre(struct bnode *t) { |
| 45 | + while (t != NULL) { |
| 46 | + printf("%d", t->v); |
| 47 | + trav_pre(t->lc); |
| 48 | + t = t->rc; |
| 49 | + } |
| 50 | +} |
| 51 | +*/ |
| 52 | + |
| 53 | +/* |
| 54 | +#define STACK_SIZE 32 |
| 55 | +
|
| 56 | +// code 7 |
| 57 | +void trav_pre(struct bnode *t) { |
| 58 | + struct bnode* stack[STACK_SIZE]; |
| 59 | + int sp = 0; |
| 60 | +start: |
| 61 | + while (t != NULL) { |
| 62 | + printf("%d", t->v); |
| 63 | + stack[sp++] = t; // PUSH |
| 64 | + t = t->lc; |
| 65 | + goto start; |
| 66 | +resume: |
| 67 | + t = t->rc; |
| 68 | + } |
| 69 | + if (sp > 0) { |
| 70 | + t = stack[--sp]; // POP |
| 71 | + goto resume; |
| 72 | + } |
| 73 | +} |
| 74 | +*/ |
| 75 | + |
| 76 | +/* |
| 77 | +#define STACK_SIZE 32 |
| 78 | +
|
| 79 | +// code 8 |
| 80 | +void trav_pre(struct bnode *t) { |
| 81 | + struct bnode *stack[STACK_SIZE]; |
| 82 | + int sp = 0; |
| 83 | + for (;;) { |
| 84 | + while (t != NULL) { |
| 85 | + printf("%d\n", t->v); |
| 86 | + stack[sp++] = t; // PUSH |
| 87 | + t = t->lc; |
| 88 | + } |
| 89 | + if (sp == 0) return; |
| 90 | + t = stack[--sp]; // POP |
| 91 | + t = t->rc; |
| 92 | + } |
| 93 | +} |
| 94 | +*/ |
| 95 | + |
| 96 | +/* |
| 97 | +// code 9 |
| 98 | +void trav_post(struct bnode *t) { |
| 99 | + if (t != NULL) { |
| 100 | + trav_post(t->lc); |
| 101 | + trav_post(t->rc); |
| 102 | + printf("%d", t->v); |
| 103 | + } |
| 104 | +} |
| 105 | +*/ |
| 106 | + |
| 107 | +/* |
| 108 | +// code 10 |
| 109 | +void trav_pre(struct bnode *t) { |
| 110 | + while (t != NULL) { |
| 111 | + if (t->lc == NULL) { |
| 112 | + printf("%d", t->v); |
| 113 | + t = t->rc; |
| 114 | + } |
| 115 | + else { |
| 116 | + struct bnode *rm = t->lc; |
| 117 | + while (rm->rc != NULL && A) { // A should be replaced |
| 118 | + rm = rm->rc; |
| 119 | + } |
| 120 | + if (rm->rc == NULL) { |
| 121 | + printf("%d", t->v); |
| 122 | + rm->rc = t; |
| 123 | + t = t->lc; |
| 124 | + } |
| 125 | + else { |
| 126 | + rm->rc = NULL; |
| 127 | + t = t->rc; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +*/ |
| 133 | + |
| 134 | +int main() { |
| 135 | + // code 2 |
| 136 | + struct bnode n8 = { 8, NULL, NULL }; |
| 137 | + struct bnode n7 = { 7, NULL, NULL }; |
| 138 | + struct bnode n6 = { 6, NULL, NULL }; |
| 139 | + struct bnode n5 = { 5, &n8, NULL }; |
| 140 | + struct bnode n4 = { 4, NULL, NULL }; |
| 141 | + struct bnode n3 = { 3, &n6, &n7 }; |
| 142 | + struct bnode n2 = { 2, &n4, &n5 }; |
| 143 | + struct bnode n1 = { 1, &n2, &n3 }; |
| 144 | + |
| 145 | + trav_pre(&n1); |
| 146 | + printf("\n"); |
| 147 | + |
| 148 | + return 0; |
| 149 | +} |
0 commit comments