home *** CD-ROM | disk | FTP | other *** search
- /* CHARFREQ.C: Analyzes frequency of printable characters in a text
- string, using a binary tree */
-
- #include <stdio.h>
- #include <string.h>
- #include <alloc.h>
- #include <ctype.h>
-
- /* NODE STRUCTURE */
- typedef struct ntag {
- char letter; /* key value */
- int freq; /* frequency */
- struct ntag *LLink, *RLink; /* linkage pointers */
- } NTYPE;
-
- /* GLOBAL */
- static NTYPE *root; /* entry to binary tree*/
- /* ---------------------- LOCAL FUNCTIONS ------------------------ */
- NTYPE *found (char arg, NTYPE *node) /* search for matching key */
- {
- while (arg != node->letter) /* loop while no match */
- if (node == NULL) /* if hit a leaf... */
- return (NULL); /* abandon search */
- else /* else go left or right from here */
- node = (arg < node->letter) ? node->LLink : node->RLink;
- return (node); /* when found match, return node addr */
- } /* ------------------------ */
- NTYPE *newnode (char arg, NTYPE *parent, NTYPE *node) /* add node */
- {
- if (node != NULL) /* recur to find location for node */
- if (arg < node->letter) /* going left or right as appropr */
- node = newnode (arg, node, node->LLink);
- else
- node = newnode (arg, node, node->RLink);
- else { /* position found for new leaf */
- node = (NTYPE*) malloc (sizeof (NTYPE)); /* alloc heap space */
- node->LLink = node->RLink = NULL; /* leaf pointers always NULL */
- node->letter = arg; /* set key */
- node->freq = 1; /* and count of one */
- if (root != NULL) /* if not first node... */
- if (arg < parent->letter) /* set parent's correct pointer */
- parent->LLink = node;
- else
- parent->RLink = node;
- }
- return (node);
- } /* ------------------------ */
- void inOrder (NTYPE *node) /* output tree by key order */
- {
- if (node != NULL) {
- inOrder (node->LLink); /* keep working left */
- printf ("\n%c %d", /* then print contents */
- node->letter, node->freq);
- inOrder (node->RLink); /* now work to the right */
- }
- } /* ------------------------ */
- void postOrder (NTYPE *node) /* delete tree from heap */
- {
- if (node != NULL) {
- postOrder (node->LLink); /* do all to left */
- postOrder (node->RLink); /* all to right */
- free (node); /* and finally this node */
- }
- } /* ---------------------- MAIN PROGRAM ------------------------- */
- main ()
- {
- char c, text [80];
- int n, p;
- NTYPE *current;
-
- do {
- puts ("\n\nType text to be analyzed:");
- gets (text);
- if ((n = strlen (text)) != 0) { /* process if text typed */
- for (p = 0; p < n; p++) {
- c = toupper (text [p]); /* get next letter, upshift */
- if (isprint (c)) { /* see if printable */
- if (root == NULL) /* if first letter in text */
- root = newnode (c, NULL, NULL); /* create root */
- else
- if ((current = found (c, root)) != NULL)
- current->freq++; /* count if duplicate */
- else
- newnode (c, root, root); /* else add to tree */
- } /* end of 'if printable' */
- } /* end of for loop */
- /* PRINT FREQUENCY ANALYSIS */
- puts ("\nFrequency of letters used:");
- inOrder (root);
-
- /* DELETE TREE FROM HEAP */
- postOrder (root);
- root = NULL;
- } /* end of if */
- } while (n != 0); /* repeat */
- }