示例#1
0
        public TernarySearchTreeNode InsertRecursive(string key, TernarySearchTreeNode nodeToInsert, int index)
        {
            var keyChar = key[index];

            if (nodeToInsert == null)
            {
                nodeToInsert = new TernarySearchTreeNode(keyChar);
            }

            if (keyChar < nodeToInsert.Key)
            {
                nodeToInsert.SetLeftNode(InsertRecursive(key, nodeToInsert.LeftNode, index));
            }
            else if (keyChar == nodeToInsert.Key)
            {
                if (index < key.Length - 1)
                {
                    nodeToInsert.SetEqualNode(InsertRecursive(key, nodeToInsert.EqualNode, ++index));
                }
                else
                {
                    nodeToInsert.SetAsEndOfWord();
                }
            }
            else
            {
                nodeToInsert.SetRightNode(InsertRecursive(key, nodeToInsert.RightNode, index));
            }

            return(nodeToInsert);
        }
示例#2
0
        public void TestTernarySearchTreeNode()
        {
            var root = new TernarySearchTreeNode();

            Assert.AreEqual(1, root.NodeCount);
            Assert.AreEqual(0, root.WordCount);
            Assert.IsFalse(root.Contains(""));
            Assert.IsFalse(root.Contains("a"));
            root.Add("a");
            Assert.AreEqual(1, root.NodeCount);
            Assert.AreEqual(1, root.WordCount);
            Assert.IsFalse(root.Contains(""));
            Assert.IsTrue(root.Contains("a"));
            root.Add("ab");
            Assert.AreEqual(2, root.NodeCount);
            Assert.AreEqual(2, root.WordCount);
            Assert.IsFalse(root.Contains(""));
            Assert.IsTrue(root.Contains("a"));
            Assert.IsFalse(root.Contains("ba"));
            Assert.IsTrue(root.Contains("ab"));
            root.Add("ac");
            Assert.AreEqual(3, root.NodeCount);
            Assert.AreEqual(3, root.WordCount);
            Assert.IsFalse(root.Contains(""));
            Assert.IsTrue(root.Contains("a"));
            Assert.IsTrue(root.Contains("ab"));
            Assert.IsTrue(root.Contains("ac"));
            root.Add("fgh");
            Assert.AreEqual(6, root.NodeCount);
            Assert.AreEqual(4, root.WordCount);
            Assert.IsTrue(root.Contains("a"));
            Assert.IsTrue(root.Contains("ab"));
            Assert.IsTrue(root.Contains("fgh"));
            root.Delete("fgh");
            Assert.AreEqual(3, root.NodeCount);
            Assert.AreEqual(3, root.WordCount);
            root.Delete("ab");
            //Assert.AreEqual(2, root.NodeCount);
            Assert.AreEqual(2, root.WordCount);
            root.Delete("a");
            //Assert.AreEqual(3, root.NodeCount);
            Assert.AreEqual(1, root.WordCount);
        }
示例#3
0
        /*
         * public TernarySearchTreeNode Insert(string key)
         * {
         *  var currentNode = RootNode;
         *
         *  for (var i = 0; i < key.Length;)
         *  {
         *      var keyChar = key[i];
         *
         *      if (RootNode == null)
         *      {
         *          currentNode = RootNode = new TernarySearchTreeNode(keyChar);
         *          i++;
         *          continue;
         *      }
         *
         *      if (currentNode.EqualNode == null)
         *      {
         *          currentNode = currentNode.SetEqualNode(keyChar);
         *          i++;
         *      }
         *      else
         *      {
         *          if (keyChar == currentNode.EqualNode.Key)
         *          {
         *              currentNode = currentNode.EqualNode;
         *              i++;
         *          }
         *          else if (keyChar < currentNode.EqualNode.Key)
         *          {
         *              if (currentNode.EqualNode.LeftNode == null) currentNode.EqualNode.SetLeftNode(keyChar);
         *              currentNode = currentNode.EqualNode.LeftNode;
         *              if (currentNode.Key == keyChar) i++;
         *          }
         *          else if (keyChar > currentNode.EqualNode.Key)
         *          {
         *              if (currentNode.EqualNode.RightNode == null) currentNode.EqualNode.SetRightNode(keyChar);
         *              currentNode = currentNode.EqualNode.RightNode;
         *              if (currentNode.Key == keyChar) i++;
         *          }
         *      }
         *  }
         *
         *  if (currentNode.IsEndOfWord) throw new ArgumentException("Key is already exist");
         *  currentNode.SetAsEndOfWord();
         *  return currentNode;
         * }*/

        public TernarySearchTreeNode Insert(string key)
        {
            RootNode = InsertRecursive(key, RootNode, 0);
            return(RootNode);
        }
示例#4
0
 public TernarySearchTreeNode SetRightNode(char key) => RightNode = new TernarySearchTreeNode(key);
示例#5
0
 public TernarySearchTreeNode SetRightNode(TernarySearchTreeNode node) => EqualNode = node;
示例#6
0
 public TernarySearchTreeNode SetEqualNode(char key) => EqualNode = new TernarySearchTreeNode(key);
示例#7
0
 public TernarySearchTreeNode SetLeftNode(char key) => LeftNode = new TernarySearchTreeNode(key);
示例#8
0
 public TernarySearchTreeNode SetLeftNode(TernarySearchTreeNode node) => LeftNode = node;