baum

diff baum.c @ 33:2e564bf8599c

new error code 6; more readable printTree; cleanups
author meillo@marmaro.de
date Sat, 01 Mar 2008 17:36:59 +0100
parents 4e60d96265f0
children e986c6abed2b
line diff
     1.1 --- a/baum.c	Fri Feb 22 15:58:25 2008 +0100
     1.2 +++ b/baum.c	Sat Mar 01 17:36:59 2008 +0100
     1.3 @@ -23,8 +23,8 @@
     1.4  struct Stackitem* stack = NULL;
     1.5  
     1.6  
     1.7 -void printNode(struct Node* node);
     1.8 -void printTree(struct Node* root);
     1.9 +void printNode(struct Node* node, int level);
    1.10 +void printTree(struct Node* root, int level);
    1.11  struct Node* lastNode(struct Node* node);
    1.12  void delete(struct Node* node);
    1.13  
    1.14 @@ -69,41 +69,29 @@
    1.15  	if (node == NULL) {
    1.16  		return;
    1.17  	}
    1.18 -	if (node->down != NULL) {
    1.19 -		delete(node->down);
    1.20 -	}
    1.21 -	if (node->right != NULL) {
    1.22 -		delete(node->right);
    1.23 -	}
    1.24 +	delete(node->down);
    1.25 +	delete(node->right);
    1.26  	free(node); node=0;
    1.27  }
    1.28  
    1.29  
    1.30  /* print */
    1.31 -void printNode(struct Node* node) {
    1.32 +void printNode(struct Node* node, int level) {
    1.33  	if (node != NULL) {
    1.34 -		fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
    1.35 +		while (level-- > 0) {
    1.36 +			fprintf(stderr, "\t");
    1.37 +		}
    1.38 +		fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value);
    1.39  	}
    1.40  }
    1.41  
    1.42 -void printTree(struct Node* root) {
    1.43 +void printTree(struct Node* root, int level) {
    1.44  	if (root == NULL) {
    1.45  		return;
    1.46  	}
    1.47 -
    1.48 -	printNode(root);
    1.49 -	fprintf(stderr, "  down: ");
    1.50 -	if (root->down != NULL) {
    1.51 -		printTree(root->down);
    1.52 -	} else {
    1.53 -		fprintf(stderr, "NULL\n");
    1.54 -	}
    1.55 -	fprintf(stderr, "  right: ");
    1.56 -	if (root->right != NULL) {
    1.57 -		printTree(root->right);
    1.58 -	} else {
    1.59 -		fprintf(stderr, "NULL\n");
    1.60 -	}
    1.61 +	printNode(root, level);
    1.62 +	printTree(root->down, level+1);
    1.63 +	printTree(root->right, level);
    1.64  }
    1.65  
    1.66  
    1.67 @@ -175,7 +163,7 @@
    1.68  				node = newNode((char*) name, value);
    1.69  				if (indent > last_indent) { /* down */
    1.70  					/* if it goes more than one level down -> error */
    1.71 -					if (indent - last_indent > 1) {
    1.72 +					if (indent > last_indent + 1) {
    1.73  						fprintf(stderr, "error: Indention over more than one level. Only indent by one!\n");
    1.74  						exit(5);
    1.75  					}
    1.76 @@ -206,7 +194,8 @@
    1.77  				name[i] = (char) c;
    1.78  				i++;
    1.79  				if (i > 255) {
    1.80 -					break;
    1.81 +					fprintf(stderr, "error: node name too long, or no value given\n");
    1.82 +					exit(6);
    1.83  				}
    1.84  			}
    1.85  			name[i] = '\0';
    1.86 @@ -270,13 +259,13 @@
    1.87  	read_input(argv[0]);
    1.88  
    1.89  	if (option_verbose) {
    1.90 -		printTree(root);
    1.91 +		printTree(root, 0);
    1.92  	}
    1.93  
    1.94  	shell_return = action(root);
    1.95  
    1.96  	if (option_verbose) {
    1.97 -		printTree(root);
    1.98 +		printTree(root, 0);
    1.99  	}
   1.100  
   1.101  	delete(root);