The C language uses binary trees to count the number of times each word in a source file.

Source: Internet
Author: User

Due to the uncertainty of the words, binary trees are used for implementation:

// Treenode. htypedef struct _ treenode {int count; // number of occurrences char * word; // The struct _ treenode * left; struct _ treenode * right;} treenode; // allocate memory to treenode * talloc (void) {return (treenode *) malloc (sizeof (treenode);} // print tree void tprint (treenode * root) {// print left-> Self-> right IF (root! = NULL) {tprint (root-> left); printf ("% 4D % s \ n", root-> count, root-> word ); tprint (root-> right) ;}} // Add the word to the appropriate node location treenode * addnode (treenode * node, const char * Word) {int con; treenode * TMP; if (node = NULL) {node = talloc (); node-> COUNT = 1; node-> word = strdup (Word ); node-> left = node-> right = NULL;} else if (con = strcmp (word, node-> word) <0) {TMP = addnode (node-> left, word); node-> left = TMP;} else if (con> 0) {TM P = addnode (node-> right, word); node-> right = TMP;} else {node-> count ++;} return node ;} /** read the word from the specified stream */INT getword (char * Ch, size_t N, file * f) {int C; char * P = CH; while (isspace (C = fgetc (F); If (C! = EOF) * P ++ = C; If (! Isalpha (c) {* P = '\ 0'; return C ;}for (; -- N> 0; P ++) {If (! Isalnum (* P = fgetc (F) {ungetc (* P, f); break ;}* P = '\ 0'; return C ;} // whether the memory occupied by the tree is void treefree (treenode * root) {If (root! = NULL) {treefree (root-> left); free (root-> word); // release the node's Word Memory free (Root ); // whether the memory occupied by the node is treefree (root-> right) ;}// test. C # include <stdio. h> # include <stdlib. h> # include "treenode. H "# define Max 100int main (INT argc, char * argv []) {file * F; char W [Max] = {0}; char * fname =" treenode. H "; if (F = fopen (fname," R "))! = NULL) {treenode * root = NULL; while (getword (W, Max, f )! = EOF) {If (isalpha (W [0]) root = addnode (root, W);} tprint (Root); treefree (Root ); fclose (f);} else {printf ("Open % s error \ n", fname);} getchar (); Return 0 ;}

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.