Click here to Skip to main content
15,881,624 members
Articles / Programming Languages / C#

Phone Directory Implementation Using TRIE

Rate me:
Please Sign up or sign in to vote.
4.88/5 (21 votes)
15 Mar 20073 min read 123.1K   4.7K   30  
Phone Directory Implementation Using TRIE data structure.
using System;
using System.Collections;
using System.Text;
using System.CodeDom;
using PhoneDirectory;
using System.IO;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            //Root Node
            Node root = new Node("", "");
            
            //Loads Names
            TextReader reader;
            string textLine;

            try
            {
                reader = new StreamReader("names.txt");

                while ((textLine = reader.ReadLine()) != null)
                {
                    root = Trie.InsertNode(textLine, root);
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Opening File: " + ex.Message );
            }

            Console.WriteLine("Enter Option");
            Console.WriteLine("-------------------------------------");
            Console.WriteLine("1. Depth First on Trie");
            Console.WriteLine("2. Breadth First on Trie");
            Console.WriteLine("3. Find a Name on the Directory");
            Console.WriteLine("4. Directory Lookup");
            Console.WriteLine("5. Exit");
            Console.WriteLine("--------------------------------------");
            Console.Write("Enter Option:");
            int option = int.Parse(Console.ReadLine());

            switch (option)
            {
                case 1:
                    IEnumerator iterator = root.GetDepthNodes();

                    while (iterator.MoveNext())
                    {
                        Node n = (Node)iterator.Current;
                        Console.WriteLine(n.Value);
                    }
                    break;

                case 2:
                    IEnumerator iterator2 = root.GetBreadthNodes();

                    while (iterator2.MoveNext())
                    {
                        Node n = (Node)iterator2.Current;
                        Console.WriteLine(n.Value);
                    }
                    break;

                case 3:
                    Console.Write("Enter Name to Find(Case Sensitive):");
                    string name = Console.ReadLine();
                    bool fnd = Trie.Find(root, name);

                    if(fnd)
                        Console.WriteLine("Found");
                    else
                        Console.WriteLine("Not Found");
                    break;

                case 4:
                    Console.Write("Enter Name to Lookup(Case Sensitive):");
                    string name2 = Console.ReadLine();
                    Trie.GetChildrenStartingFromKey(root, name2);
                    break;

                //case 53:
                   // break;

            }

            Console.WriteLine("--------------------------------------");
            Console.WriteLine("Press any key to exit!");
            Console.ReadLine();
        }
    }
}

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions