Exemple #1
0
 private bool SearchHelper(TernaryNode root, string word)
 {
     if (root == null)
     {
         return(false);
     }
     if (root.key == word[0])
     {
         if (word.Length == 1)
         {
             return(true);
         }
         else
         {
             return(SearchHelper(root.equal, word.Substring(1)));
         }
     }
     else if (word[0] < root.key)
     {
         return(SearchHelper(root.less, word));
     }
     else
     {
         return(SearchHelper(root.greater, word));
     }
 }
Exemple #2
0
 public TernaryNode(char c)
 {
     key     = c;
     less    = null;
     equal   = null;
     greater = null;
 }
Exemple #3
0
        public void Insert(string s)
        {
            TernaryNode node = InsertHelper(Root, s);

            if (Root == null)
            {
                Root = node;
            }
        }
Exemple #4
0
 public void Traversal(TernaryNode root, StringBuilder sb)
 {
     if (root != null)
     {
         Traversal(root.less, sb);
         sb.Append(root.key);
         Traversal(root.equal, sb);
         if (root.endWord)
         {
             Console.WriteLine(sb.ToString());
         }
         sb.Length--;
         Traversal(root.greater, sb);
     }
 }
Exemple #5
0
 private TernaryNode InsertHelper(TernaryNode root, string s)
 {
     if (root == null)
     {
         root = new TernaryNode(s[0]);
         if (s.Length == 1)
         {
             root.endWord = true;
             return(root);
         }
         else
         {
             root.equal = InsertHelper(root.equal, s.Substring(1));
         }
     }
     else
     {
         if (s[0].Equals(root.key))
         {
             if (s.Length == 1)
             {
                 root.endWord = true;
                 return(root);
             }
             root.equal = InsertHelper(root.equal, s.Substring(1));
         }
         else if (s[0] < (root.key))
         {
             root.less = InsertHelper(root.less, s);
         }
         else
         {
             root.greater = InsertHelper(root.greater, s);
         }
     }
     return(root);
 }
Exemple #6
0
 public TernarySearchTrees()
 {
     Root = null;
 }