Ejemplo n.º 1
0
 public TernaryTreeNode(TernaryTreeNode parent, char value, bool isFinal)
 {
     this.Parent      = parent;
     this.Value       = value;
     this.FinalLetter = isFinal;
     childs           = new TernaryTreeNode[3];
 }
Ejemplo n.º 2
0
        void WordInsertion(string word)
        {
            int             index       = 0;
            TernaryTreeNode currentNode = Root;

            while (index < word.Length)
            {
                currentNode = ChooseNode(currentNode, word, ref index);
            }
        }
Ejemplo n.º 3
0
        void InsertInTree(TernaryTreeNode currentNode, string word, ref int currentIndex)
        {
            int length = word.Length;

            currentIndex++;
            var currNode = currentNode;

            for (int i = currentIndex; i < length; i++)
            {
                currNode = currNode.AddMiddleChild(word[i], word.Length == currentIndex + 1);
            }

            currentIndex = length;
        }
Ejemplo n.º 4
0
        public void Insert(string word)
        {
            if (string.IsNullOrWhiteSpace(word))
            {
                throw new Exception("Inputted value is empty");
            }

            if (Root == null)
            {
                Root = new TernaryTreeNode(null, word[0], word.Length == 1);
            }

            WordInsertion(word);
        }
Ejemplo n.º 5
0
        TernaryTreeNode ChooseNode(TernaryTreeNode currentNode, string word, ref int index)
        {
            //Center Branch
            if (word[index] == currentNode.Value)
            {
                index++;

                if (currentNode.GetMiddleChild == null)
                {
                    InsertInTree(currentNode.AddMiddleChild(word[index], word.Length == index + 1), word, ref index);
                }


                return(currentNode.GetMiddleChild);
            }
            //Right Branch
            else if (word[index] > currentNode.Value)
            {
                if (currentNode.GetRightChild == null)
                {
                    InsertInTree(currentNode.AddRightChild(word[index], word.Length == index + 1), word, ref index);
                }

                return(currentNode.GetRightChild);
            }
            //Left Branch
            else
            {
                if (currentNode.GetLeftChild == null)
                {
                    InsertInTree(currentNode.AddLeftChild(word[index], word.Length == index + 1), word, ref index);
                }

                return(currentNode.GetLeftChild);
            }
        }
Ejemplo n.º 6
0
 public virtual TernaryTreeNode AddMiddleChild(char value, bool isFinal)
 {
     childs[1] = new TernaryTreeNode(this, value, isFinal);
     return(childs[1]);
 }
Ejemplo n.º 7
0
 public virtual TernaryTreeNode AddRightChild(char value, bool isFinal)
 {
     childs[2] = new TernaryTreeNode(this, value, isFinal);
     return(childs[2]);
 }
Ejemplo n.º 8
0
 public TernaryTreeNode(TernaryTreeNode parent)
 {
     this.Parent = parent;
     childs      = new TernaryTreeNode[3];
 }