Implement Binary Tree in C language-count word count using binary tree

Source: Internet
Author: User

Implement Binary Tree in C language-count word count using binary tree
Problem the words I need to calculate are directly hardcoded in the program. The reason for this is that the confusion caused by file input and output is omitted. In each article, I only talk about one topic; this makes it easier for me to review later. Solution first, we need to define a struct, as shown in the following code:

const int LONGEST_WORD = 32;    // The longest word size

struct binary_tree {
    char str[LONGEST_WORD];
    int count;
    struct binary_tree * left;
    struct binary_tree * right;
};

typedef struct binary_tree node;

 

Note that we assume that the longest word is defined as a constant. Here I think the length of 32 should be enough. If the article for statistics is a chemistry paper, we suggest you increase the number, because the chemical formula is usually very long; then, our struct; this should be easy to understand; because the C language does not provide the BOOL type I want, I wrote the following code myself; this definition is very useful. It is generally more recommended than define; enum BOOL {NO, YES}; typedef enum BOOL; next, we need to know how to compare the size of words; therefore, you need a function called cmp. The code implementation is as follows:
BOOL cmp(char * s, char * t)
{
    int i;
    for (i = 0; s[i] == t[i]; i++)
        if ( s[i] == '\0' )
            return NO;
    return (s[i] - t[i]) < 0 ? NO:YES;
}

 

Traverse two strings at the same time, and then process the returned values; this will only return NO/YES for two cases; otherwise, three values (-, positive number) will be returned; in that case, it is not conducive to our future work. Next, if YES is returned, we should (how) (what to do); if NO is returned, we should (how) (what to do); therefore, we need an insert function to insert two different data types into the left and right Subtrees;
void insert(node ** tree, char * val) {
    node * temp = NULL;
    if(!(*tree)) {
        temp = (node*)malloc(sizeof(node));
        temp->left = temp->right = NULL;
        temp->str = val;    //issue code ...
        temp->count = 1;
        *tree = temp;
        return ;
    }

    if(cmp(val, (*tree)->str)) {
        insert(&(*tree)->left,val);
    }else if (cmp(val,(*tree)->str)) {
        insert(&(*tree)->right,val);
    }else{
        (*tree)->count++;
    }
} 
 

 

The code in this section is almost the same as the code mentioned above (implementing Binary Tree in C Language). Where is the detailed introduction? Here, I will mainly explain the line with the issue code. If this line is not modified, the program will crash; however, I will intentionally not immediately modify it and continue writing down; we need a function to destroy the node:
void deltree(node * tree) {
    if(tree) {
        deltree(tree->left);
        deltree(tree->right);
        free(tree);
    }
}   

 

To view our results, we need a Traversal method. Here we choose the middle order!
void print_inorder(node * tree) {
    if(tree) {
        print_inorder(tree->left);
        printf("[%s\t\t\t]count:[%d]\n",tree->str,tree->count);
        print_inorder(tree->right);
    }
} 

 

After we introduce the header file stdio. h/stdlib. h, we import the main int main (int argc, char ** arg {
   node * root;
    node * tmp;
    //int i;

    root = NULL;
    /* Inserting nodes into tree */
    insert(&root,"hello");
    insert(&root,"hey");
    insert(&root,"hello");
    insert(&root,"ok");
    insert(&root,"hey");
    insert(&root,"hey");
    insert(&root,"hey");

    printf("In Order Display\n");
    print_inorder(root);/* Deleting all nodes of tree */

    deltree(root);
}

 

The gcc compilation and running result is as follows: Sure enough, there is a problem with our issue code because the string cannot be assigned a value like other strings, such as the int type; so we need a cpy function: void mystrcpy (char * s, char * t) {while (* s ++ = * t ++ )! = '\ 0');} All code is as follows:
#include <stdio.h>
#include <stdlib.h>


const int LONGEST_WORD = 32;    // The longest word size

struct binary_tree {
    char str[LONGEST_WORD];
    int count;
    struct binary_tree * left;
    struct binary_tree * right;
};

typedef struct binary_tree node;

enum BOOL {
    NO, 
    YES 
};

typedef enum BOOL BOOL;

BOOL cmp(char * s, char * t)
{
    int i;
    for (i = 0; s[i] == t[i]; i++)
        if ( s[i] == '\0' )
            return NO; 
    return (s[i] - t[i]) < 0 ? NO:YES;
}
void mystrcpy(char *s, char *t) 
{
    while ((*s++ = *t++) != '\0')
        ;   
}

void insert(node ** tree, char * val) {
    node * temp = NULL;
    if(!(*tree)) {
        temp = (node*)malloc(sizeof(node));
        temp->left = temp->right = NULL;
        //temp->str = val;  //issue code ...
        mystrcpy(temp->str,val);
        temp->count = 1;
        *tree = temp;
        return ;
    }

    if(cmp(val, (*tree)->str)) {
        insert(&(*tree)->left,val);
    }else if (cmp(val,(*tree)->str)) {
        insert(&(*tree)->right,val);
    }else{
        (*tree)->count++;
    }
}

void deltree(node * tree) {
    if(tree) {
        deltree(tree->left);
        deltree(tree->right);
        free(tree);
    }
}

void print_inorder(node * tree) {
    if(tree) {
        print_inorder(tree->left);
        printf("[%s\t\t\t]count:[%d]\n",tree->str,tree->count);
        print_inorder(tree->right);
    }
}




int main(int argc, char ** argv)
{
    node * root;
    node * tmp;
    //int i;

    root = NULL;
    /* Inserting nodes into tree */
    insert(&root,"hello");
    insert(&root,"hey");
    insert(&root,"hello");
    insert(&root,"ok");
    insert(&root,"hey");
    insert(&root,"hey");
    insert(&root,"hey");


    printf("In Order Display\n");
    print_inorder(root);


    /* Deleting all nodes of tree */

    deltree(root);
}

 

The final running result is as follows: Discussion, so this program has been completed! There are also many optimizations and more functions available. For example, you can find the number of occurrences of a specific character, or the number of rows of a specific character, and so on; we will gradually improve it in the future;


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.