#include "bintree.h" #include "tokenlookup.h" #include /* for strcpy() */ #include /* for bzero() */ #include /* for printf() */ #include /* for malloc() */ /* $Id: bintree.c,v 1.15 2005/09/04 14:14:56 joshr Exp $ */ /* get space for a new empty tree node */ bintree * new_node() { bintree *obj = ((bintree*)malloc(sizeof(bintree))); /* get space */ bzero( obj, sizeof(bintree)); /* zero it, which sets member ints to 0 and member ptrs to NULL */ return obj; } /* create an initialized tree node */ struct bintree * init_new_node(char *str, int token, bintree * left_child, bintree * right_child) { struct bintree * t; t = new_node(); /* all members are NULLs and 0's */ set_string(t, str); t->token = token; t->left = left_child; t->right = right_child; return t; } /* set a tree node's string member */ bintree * set_string(bintree *tree, char *str) { free_string(tree); /* delete string there if it exists */ if (str) { /* if it's not a null ptr */ tree->string = malloc( strlen(str) + 1 ); strcpy(tree->string, str); } return tree; } /* free the tree's string */ bintree* free_string(bintree *tree) { if (tree && tree->string) { free(tree->string); tree->string = NULL; } return tree; } void print_inorder(bintree * root, int indentlevel) { char prefix[1024] = {0}; /* set to all zeros */ int i=0; for(i=0; ileft) { print_inorder( root->left, indentlevel+1 ); printf(" "); } /* printf("%s [%s]", root->string, lookup_token(root->token)); */ if (root->string) printf("%s", root->string); if(root->right) { printf(" "); print_inorder( root->right, indentlevel+1 ); } printf( ")" ); } #if 0 void print_preorder(bintree * root, int indentlevel) { char prefix[1024] = {0}; /* set to all zeros */ int i=0; for(i=0; istring); /* printf("%s [%s]", root->string, lookup_token(root->token)); */ printf( " (" ); if(root->left) print_preorder( root->left, indentlevel+1 ); if(root->right) print_preorder( root->right, indentlevel+1 ); printf( ") " ); } } #endif