Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Patricia and Huffman, Sitting in a Trie

, 27 Dec 2004
Article demonstrates a text-based Patricia trie and adds new text-compression features.
article_demo.zip
MyPat.exe
article_src.zip
docs
pat.sdr
src
/*

"Text-Based Patricia Trie"

Copyright (C) 2004 by Michael Walter Jaworski

Licensed under the Academic Free License version 1.2

*/

#include "pat.h"

#include <iostream.h>
#include <fstream.h>

int main ()
{
	//data
	PayloadC* p = new PayloadC ();
	
	PatriciaTrieC pat;
	
	ifstream ifs;

	char word [80] = {'\0'};

	// INSERTING
	///////////////////////////////////////////////////////////////////////
	ifs.open ("pat.txt");

	//get word out of file		
	while (!ifs.eof ())
	{
		//get word
		ifs.getline (word, 80);

		//check if blank line
		if (word [0] == '\0')
			break;
		
		cout << "inserted [";

		//insert and output if succeeds
		if (pat.Insert (word, p))
		{
			for (int i = 0; i < 80 && word [i] != '\0'; i++)
				cout << word [i];
		}
		else
		{
			cout << "--";
		}

		cout << ']' << endl;
	}

	//close to open again for search
	ifs.close ();


	// SEARCHING
	///////////////////////////////////////////////////////////////////////
	ifs.open ("pat.txt");
		
	while (!ifs.eof ())
	{
		//get word
		ifs.getline (word, 80);

		if (word [0] == '\0')
			break;

		cout << "found [";

		if (pat.Search (word))
		{
			for (int i = 0; i < 80 && word [i] != '\0'; i++)
				cout << word [i];
		}
		else
		{
			cout << "--";
		}

		cout << ']' << endl;
	}

	ifs.close ();


	// DELETING 
	///////////////////////////////////////////////////////////////////////
	ifs.open ("pat.txt");
		
	while (!ifs.eof ())
	{
		//get word
		ifs.getline (word, 80);

		if (word [0] == '\0')
			break;

		cout << "removed [";

		if (pat.Remove (word))
		{
			for (int i = 0; i < 80 && word [i] != '\0'; i++)
				cout << word [i];
		}
		else
		{
			cout << "--";
		}

		cout << ']' << endl;
	}

	ifs.close ();

	//can the temporary payload used in searching and deleting
	delete p;

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Michael Jaworski
Software Developer
United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 28 Dec 2004
Article Copyright 2004 by Michael Jaworski
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid