예제 #1
0
        private bool Search2(TwoThreeNode <T> node, T value)
        {
            if (node.Val1.CompareTo(value) == 0)
            {
                return(true);
            }

            if (node.Val1.CompareTo(value) > 0)
            {
                if (node.Left != null)
                {
                    return(Search(node.Left, value));
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (node.Right != null)
                {
                    return(Search(node.Right, value));
                }
                else
                {
                    return(false);
                }
            }
        }
예제 #2
0
        private TwoThreeNode <T> FindNode2(TwoThreeNode <T> node, T value)
        {
            if (node.Val1.CompareTo(value) == 0)
            {
                return(node);
            }

            if (node.Val1.CompareTo(value) > 0)
            {
                if (node.Left != null)
                {
                    return(FindNode(node.Left, value));
                }
                else
                {
                    return(node);
                }
            }
            else
            {
                if (node.Right != null)
                {
                    return(FindNode(node.Right, value));
                }
                else
                {
                    return(node);
                }
            }
        }
예제 #3
0
        private static bool SearchExt2(this TwoThreeTree <string> tree, TwoThreeNode <string> node, string value)
        {
            if (node.Val1.CompareTo(value) == 0 || LevenshteinDistance(node.Val1, value) == 1)
            {
                return(true);
            }

            if (node.Val1.CompareTo(value) > 0)
            {
                if (node.Left != null)
                {
                    return(SearchWithOneMistake(tree, node.Left, value));
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (node.Right != null)
                {
                    return(SearchWithOneMistake(tree, node.Right, value));
                }
                else
                {
                    return(false);
                }
            }
        }
예제 #4
0
 public TwoThreeNode(TwoThreeNode <T> left, TwoThreeNode <T> right, TwoThreeNode <T> parent, T val1)
 {
     Left   = left;
     Right  = right;
     Parent = parent;
     Val1   = val1;
     Type   = NodeType.TwoNode;
 }
예제 #5
0
        private TwoThreeNode <T> Split(TwoThreeNode <T> node)
        {
            var a = new TwoThreeNode <T>(node.Left, node.Middle1, node.Parent, node.Val1);
            var b = new TwoThreeNode <T>(node.Middle2, node.Right, node.Parent, node.Val3);
            TwoThreeNode <T> x;

            if (node.Parent == null)
            {
                x        = new TwoThreeNode <T>(a, b, null, node.Val2);
                a.Parent = x; b.Parent = x;
                return(x);
            }
            else if (node.Parent.Type == NodeType.TwoNode)
            {
                x = new TwoThreeNode <T>(NodeType.ThreeNode)
                {
                    Parent = node.Parent.Parent
                };
                a.Parent = x; b.Parent = x;
                if (node.Parent.Right == node)
                {
                    x.Left = node.Parent.Left; x.Middle1 = a; x.Right = b;
                    x.Val1 = node.Parent.Val1; x.Val2 = node.Val2;
                }
                else
                {
                    x.Left = a; x.Middle1 = b; x.Right = node.Parent.Right;
                    x.Val1 = node.Val2; x.Val2 = node.Parent.Val1;
                }
                node.Parent = x;
                return(x);
            }
            else
            {
                x = new TwoThreeNode <T>(NodeType.FourNode)
                {
                    Parent = node.Parent.Parent
                };
                a.Parent = x; b.Parent = x;
                if (node.Parent.Right == node)
                {
                    x.Left = node.Parent.Left; x.Middle1 = node.Parent.Middle1; x.Middle2 = a; x.Right = b;
                    x.Val1 = node.Parent.Val1; x.Val2 = node.Parent.Val2; x.Val3 = node.Val2;
                }
                else if (node.Parent.Left == node)
                {
                    x.Right = node.Parent.Right; x.Middle1 = b; x.Middle2 = node.Parent.Middle1; x.Left = a;
                    x.Val1  = node.Val2; x.Val2 = node.Parent.Val1; x.Val3 = node.Parent.Val2;
                }
                else
                {
                    x.Left = node.Parent.Left; x.Middle1 = a; x.Middle2 = b; x.Right = node.Parent.Right;
                    x.Val1 = node.Parent.Val1; x.Val2 = node.Val2; x.Val3 = node.Parent.Val2;
                }
                node.Parent = x;
                return(Split(x));
            }
        }
예제 #6
0
 public TwoThreeNode(TwoThreeNode <T> left, TwoThreeNode <T> middle1, TwoThreeNode <T> right, TwoThreeNode <T> parent, T val1, T val2)
 {
     Left    = left;
     Middle1 = middle1;
     Right   = right;
     Parent  = parent;
     Val1    = val1;
     Val2    = val2;
     Type    = NodeType.ThreeNode;
 }
예제 #7
0
 public TwoThreeNode(TwoThreeNode <T> left, TwoThreeNode <T> right, TwoThreeNode <T> middle1, TwoThreeNode <T> middle2, TwoThreeNode <T> parent, T val1, T val2, T val3)
 {
     Left    = left;
     Right   = right;
     Middle1 = middle1;
     Middle2 = middle2;
     Parent  = parent;
     Val1    = val1;
     Val2    = val2;
     Val3    = val3;
     Type    = NodeType.FourNode;
 }
예제 #8
0
        public bool Search(TwoThreeNode <T> node, T value)
        {
            if (node.Type == NodeType.TwoNode)
            {
                return(Search2(node, value));
            }
            else if (node.Type == NodeType.ThreeNode)
            {
                return(Search3(node, value));
            }

            return(false);
        }
예제 #9
0
        private TwoThreeNode <T> FindNode(TwoThreeNode <T> node, T value)
        {
            if (node.Type == NodeType.TwoNode)
            {
                return(FindNode2(node, value));
            }
            else if (node.Type == NodeType.ThreeNode)
            {
                return(FindNode3(node, value));
            }

            return(null);
        }
예제 #10
0
        public static bool SearchWithOneMistake(this TwoThreeTree <string> tree, TwoThreeNode <string> node, string value)
        {
            if (node.Type == NodeType.TwoNode)
            {
                return(SearchExt2(tree, node, value));
            }
            else if (node.Type == NodeType.ThreeNode)
            {
                return(SearchExt3(tree, node, value));
            }

            return(false);
        }
예제 #11
0
        private TwoThreeNode <T> CreateAThreeNode(TwoThreeNode <T> node, T value)
        {
            TwoThreeNode <T> newThreeNode;

            if (node.Val1.CompareTo(value) < 0)
            {
                newThreeNode = new TwoThreeNode <T>(node.Val1, value)
                {
                    Parent = node.Parent
                };
                if (node.Parent != null)
                {
                    if (node.Parent.Left == node)
                    {
                        node.Parent.Left = newThreeNode;
                    }
                    else if (node.Parent.Right == node)
                    {
                        node.Parent.Right = newThreeNode;
                    }
                    else
                    {
                        node.Parent.Middle1 = newThreeNode;
                    }
                }
            }
            else
            {
                newThreeNode = new TwoThreeNode <T>(value, node.Val1)
                {
                    Parent = node.Parent
                };
                if (node.Parent != null)
                {
                    if (node.Parent.Left == node)
                    {
                        node.Parent.Left = newThreeNode;
                    }
                    else if (node.Parent.Right == node)
                    {
                        node.Parent.Right = newThreeNode;
                    }
                    else
                    {
                        node.Parent.Middle1 = newThreeNode;
                    }
                }
            }
            return(newThreeNode);
        }
예제 #12
0
        public void Insert(T value)
        {
            if (Root == null)
            {
                Root = new TwoThreeNode <T>(null, null, null, value);
                return;
            }

            if (!Search(Root, value))
            {
                var node = FindNode(Root, value);
                if (node == Root && node.Type == NodeType.TwoNode)
                {
                    Root = CreateAThreeNode(node, value);
                }
                else if (node == Root && node.Type == NodeType.ThreeNode)
                {
                    Root = CreateAFourNode(node, value);
                    Root = Split(Root);
                }
                else if (node.Type == NodeType.TwoNode)
                {
                    node = CreateAThreeNode(node, value);
                }
                else if (node.Type == NodeType.ThreeNode)
                {
                    node = CreateAFourNode(node, value);
                    TwoThreeNode <T> inCase = Split(node);

                    if (inCase.Parent == null)
                    {
                        Root = inCase;
                    }
                }
            }
        }
예제 #13
0
 private TwoThreeNode <T> FindNode3(TwoThreeNode <T> node, T value)
 {
     if (node.Val1.CompareTo(value) > 0)
     {
         if (node.Left != null)
         {
             return(FindNode(node.Left, value));
         }
         else
         {
             return(node);
         }
     }
     else if (node.Val2.CompareTo(value) < 0)
     {
         if (node.Right != null)
         {
             return(FindNode(node.Right, value));
         }
         else
         {
             return(node);
         }
     }
     else
     {
         if (node.Middle1 != null)
         {
             return(FindNode(node.Middle1, value));
         }
         else
         {
             return(node);
         }
     }
 }
예제 #14
0
        public T GetMin(T minValue)
        {
            TwoThreeNode <T> node = FindNode(Root, minValue);

            return(node.Val1);
        }
예제 #15
0
 public TwoThreeTree()
 {
     Root = null;
 }