Thread: Count occurrence in bin tree

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    Count occurrence in bin tree

    I need to count the number of times a certain char has performed in a
    string.
    Means to the string:
    cefgacaffe

    output:
    char occurence
    a 2
    c 2
    e 2
    f 3
    g 1

    This is what I've been up to:
    Code:
    #include<stdio.h>
    #include<malloc.h>
    
    typedef struct tree_el {
       char data;
       int counter;
       struct tree_el * right, * left;
    }TREE;
    
    typedef TREE *node;
    
    void insert(node * tree, node* item)
    {
    	if(!(*tree)) {
    	*tree = *item;
    	return;
    	}
    	if((*item)->data<(*tree)->data)
    		insert(&(*tree)->left, item);
    	else if((*item)->data>(*tree)->data)
    		insert(&(*tree)->right, item);
    }
    
    char intrav (node tree, char data)
    {
    	if (tree!=NULL)
    	{
    		
    		if (intrav(tree->left, data)==data)
    			++(tree->counter);		
    		printf ("%c | %d\n", tree->data, tree->counter);
    		intrav(tree->right, data);
    	}
    	return data;
    }
    
    void CountOcrnc (node tree)
    {
    	node temp;
    	temp=(node)malloc(sizeof(node));
    	temp->left= temp->right= NULL;
    	temp=tree;
    	while (tree!=NULL) 
    	{
    		intrav(tree, temp->data);
    		tree=tree->left;
    		tree=tree->right;
    	}
    }
    
    int main(void)
    {
    	char ltrs[20]={"cefgacaffe0"};
    int count=0;
    node curr=NULL, root= NULL;
    while ( ltrs[count] != '0' )
    	{
    		curr=(node) malloc (sizeof (TREE));
    		curr->left= curr->right= NULL;
    		curr->counter=0;
    		curr->data=ltrs[count];
    		insert(&root, &curr);
    		++count;
    	}
    
    CountOcrnc (root);
       return 1;
    }
    Guess I don't get expected output, cause of a mass I'm facing in intrav function.
    Please help me organize it.

    TIA,

    Ronen

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    In insert, if the node is found, increment counter. If the node is not found, set counter to 1. Now, to print each letter with the number of occurances, you just need to traverse the tree.
    Code:
    #include<stdio.h>
    #include<stdlib.h> /* malloc.h is not standard */
    
    typedef struct tree_el {
        char data;
        int counter;
        struct tree_el * right, * left;
    }TREE;
    
    typedef TREE *node;
    
    void insert(node * tree, node* item)
    {
        if(!(*tree)) {
            *tree = *item;
            return;
        }
        if((*item)->data<(*tree)->data)
            insert(&(*tree)->left, item);
        else if((*item)->data>(*tree)->data)
            insert(&(*tree)->right, item);
        else
            ++(*tree)->counter;
    }
    
    void intrav (node tree)
    {
        if (tree!=NULL)
        {
            intrav(tree->left);	
            printf ("%c | %d\n", tree->data, tree->counter);
            intrav(tree->right);
        }
    }
    
    int main(void)
    {
        char ltrs[20]={"cefgacaffe0"};
        int count=0;
    
        node curr=NULL, root= NULL;
        while ( ltrs[count] != '0' )
        {
            curr=(node) malloc (sizeof (TREE));
            curr->left= curr->right= NULL;
            curr->counter=1;
            curr->data=ltrs[count];
            insert(&root, &curr);
            ++count;
        }
        intrav(root);
    
        return 0; /* 0 is success, nonzero is failure */
    }

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Thank you, Ren!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM
  5. Independent-Robert Fisk-Osama
    By a muslim in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-18-2001, 08:41 PM