Ejemplo n.º 1
0
//complete.c
//Created by Sameer Mehta
int main() {
	trie * print; //for printing
	// load all the dictionary words into the tree
	trie * trie;							//creates a instance of trie from ADT
	TrieInitialize(&trie);						//initializes the creates instance

	FILE * infile = fopen("american-english-no-accents", "r");	//creates a file to read to 'infile'
	char line[256];							//size of lines
	while ((fscanf(infile, "%s", line)) != EOF) {			//scan function to scan the file
		TrieAdd(trie, line);					//adds the file to the trie usign the fucntion TrieAdd
	}
	fclose(infile);							//close the reading of the file

	//get user input
    char * prefix;
   // trie * print;							//instance is made
    while (1) {								//while true
    	printf("Enter the word: ");					//asks for user input
    	fscanf(stdin, "%s", prefix);
    	fflush(stdin);							//deallocated/frees memory

    	print = TrieSearch(trie, prefix);				//prints the trie fby calling the search function
    	TriePrinter(print);						//prints what was initialized above using the printer function
    }
}
Ejemplo n.º 2
0
int main()
{
    char words[1010][22];
    char temp[25];
    int num = 0;

    TrieNode *root = new TrieNode();

    root->count = 1;
    for( int i=0; i < SIZE; i++)
        root->children[i] = NULL;

    while( scanf("%s", words+num) )
    {     
      
        TrieInsert( root, words[num] );
        num++;  
         // printf("%d:%s\n", num, words[num-1]);
    }

   // printf("Start search\n");
    for( int i = 0; i < num; i++)
    {

        int retValue = TrieSearch( root, words[i] );
        printf("%s ", words[i]);

        for(int j = 0; j< retValue; j++)
            printf("%c", words[i][j]);
        printf("\n");
    }
    return 0;
}
Ejemplo n.º 3
0
void TrieAdd(trieNode_t **root, char *key, int data)
{
 trieNode_t *pTrav = NULL;

 if(NULL == *root)
 {
  printf("NULL tree\n");
  return;
 }
#ifdef DEBUG
 printf("\nInserting key %s: \n",key);
#endif
 pTrav = (*root)->children;



 if(pTrav == NULL)
 {
  /*First Node*/
  for(pTrav = *root; *key; pTrav = pTrav->children)
  {
   pTrav->children = TrieCreateNode(*key, 0xffffffff);
   pTrav->children->parent = pTrav;
#ifdef DEBUG
   printf("Inserting: [%c]\n",pTrav->children->key);
#endif
   key++;
  }

  pTrav->children = TrieCreateNode('\0', data);
  pTrav->children->parent = pTrav;
#ifdef DEBUG
  printf("Inserting: [%c]\n",pTrav->children->key);
#endif
  return;
 }

 if(TrieSearch(pTrav, key))
 {
  printf("Duplicate!\n");
  return;
 }

 while(*key != '\0')
 {
  if(*key == pTrav->key)
  {
   key++;
#ifdef DEBUG
   printf("Traversing child: [%c]\n",pTrav->children->key);
#endif
   pTrav = pTrav->children;
  }
  else
   break;
 }

 while(pTrav->next)
 {
  if(*key == pTrav->next->key)
  {
   key++;
   TrieAdd(&(pTrav->next), key, data);
   return;
  }
  pTrav = pTrav->next;
 }

 if(*key)
 {
  pTrav->next = TrieCreateNode(*key, 0xffffffff);
 }
 else
 {
  pTrav->next = TrieCreateNode(*key, data);
 }

 pTrav->next->parent = pTrav->parent;
 pTrav->next->prev = pTrav;

#ifdef DEBUG
 printf("Inserting [%c] as neighbour of [%c] \n",pTrav->next->key, pTrav->key);
#endif

 if(!(*key))
  return;

 key++;

 for(pTrav = pTrav->next; *key; pTrav = pTrav->children)
 {
  pTrav->children = TrieCreateNode(*key, 0xffffffff);
  pTrav->children->parent = pTrav;
#ifdef DEBUG
  printf("Inserting: [%c]\n",pTrav->children->key);
#endif
  key++;
 }

 pTrav->children = TrieCreateNode('\0', data);
 pTrav->children->parent = pTrav;
#ifdef DEBUG
 printf("Inserting: [%c]\n",pTrav->children->key);
#endif
 return;
}
Ejemplo n.º 4
0
void TrieRemove(trieNode_t **root, char *key)
{
 trieNode_t *tPtr = NULL;
 trieNode_t *tmp = NULL;

 if(NULL == *root || NULL == key)
  return;

 tPtr = TrieSearch((*root)->children, key);

 if(NULL == tPtr)
 {
  printf("Key [%s] not found in trie\n", key);
  return;
 }

#ifdef DEBUG
 printf("Deleting key [%s] from trie\n", key);
#endif

 while(1)
 {
  if( tPtr->prev && tPtr->next)
  {
   tmp = tPtr;
   tPtr->next->prev = tPtr->prev;
   tPtr->prev->next = tPtr->next;
#ifdef DEBUG
   printf("Deleted [%c] \n", tmp->key);
#endif
   free(tmp);
   break;
  }
  else if(tPtr->prev && !(tPtr->next))
  {
   tmp = tPtr;
   tPtr->prev->next = NULL;
#ifdef DEBUG
   printf("Deleted [%c] \n", tmp->key);
#endif
   free(tmp);
   break;
  }
  else if(!(tPtr->prev) && tPtr->next)
  {
   tmp = tPtr;
   tPtr->next->prev = NULL;
   tPtr->parent->children = tPtr->next;
#ifdef DEBUG
   printf("Deleted [%c] \n", tmp->key);
#endif
   free(tmp);
   break;
  }
  else
  {
   tmp = tPtr;
   tPtr = tPtr->parent;
   tPtr->children = NULL;
#ifdef DEBUG
   printf("Deleted [%c] \n", tmp->key);
#endif
   free(tmp);
  }
 }

#ifdef DEBUG
 printf("Deleted key [%s] from trie\n", key);
#endif
}
Ejemplo n.º 5
0
//-----------------------------------------------------------------
// Add a Trie (Prefix Tree) Node
//-----------------------------------------------------------------
void TrieAdd(trieNode_t **root, wchar_t *key)
{
	trieNode_t *pTrav = NULL;

	if (NULL == *root) {
		//printf("NULL tree\n");
		return;
	}
	//printf("\nInserting key %ws: \n", key);
	pTrav = (*root)->children;

	if (pTrav == NULL)
	{
		// If the node is NULL, this must be the first Node
		for (pTrav = *root; *key; pTrav = pTrav->children) {
			pTrav->children = TrieCreateNode(*key);
			pTrav->children->parent = pTrav;
			//printf("Inserting: [%c]\n", pTrav->children->key);
			key++;
		}

		pTrav->children = TrieCreateNode('\0');
		pTrav->children->parent = pTrav;
		//printf("Inserting: [%c]\n", pTrav->children->key);
		return;
	}

	if (TrieSearch(pTrav, key))
	{
		//printf("Duplicate!\n");
		return;
	}

	while (*key != '\0')
	{
		if (*key == pTrav->key)
		{
			key++;
			//printf("Traversing child: [%c]\n", pTrav->children->key);
			pTrav = pTrav->children;
		}
		else
			break;
	}

	while (pTrav->next)
	{
		if (*key == pTrav->next->key)
		{
			key++;
			TrieAdd(&(pTrav->next), key);
			return;
		}
		pTrav = pTrav->next;
	}

	if (*key) {
		pTrav->next = TrieCreateNode(*key);
	}
	else {
		pTrav->next = TrieCreateNode(*key);
	}

	pTrav->next->parent = pTrav->parent;
	pTrav->next->prev = pTrav;

	//printf("Inserting [%c] as neighbour of [%c] \n", pTrav->next->key, pTrav->key);

	if (!(*key))
		return;

	key++;

	for (pTrav = pTrav->next; *key; pTrav = pTrav->children)
	{
		pTrav->children = TrieCreateNode(*key);
		pTrav->children->parent = pTrav;
		//printf("Inserting: [%c]\n", pTrav->children->key);
		key++;
	}

	pTrav->children = TrieCreateNode('\0');
	pTrav->children->parent = pTrav;
	//printf("Inserting: [%c]\n", pTrav->children->key);
	return;
}
Ejemplo n.º 6
0
void TrieAdd(Trie **root, char *key, int data)
{
 Trie *node = NULL;
 
 if(NULL == *root)
 {
  return;
 }
 node = (*root)->children;
 if(node == NULL)
 {
  /*First Node*/
  for(node = *root; *key; node = node->children)
  {
   node->children = TrieCreateNode(*key, 0xffffffff);
   node->children->parent = node;

   key++;
  }
  node->children = TrieCreateNode('\0', data);
  node->children->parent = node;

  return;
 }
 
 if(TrieSearch(node, key))
 {
  return;
 }
 
 while(*key != '\0')
 {
  if(*key == node->key)
  {
   key++;
   node = node->children;
  }
  else
   break;
 }
 
 while(node->next)
 {
  if(*key == node->next->key)
  {
   key++;
   TrieAdd(&(node->next), key, data);
   return;
  }
  node = node->next;
 }
 
 if(*key)
 {
  node->next = TrieCreateNode(*key, 0xffffffff);
 }
 else
 {
  node->next = TrieCreateNode(*key, data);
 }
 
 node->next->parent = node->parent;
 node->next->prev = node;
 
 if(!(*key))
  return;
 
 key++;
 
 for(node = node->next; *key; node = node->children)
 {
  node->children = TrieCreateNode(*key, 0xffffffff);
  node->children->parent = node;
  key++;
 }
 
 node->children = TrieCreateNode('\0', data);
 node->children->parent = node;
 return;
}