Mercurial > hg > Members > nobuyasu > CbC
changeset 23:72b74b3466f9 draft
add Makfie tree.c
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 13 Aug 2012 03:52:04 +0900 |
parents | 13a2c13bbd0b |
children | 5354e0f8f557 |
files | compare/tree/Makefile compare/tree/tree.c |
diffstat | 2 files changed, 112 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/compare/tree/Makefile Mon Aug 13 03:52:04 2012 +0900 @@ -0,0 +1,16 @@ +#CC = gcc +CC = cbc-gcc-4.6.0 +#CFLAGS = -O3 +CFLAGS = -O0 -g +PROG = tree unbalance_binary_tree + +all: $(PROG) + +tree: tree.c + $(CC) $(CFLAGS) -o $@ $^ + +unbalance_binary_tree: unbalance_binary_tree.cbc + $(CC) $(CFLAGS) -o $@ $^ + +clean: + rm -rf $(PROG) *.dSYM
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/compare/tree/tree.c Mon Aug 13 03:52:04 2012 +0900 @@ -0,0 +1,96 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct Node Node; +struct Node { + int num; + Node *left; + Node *right; +}; + + +Node *make_node() +{ + Node *n = malloc(sizeof(Node)); + n->num = -1; + n->left = NULL; + n->right = NULL; + return n; +} + +void create(Node *nodep, int *numbers, int start, int n) +{ + nodep->num = numbers[start]; + n--; + if (n < 1) return; + nodep->left = make_node(); + + int half = n/2; + create(nodep->left, numbers, start+1, half); + + if (n-half < 1) return; + nodep->right = make_node(); + create(nodep->right, numbers, start+half+1, n-half); +} + + + +Node *return_maked_nodes(int *numbers, int n) +{ + Node *root = make_node(); + root->left = make_node(); + root->right = make_node(); + root->num = numbers[0]; + n--; + if (n < 1) return root; + int half = n/2; + create(root->left, numbers, 1, half); + create(root->right, numbers, half+1, n-half); + + return root; +} + +void print_node(Node *p) +{ + if (p->left != NULL) print_node(p->left); + if (p->right != NULL) print_node(p->right); + if (p->num != -1) printf("%d \n",p->num); +} + +void free_node(Node *p) +{ + if (p->left != NULL) free_node(p->left); + if (p->right != NULL) free_node(p->right); + free(p); +} + +Node *lookup(Node *p, int value) +{ + if (p == NULL) + return NULL; + if (p->num == value) {} + +} + +void search_node(Node *p, int *numbers) +{ + + +} + +int main() +{ + int numbers[] = {3,5,10,2,65,23,19,42,50,29,32,67,33,31,99}; + int n = sizeof(numbers)/sizeof(int); + + Node *root; + + root = return_maked_nodes(numbers, n); + + search_node(root, numbers); + print_node(root); + free_node(root); + + + return 0; +}