baum
changeset 1:3da0ff17c8e7
added features (print, sum, number); split in header file
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 14:31:02 +0100 |
parents | 2f71d692d4f9 |
children | 557fa4df2bcd |
files | baum.c baum.h |
diffstat | 2 files changed, 90 insertions(+), 14 deletions(-) [+] |
line diff
1.1 --- a/baum.c Thu Feb 07 12:51:54 2008 +0100 1.2 +++ b/baum.c Thu Feb 07 14:31:02 2008 +0100 1.3 @@ -9,18 +9,21 @@ 1.4 1.5 #include <stdio.h> 1.6 #include <stdlib.h> 1.7 +#include <string.h> 1.8 1.9 +#include "baum.h" 1.10 1.11 -struct Node { 1.12 - char* name; 1.13 - char value; 1.14 - struct Node* down; 1.15 - struct Node* right; 1.16 -}; 1.17 +char action(struct Node* node); 1.18 1.19 struct Node* root; 1.20 1.21 1.22 +void logit(char* text) { 1.23 + fprintf(stderr, "[%s]\n", text); 1.24 +} 1.25 + 1.26 + 1.27 +/* new */ 1.28 struct Node* newNode(char* name) { 1.29 struct Node* node; 1.30 node = (struct Node*) malloc(sizeof(struct Node)); 1.31 @@ -31,40 +34,105 @@ 1.32 return node; 1.33 } 1.34 1.35 -void init() { 1.36 - root = newNode("print"); 1.37 - root->down = newNode("sum"); 1.38 -} 1.39 1.40 -void cleanup(struct Node* node) { 1.41 +/* delete */ 1.42 +void delete(struct Node* node) { 1.43 if (node->down != NULL) { 1.44 - cleanup(node->down); 1.45 + delete(node->down); 1.46 } 1.47 if (node->right != NULL) { 1.48 - cleanup(node->right); 1.49 + delete(node->right); 1.50 } 1.51 free(node); node=0; 1.52 } 1.53 1.54 + 1.55 +/* print */ 1.56 void printNode(struct Node* node) { 1.57 printf("Node: %20s (%c)\n", node->name, node->value); 1.58 } 1.59 1.60 void printTree(struct Node* root) { 1.61 printNode(root); 1.62 + printf(" down: "); 1.63 if (root->down != NULL) { 1.64 printTree(root->down); 1.65 + } else { 1.66 + printf("NULL\n"); 1.67 } 1.68 + printf(" right: "); 1.69 if (root->right != NULL) { 1.70 printTree(root->right); 1.71 + } else { 1.72 + printf("NULL\n"); 1.73 } 1.74 } 1.75 1.76 1.77 +char action_print(struct Node* node) { 1.78 + printf("%c\n", action(node->down)); 1.79 + return 0; 1.80 +} 1.81 + 1.82 +char action_sum(struct Node* node) { 1.83 + struct Node* tp; 1.84 + tp = node->down; 1.85 + while (tp != NULL) { 1.86 + node->value += action(tp); 1.87 + tp = tp->right; 1.88 + } 1.89 + return node->value; 1.90 +} 1.91 + 1.92 +char action_number(struct Node* node) { 1.93 + return node->value; 1.94 +} 1.95 + 1.96 +char action(struct Node* node) { 1.97 + if (strcmp(node->name, "print") == 0) { 1.98 + logit("print-node"); 1.99 + return action_print(node); 1.100 + } else if (strcmp(node->name, "sum") == 0) { 1.101 + logit("sum-node"); 1.102 + return action_sum(node); 1.103 + } else if (strcmp(node->name, "number") == 0) { 1.104 + logit("number-node"); 1.105 + return action_number(node); 1.106 + } else { 1.107 + fprintf(stderr, "unknown kind of node"); 1.108 + exit(1); 1.109 + } 1.110 +} 1.111 + 1.112 + 1.113 + 1.114 +/* traverse */ 1.115 +void traverse(struct Node* root) { 1.116 + /* each node controlls the nodes below itself */ 1.117 + action(root); 1.118 +} 1.119 + 1.120 +/* init */ 1.121 +void init() { 1.122 + root = newNode("print"); 1.123 + root->down = newNode("number"); 1.124 + root->down = newNode("sum"); 1.125 + root->down->down = newNode("number"); 1.126 + root->down->down->value = 70; /* 'F' */ 1.127 + root->down->down->right = newNode("number"); 1.128 + root->down->down->right->value = 50; /* '2' */ 1.129 + /* result should be 'x' */ 1.130 +} 1.131 + 1.132 + 1.133 +/* main */ 1.134 int main(int argc, char* argv[]) { 1.135 init(); 1.136 printTree(root); 1.137 - cleanup(root); 1.138 + 1.139 + action(root); 1.140 + 1.141 + delete(root); 1.142 1.143 return(0); 1.144 }