Click here to Skip to main content
12,633,998 members (21,937 online)
Click here to Skip to main content

Stats

77.9K views
3.5K downloads
29 bookmarked
Posted

Phone Directory Implementation Using TRIE

, 15 Mar 2007
Phone Directory Implementation Using TRIE data structure.
Program
bin
Debug
Program.exe
Program.pdb
Program.vshost.exe
Trie.dll
Trie.pdb
obj
Debug
Program.exe
Program.pdb
ResolveAssemblyReference.cache
TempPE
Program.csproj.user
Properties
Trie
bin
Debug
Trie.dll
Trie.pdb
obj
Debug
TempPE
Trie.dll
Trie.pdb
Properties
using System;
using System.Collections;
using System.Text;

namespace PhoneDirectory
{
    //Represents a tree node. The node could be a root or a child.
    public class Node
    {
        //Children
        NodeCollection _children = null;
        
        //Parent
        Node _parent = null;
        
        //value of each node
        string _value = "";

        //value of each node
        string _key = "";

        //is terminal node
        bool _isTerminal = false;

        //Constructor
        public Node(string  key, string value)
        {
            //creates an empty collection
            _children = new NodeCollection(this);
            _key = key;
            _value = value;
        }
        
        //Gets the parent node
        public Node Parent
        {
            get
            {
                return _parent;
            }
            internal set
            {
                _parent = value;
            }
        }
        
        //Gets the children
        public NodeCollection Children
        {
            get
            {
                return _children;
            }
        }
        
        //Gets the root node
        public Node Root
        {
            get
            {
                if (null == _parent) return this;
                return _parent.Root;
            }
        }
        
        //Determins if this node is ancestor of the given node
        public bool IsParentOfNode(Node node)
        {
            if (_children.Contains(node)) return true;
            foreach (Node childNode in _children)
                if (childNode.IsParentOfNode(node)) return true;
            return false;
        }

        //Determins if this node is descendant of the given node
        public bool IsChildOfNode(Node node)
        {
            if (null == _parent) return false;
            if (node == _parent) return true;
            return _parent.IsChildOfNode(node);
        }

        //determines if this node shares hierarchy with given node
        public bool IsParentOrChild(Node node)
        {
            if (node == this) return true;
            if (this.IsParentOfNode(node)) return true;
            if (this.IsChildOfNode(node)) return true;
            return false;
        }
        
        //Gets the node value
        public string Value
        {
            get
            {
                return _value;
            }
        }

        //Gets the node value
        public bool IsTerminal
        {
            get
            {
                return _isTerminal;
            }

            set
            {
                _isTerminal = value;
            }
        }

        //Gets the node value
        public string Key
        {
            get
            {
                return _key;
            }
        }
        
        //iterates depth first, returns all the keys
        public IEnumerator GetDepthKeys()
        {
            yield return _key; // _value;

            foreach (Node child in _children)
            {
                IEnumerator childEnumerator = child.GetDepthKeys();
                while (childEnumerator.MoveNext())
                    yield return childEnumerator.Current;
            }
        }
        
        //iterate breadth first
        public IEnumerator GetBreadthNodes()
        {
            System.Collections.Generic.Queue<Node> que = new System.Collections.Generic.Queue<Node>();
            que.Enqueue(this);
            while (0 < que.Count)
            {
                Node node = que.Dequeue();
                foreach (Node child in node._children)
                    que.Enqueue(child);
                yield return node; // _value;
            }
        }

        //iterates depth first, returns all the nodes
        public System.Collections.IEnumerator GetDepthNodes()
        {
            yield return this;

            foreach (Node child in _children)
            {
                IEnumerator childEnumerator = child.GetDepthNodes();
                while (childEnumerator.MoveNext())
                    yield return childEnumerator.Current;
            }

       }

        
    }      
}

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

Madhu Rajagopalan
United States United States
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 15 Mar 2007
Article Copyright 2007 by Madhu Rajagopalan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid