Mercurial > hg > Members > nobuyasu > CbC
changeset 27:adce006dc64a draft
add unbalance_binary_tree2.cbc
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Aug 2012 17:45:55 +0900 |
parents | 393759114f3a |
children | ac2555eeeb17 |
files | compare/tree/unbalance_binary_tree.cbc compare/tree/unbalance_binary_tree2.cbc |
diffstat | 2 files changed, 184 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/compare/tree/unbalance_binary_tree.cbc Mon Aug 13 04:35:35 2012 +0900 +++ b/compare/tree/unbalance_binary_tree.cbc Tue Aug 14 17:45:55 2012 +0900 @@ -1,6 +1,9 @@ #include <stdio.h> #include <stdlib.h> + + + typedef struct Node Node; struct Node { int num; @@ -108,7 +111,7 @@ ds->size = size; ds->i = 0; ds->next = exit0; - goto main1(ds); + main1(ds); return 0; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/compare/tree/unbalance_binary_tree2.cbc Tue Aug 14 17:45:55 2012 +0900 @@ -0,0 +1,180 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 2048 +char main_stack[STACK_SIZE]; +#define stack_last (main_stack+STACK_SIZE) + +typedef char *stack; + +typedef struct Node Node; +struct Node { + int num; + Node *left; + Node *right; +}; + +typedef struct Ds Ds; +struct Ds { + Node *root; + int *numbers; + int size; + int i; + stack sp; + __code (*next)(Ds *ds); +}; + +typedef struct main_continuation Cont; +struct main_continuation { + Node *node; + __code (*ret)(); +}; + + +Node *make_node(); +__code exit0(Ds *ds); +__code cs(Ds *ds); +__code loop(Ds *ds); +__code insert(Ds *ds, Node* p); +__code create_nodes(Ds *ds); +void print_nodes(Node *p); +__code print_right_node(Ds *ds, Node *p); +__code cbc_print(Ds *ds, Node *p); +__code cbc_print_nodes(Ds *ds); +void free_nodes(Node *p); + +Node *make_node() +{ + Node *n = (Node *)malloc(sizeof(Node)); + n->num = -1; + n->left = NULL; + n->right = NULL; + return n; +} + +__code exit0(Ds *ds) +{ + exit(0); +} + +__code cs(Ds *ds) +{ +// print_nodes(ds->root); + ds->next = exit0; + goto cbc_print_nodes(ds); +} + +__code loop(Ds *ds) +{ + if (ds->i < ds->size) { + goto insert(ds, ds->root); + } + goto ds->next(ds); +} + +__code insert(Ds *ds, Node *p) +{ + if (p->num == -1) { + p->num = ds->numbers[ds->i]; + ds->i++; + + p->left = make_node(); + p->right = make_node(); + goto loop(ds); + } + if (p->num < ds->numbers[ds->i]) { + goto insert(ds,p->left); + } else { + goto insert(ds,p->right); + } +} + +__code create_nodes(Ds *ds) +{ + Node *root = make_node(); + root->num = ds->numbers[ds->i]; + ds->i++; + + root->left = make_node(); + root->right = make_node(); + + ds->root = root; + goto insert(ds, root); +} + +__code print_right_node(Ds *ds, Node *p) +{ + printf("%d \n", p->num); + if (p->right != NULL) { + goto cbc_print(ds, p->right); + } + if (ds->sp < stack_last) { + struct main_continuation *c = (struct main_continuation *)ds->sp; + Node *r = (Node *) c->node; + ds->sp += sizeof(struct main_continuation); + goto print_right_node(ds, r); + } + + goto ds->next(ds); +} + +__code cbc_print(Ds *ds, Node *p) +{ + if (p->left != NULL) { + struct main_continuation *c = + (struct main_continuation *)(ds->sp -= sizeof(struct main_continuation)); + c->node = p; + goto cbc_print(ds, p->left); + } + if (p->num != -1) printf("%d \n", p->num); + + if (ds->sp < stack_last) { + struct main_continuation *c = (struct main_continuation *)ds->sp; + + Node *r = (Node *) c->node; + ds->sp += sizeof(struct main_continuation); + goto print_right_node(ds, r); + } + + goto ds->next(ds); +} + +__code cbc_print_nodes(Ds *ds) +{ + goto cbc_print(ds, ds->root); +} + +void print_nodes(Node *p) +{ + if (p->left != NULL) print_nodes(p->left); + if (p->num != -1) printf("%d \n",p->num); + if (p->right != NULL) print_nodes(p->right); +} + +void free_nodes(Node *p) +{ + if (p->left != NULL) free_nodes(p->left); + if (p->right != NULL) free_nodes(p->right); + free(p); +} + +void main1(Ds *ds) +{ + goto create_nodes(ds); +} + +int main() +{ + int numbers[] = {3,5,10,2,65,23,19,42,50,29,32,67,33,31,99}; + int size = sizeof(numbers)/sizeof(int); + + Ds *ds = (Ds *)malloc(sizeof(Ds)); + ds->numbers = numbers; + ds->size = size; + ds->i = 0; + ds->sp = stack_last; + ds->next = cs; + main1(ds); + + return 0; +}