changeset 24:5354e0f8f557 draft

add unbalance_binary_tree.c
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Mon, 13 Aug 2012 03:59:25 +0900
parents 72b74b3466f9
children 844c7b5d74ab
files compare/tree/unbalance_binary_tree.c compare/tree/unbalance_binary_tree.cbc
diffstat 2 files changed, 92 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compare/tree/unbalance_binary_tree.c	Mon Aug 13 03:59:25 2012 +0900
@@ -0,0 +1,83 @@
+#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 insert(Node *p, int num)
+{
+	if (p->num == -1) {
+		p->num = num;
+		p->left = make_node();
+		p->right = make_node();
+		return;
+	}
+	if (p->num < num) {
+		insert(p->left,num);
+	} else {
+		insert(p->right, num);
+	}
+}
+
+Node *return_nodes(int *numbers, int size)
+{
+	Node *root = make_node();
+	root->num = numbers[0];
+	root->left = make_node();
+	root->right = make_node();
+
+	int i;
+	for (i=1;i<size;i++) {
+		insert(root, numbers[i]);
+	}
+	return 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);
+}
+
+Node *lookup(Node *p, int value)
+{
+	if (p == NULL)
+		return NULL;
+	if (p->num == value) {}
+
+}
+
+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);
+	
+	Node *root = return_nodes(numbers, size);
+
+	print_nodes(root);
+	free_nodes(root);
+
+
+	return 0;
+}
--- a/compare/tree/unbalance_binary_tree.cbc	Mon Aug 13 03:52:04 2012 +0900
+++ b/compare/tree/unbalance_binary_tree.cbc	Mon Aug 13 03:59:25 2012 +0900
@@ -19,6 +19,7 @@
 
 
 Node *make_node();
+__code exit0(Ds *ds);
 __code loop(Ds *ds);
 __code insert(Ds *ds, Node* p);
 __code create_nodes(Ds *ds);
@@ -34,14 +35,18 @@
 	return n;
 }
 
+__code exit0(Ds *ds)
+{
+	print_nodes(ds->root);
+	exit(0);
+}
+
 __code loop(Ds *ds)
 {
 	if (ds->i < ds->size) {
 		goto insert(ds, ds->root);
 	}
-
-	print_nodes(ds->root);
-	exit(0);
+	goto ds->next(ds);
 }
 
 __code insert(Ds *ds, Node *p)
@@ -102,6 +107,7 @@
 	ds->numbers = numbers;
 	ds->size = size;
 	ds->i = 0;
+	ds->next = exit0;
 	goto main1(ds);
 
 	return 0;