如何在二叉搜索树中递增计数器?

时间:2016-11-22 20:11:40

标签: c hashtable binary-search-tree counter

我在bst中有一个数字列表,我需要计算程序可以访问每个数字并打印它的次数

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;
  }

我的问题是打印文件中数字比较的次数。

我放下了NewNode,Search和H功能

//  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);
  }

大家好!

0 个答案:

没有答案