0

I have a list of numbers in a bst and I need to count how many times the program has the access to every single number and print it

enter 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define HASHSIZE 153

typedef struct tree
{
int key;
int count;
struct tree *dx;
struct tree *sx;
struct tree *parent;
} node;


int Insertion(node **T, node *z)
{
node* y = NULL;
node* x = *T;
// int i=1;


while ( x != NULL )      
{
    y = x;
    if ( z->key < x->key)                          
    {
        x = x->sx;
       // x->count=i;    I DON'T KNOW HERE
    } else if ( z->key > x->key)
    {
        x = x->dx;           

    } else
    {

        return -1;  
    }
 }
 z->parent = y;

 if ( y == NULL ) *T = z;
 if ( (y != NULL) && (z->key < y->key) ) y->sx = z;
 if ( (y != NULL) && (z->key > y->key) ) y->dx = z;

   return 0;
 }

 int main(void)
 {
   FILE *fp;
   fp = fopen("data.txt","r");
   node *x = NULL;
   int search;

   hashtable = (hash *) malloc(HASHSIZE * sizeof(hash));   
   for (i = 0; i<HASHSIZE; i++) hashtable[i].head = NULL;

   while (!feof(fp))
   {
     fscanf(fp, "%d\t", search);
     if ( Search(hashtable[H(search)].head, search)==NULL )   
        {
            x = NewNode(search);
            Insertion( &hashtable[H(x->key)].head, x);
        }       
   }

  //Here there's also the node elimination but it's not important right now

  fclose(fp);
  return 0;
  }

My problem is printing the number of times that a number compare in a file.

I put down there NewNode, Search and H function

//  HASH  //
typedef struct _hash    
{
    node *head;
} hash;

hash *hashtable = NULL;


int H(int key)  
{
return (key % HASHSIZE);
}



//  NEWNODE //

node* NewNode(int z)
{
  node* temp; 
  temp = (node*) malloc( sizeof(node) );
  temp->key = z;
  temp->sx = NULL;
  temp->dx = NULL;
  temp->parent = NULL;

return temp;
}



// SEARCH //
node* Search(node *x, int k)
{
  while ( (x != NULL) && (k != x->key) )
  {
    if ( k < x->key )  
    {
        x = x->sx;
    }
     else             
    {
        x = x->dx;
    }
  }
  return x;
 }

 // TOP //

 int Top(int a, int b)
 {
  if (a>b) return a;
  return b;
 }

 // MIN //

 node* Min(node *x)
 {
  while (x->sx != NULL) x = x->sx;
  return x;
 }

 // MAX //
  node* Max(node *x)
  {
   while (x->dx != NULL) x = x->dx;
   return x;
  }

  // NEXT //
  node* Next(node *x)
  {
   if (x->dx != NULL)
       return Min(x->dx);
   node *y = x->parent;
   while ( y != NULL && x == y->dx ){
    x = y;
    y = y->parent;
   }
    return y;
  }

 //  HEIGHT //

 int Height(node *x)
 {
  if (x == NULL)
    return 0;
   return( 1 + Top( Height(x->sx), Height(x->dx) ));
 }

 // DECANT //

 void Decant(node **T, node *u, node *v)
 {
    if ( u->parent == NULL)
    *T = v;
    if ( u->parent != NULL  &&  u == u->parent->sx )
    u->parent->sx = v;
    if ( u->parent != NULL  &&  u == u->parent->dx )
    u->parent->dx = v;
    if ( v != NULL)
    v->parent = u->parent;
 }

 // DELATE //
 void Delate(node **T, node *z)
 {
    node *y;
    if ( z->sx == NULL )
    Decant(T, z, z->dx);
    if ( (z->sx != NULL) && (z->dx == NULL) )
    Decant(T, z, z->sx);
    if ( (z->sx != NULL) && (z->dx != NULL) )
    {
      y = Min(z->dx);
      if (y->parent != z){
        Decant(T, y, y->dx);
        y->dx = z->dx;
        y->dx->parent = y;
    }
    Decant(T, z, y);
    y->sx = z->sx;
    y->sx->parent = y;
   }
   free(z);
  }

Thx everyone!

HugoB
  • 121
  • 7
  • It is off topic but please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). It is better as `while(fscanf(fp, "%d", &search) == 1)` and no need of the `\t` because it is usually ignored (except when `%c` is present somewhere). Please note your mistake the the missing `&`. – Weather Vane Nov 22 '16 at 20:14
  • I'm having trouble understanding you. Try writing a list of operations you want happening during a search, or pseudo-code. – kabanus Nov 22 '16 at 20:19
  • @WeatherVane thx so much! But right now I really need to fix the problem of counting. Can ya help me? – HugoB Nov 22 '16 at 20:24
  • @kabanus The code is correct, no problem with the node insertion and so on. I really need to understand how to fix the problem of counting. – HugoB Nov 22 '16 at 20:27
  • Your code does not compile with numerous errors and warnings. Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem. Show some examples of the input, the expected output, and the actual output. – Weather Vane Nov 22 '16 at 20:28
  • Also, even if it does work, I don't understand what your goal is. What exactly does "number of times that a number compare in a file" mean? when do you want to print/return this count? – kabanus Nov 22 '16 at 20:30
  • I have a huge file with numbers and they repeat each other randomly. For example 50 numbers that repeat themselves a lot of times. I need to know the number that appear more and print ONLY the counter (If the number 5, that is the more frequent appear 65 times, it has to print "65") – HugoB Nov 22 '16 at 20:40
  • Why do you need a tree for that? If the largest number is not great, a simple count array would do? – Weather Vane Nov 22 '16 at 20:52
  • @WeatherVane 'cause the problem is with bst. – HugoB Nov 22 '16 at 21:00

0 Answers0