Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXLEN 15
- typedef struct treeNode{
- char string[MAXLEN+1];
- struct treeNode *left;
- struct treeNode *right;
- }treeNode;
- treeNode * insert(treeNode *node, char s[MAXLEN]){
- puts("running insert");
- if(node == NULL){
- node = (treeNode *)malloc(sizeof(treeNode));
- strncpy(node -> string, s, MAXLEN);
- node -> left = NULL;
- node -> right = NULL;
- }
- else if(strcmp(node->string, s)>0){
- node -> right = insert(node->right, s);
- }
- else if(strcmp(node->string, s)<0){
- node -> left = insert(node->left, s);
- }
- else if(strcmp(node->string, s) == 0){
- node -> left = insert(node->left, s);
- }
- return node;
- }
- void treeprint( treeNode *node , FILE *OUTPUT_FILE)
- {
- if ( node != NULL)
- {
- treeprint(node->left, OUTPUT_FILE);
- fprintf(OUTPUT_FILE , "%s\n" , node->string);
- treeprint(node->right , OUTPUT_FILE);
- }
- }
- int main(int argc, char *argv[]){
- treeNode *root = NULL;
- FILE *ifp;
- FILE *ofp;
- char s[MAXLEN+1];
- if(argc != 2){
- fprintf(stderr, "Usage: %s file\n", argv[0]); exit(1);
- }
- if((ifp = fopen(argv[1], "r")) == NULL){
- fprintf(stderr, "Could not open file: %s\n", argv[1]); exit(1);
- }
- ofp = fopen("output.txt", "w+");
- while(fscanf(ifp, "%s", &s) != EOF){
- root = insert(root, s);
- }
- treeprint(root, ofp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement