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

Phone Directory Implementation Using TRIE

, 15 Mar 2007
Phone Directory Implementation Using TRIE data structure.
article_demozip.zip
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
article_srczip.zip
Trie
bin
Debug
Trie.dll
Trie.pdb
obj
Debug
TempPE
Trie.dll
Trie.pdb
Properties
using System;
using System.Collections;
using System.Collections;
using System.Text;

namespace PhoneDirectory
{
    //Represents the collection of nodes
    public class NodeCollection : IEnumerable
    {
        //Internal list contains the children nodes.
        System.Collections.Generic.List<Node> _children = new System.Collections.Generic.List<Node>();

        //repewsents the owner
        Node _owner = null;

        internal NodeCollection(Node owner)
        {
            if (null == owner) throw new ArgumentNullException("Owner");
            _owner = owner;
        }

        //Adds a node into the children list
        public void Add(Node node)
        {
            //cannot readd a same node
            if (_owner.IsParentOrChild(node))
                throw new InvalidOperationException("Cannot add an ancestor or descendant.");
            _children.Add(node);
            node.Parent = _owner;
        }

        //Determins if the children nodes contains the given value
        public bool ContainsValue(string value)
        {
            foreach (Node item in _children)
            {
                if (item.Value.ToString() == value)
                    return true;
            }
            return false;
        }

        //Determins if the children nodes contain the given key
        public bool ContainsKey(string value)
        {
            foreach (Node item in _children)
            {
                if (item.Key.ToString() == value)
                    return true;
            }
            return false;
        }

        //Returns a node based on the key
        public Node GetNodeByKey(string value)
        {
            foreach (Node item in _children)
            {
                if (item.Key.ToString() == value)
                    return item;
            }
            return null;
        }

        //Returns a node based on the value
        public Node GetNodeByValue(string value)
        {
            foreach (Node item in _children)
            {
                if (item.Value.ToString() == value)
                    return item;
            }
            return null;
        }

        //Removes a node
        public void Remove(Node node)
        {
            _children.Remove(node);
            node.Parent = null;
        }

        //Determins if the children contains a node, test for equality
        public bool Contains(Node node)
        {
            return _children.Contains(node);
        }

        //Clears all the nodes
        public void Clear()
        {
            foreach (Node node in this)
                node.Parent = null;
            _children.Clear();
        }

        //Returns the count of children
        public int Count
        {
            get
            {
                return _children.Count;
            }
        }

        //Returns the owner
        public Node Owner
        {
            get
            {
                return _owner;
            }
        }

        //Returns the node based on index
        public Node this[int index]
        {
            get
            {
                return _children[index];
            }
        }

        //returns the enumerator
        public IEnumerator GetEnumerator()
        {
            return _children.GetEnumerator();
        }

        //returns the enumerator
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

    }
}

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 15 Mar 2007
Article Copyright 2007 by Madhu Rajagopalan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid