Esempio n. 1
0
 public bool AddKeyValue(int key, T val)
 {
     if (Root == null)
     {
         Root = new BSTNode <T>(key, val, null);
     }
     else
     {
         BSTFind <T> foundNode = FindNodeByKey(key);
         if (foundNode.NodeHasKey == false)
         {
             BSTNode <T> newNode = new BSTNode <T>(key, val, foundNode.Node);
             if (foundNode.ToLeft)
             {
                 foundNode.Node.LeftChild = newNode;
                 newNode.Parent           = foundNode.Node;
             }
             else
             {
                 foundNode.Node.RightChild = newNode;
                 newNode.Parent            = foundNode.Node;
             }
         }
         else
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 2
0
        public bool AddKeyValue(int key, T val) // добавляем ключ-значение в дерево, return false если ключ уже есть
        {
            if (Root == null)
            {
                Root = new BSTNode <T>(key, val, null);
                return(true);
            }
            BSTFind <T> bst_find = FindNodeByKey(key);

            if (bst_find.Node != null && !bst_find.NodeHasKey)
            {
                if (bst_find.ToLeft)
                {
                    bst_find.Node.LeftChild = new BSTNode <T>(key, val, bst_find.Node);
                }
                else
                {
                    bst_find.Node.RightChild = new BSTNode <T>(key, val, bst_find.Node);
                }
                return(true);
            }
            else if (bst_find.Node == null)
            {
                if (bst_find.ToLeft)
                {
                    Root.LeftChild = new BSTNode <T>(key, val, Root);
                }
                else
                {
                    Root.RightChild = new BSTNode <T>(key, val, Root);
                }
                return(true);
            }
            return(false);
        }
        public bool AddKeyValue(int key, T val)
        {
            // добавляем ключ-значение в дерево, если корень не null
            if (Root != null)
            {
                BSTFind <T> foundNode = FindNodeByKey(key);

                if (!foundNode.NodeHasKey)
                {
                    if (foundNode.ToLeft)
                    {
                        foundNode.Node.LeftChild = new BSTNode <T>(key, val, foundNode.Node);
                    }
                    else
                    {
                        foundNode.Node.RightChild = new BSTNode <T>(key, val, foundNode.Node);
                    }

                    return(true);
                }
            }
            else
            {
                Root = new BSTNode <T>(key, val, null); // создаем корень дерева
            }

            return(false); // если ключ уже есть
        }
Esempio n. 4
0
 private BSTFind <T> FindNodeByKey(int key, BSTNode <T> node, BSTFind <T> bst_find)
 {
     bst_find.Node = node;
     if (node.NodeKey > key)
     {
         if (node.LeftChild != null)
         {
             return(FindNodeByKey(key, node.LeftChild, bst_find));
         }
         bst_find.ToLeft = true;
         return(bst_find);
     }
     else
     {
         if (node.NodeKey == key)
         {
             bst_find.NodeHasKey = true;
         }
         else if (node.RightChild != null)
         {
             return(FindNodeByKey(key, node.RightChild, bst_find));
         }
         return(bst_find);
     }
 }
Esempio n. 5
0
        public bool AddKeyValue(int key, T val)
        {
            if (Root == null)
            {
                Root = new BSTNode <T>(key, val, null);
                return(true);
            }
            BSTFind <T> bst_find = FindNodeByKey(key);

            if (bst_find.Node != null && !bst_find.NodeHasKey)
            {
                if (bst_find.ToLeft)
                {
                    bst_find.Node.LeftChild = new BSTNode <T>(key, val, bst_find.Node);
                }
                else
                {
                    bst_find.Node.RightChild = new BSTNode <T>(key, val, bst_find.Node);
                }
                return(true);
            }
            else if (bst_find.Node == null)
            {
                if (bst_find.ToLeft)
                {
                    Root.LeftChild = new BSTNode <T>(key, val, Root);
                }
                else
                {
                    Root.RightChild = new BSTNode <T>(key, val, Root);
                }
                return(true);
            }
            return(false);
        }
Esempio n. 6
0
 public bool AddKeyValue(int key, T val)
 {
     // добавляем ключ-значение в дерево
     if (Root == null)
     {
         Root = new BSTNode <T>(key, val, null);
     }
     else
     {
         BSTFind <T> foundNode = FindNodeByKey(key);
         if (foundNode.NodeHasKey == false)
         {
             BSTNode <T> newNode = new BSTNode <T>(key, val, foundNode.Node);
             if (foundNode.ToLeft == true)
             {
                 foundNode.Node.LeftChild = newNode;
                 newNode.Parent           = foundNode.Node;
             }
             else
             {
                 foundNode.Node.RightChild = newNode;
                 newNode.Parent            = foundNode.Node;
             }
         }
         else
         {
             return(false); // если ключ уже есть
         }
     }
     return(true);
 }
Esempio n. 7
0
        public BSTFind <T> FindNodeByKey(int key)
        {
            // ищем в дереве узел и сопутствующую информацию по ключу
            if (Root == null)
            {
                return(null);
            }

            BSTNode <T> selectedNode = Root;
            BSTFind <T> newSearch    = new BSTFind <T>();


            while (newSearch.NodeHasKey == true || newSearch.NodeHasKey == false)
            {
                if (key == selectedNode.NodeKey)
                {
                    newSearch.Node       = selectedNode;
                    newSearch.NodeHasKey = true;
                    return(newSearch);
                }

                if (key < selectedNode.NodeKey)
                {
                    if (selectedNode.LeftChild != null)
                    {
                        selectedNode = selectedNode.LeftChild;
                    }
                    else
                    {
                        newSearch.Node       = selectedNode;
                        newSearch.NodeHasKey = false;
                        newSearch.ToLeft     = true;
                        return(newSearch);
                    }
                }
                else
                {
                    if (selectedNode.RightChild != null)
                    {
                        selectedNode = selectedNode.RightChild;
                    }
                    else
                    {
                        newSearch.Node       = selectedNode;
                        newSearch.NodeHasKey = false;
                        newSearch.ToLeft     = false;
                        return(newSearch);
                    }
                }
            }
            return(newSearch);
        }
 public BSTFind <T> FindNodeByKey(int key) // find Node in tree with key
 {
     if (Root == null)
     {
         BSTFind <T> rezult = new BSTFind <T>();
         rezult.Node       = null;
         rezult.NodeHasKey = false;
         return(rezult);
     }
     else
     {
         if (Root.LeftChild == null && Root.RightChild == null)
         {
             if (key != Root.NodeKey)
             {
                 if (key < Root.NodeKey)
                 {
                     BSTFind <T> node = new BSTFind <T>();
                     node.Node       = null;
                     node.NodeHasKey = false;
                     node.ToLeft     = true;
                     return(node);
                 }
                 else
                 {
                     BSTFind <T> node = new BSTFind <T>();
                     node.Node       = null;
                     node.NodeHasKey = false;
                     node.ToLeft     = false;
                     return(node);
                 }
             }
             else
             {
                 BSTFind <T> node = new BSTFind <T>();
                 node.Node       = Root;
                 node.NodeHasKey = true;
                 node.ToLeft     = false;
                 return(node);
             }
         }
         else
         {
             BSTFind <T> node = Find(Root, key);
             return(node);
         }
     }
 }
Esempio n. 9
0
 public BSTFind <T> FindNodeByKey(int key) // ищем в дереве узел и сопутствующую информацию по ключу
 {
     if (Root != null)
     {
         BSTFind <T> bst_find = new BSTFind <T>();
         if (Root.LeftChild != null || Root.RightChild != null)
         {
             return(FindNodeByKey(key, Root, bst_find));
         }
         bst_find.Node       = Root;
         bst_find.NodeHasKey = Root.NodeKey == key;
         bst_find.ToLeft     = Root.NodeKey > key;
         return(bst_find);
     }
     return(null);
 }
Esempio n. 10
0
 public BSTFind <T> FindNodeByKey(int key)
 {
     if (Root != null)
     {
         BSTFind <T> bst_find = new BSTFind <T>();
         if (Root.LeftChild != null || Root.RightChild != null)
         {
             return(FindNodeByKey(key, Root, bst_find));
         }
         bst_find.Node       = Root;
         bst_find.NodeHasKey = Root.NodeKey == key;
         bst_find.ToLeft     = Root.NodeKey > key;
         return(bst_find);
     }
     return(null);
 }
 public BSTFind <T> Find(BSTNode <T> node, int key) // find node by key
 {
     if (node.NodeKey == key)
     {
         BSTFind <T> rez = new BSTFind <T>();
         rez.Node       = node;
         rez.NodeHasKey = true;
         rez.ToLeft     = false;
         return(rez);
     }
     else
     {
         if (key > node.NodeKey)
         {
             if (node.RightChild == null)
             {
                 BSTFind <T> rez = new BSTFind <T>();
                 rez.Node       = node;
                 rez.NodeHasKey = false;
                 rez.ToLeft     = false;
                 return(rez);
             }
             else
             {
                 BSTFind <T> rez = Find(node.RightChild, key);
                 return(rez);
             }
         }
         else
         {
             if (node.LeftChild == null)
             {
                 BSTFind <T> rez = new BSTFind <T>();
                 rez.Node       = node;
                 rez.NodeHasKey = false;
                 rez.ToLeft     = true;
                 return(rez);
             }
             else
             {
                 BSTFind <T> rez = Find(node.LeftChild, key);
                 return(rez);
             }
         }
     }
 }
 public BSTNode <T> FinMinMax(BSTNode <T> FromNode, bool FindMax) // find min/max in tree from someNode
 {
     if (FromNode == null)
     {
         return(null);
     }
     else
     {
         BSTFind <T> node = FindNodeByKey(FromNode.NodeKey);
         if (node.Node == null)
         {
             return(Root);
         }
         else
         {
             if (FindMax)
             {
                 if (FromNode.RightChild == null)
                 {
                     return(FromNode);
                 }
                 else
                 {
                     BSTNode <T> rez = FinMinMax(FromNode.RightChild, true);
                     return(rez);
                 }
             }
             else
             {
                 if (FromNode.LeftChild == null)
                 {
                     return(FromNode);
                 }
                 else
                 {
                     BSTNode <T> rez = FinMinMax(FromNode.LeftChild, false);
                     return(rez);
                 }
             }
         }
     }
 }
Esempio n. 13
0
        public bool DeleteNodeByKey(int key)
        {
            BSTFind <T> bst_find = FindNodeByKey(key);

            if (bst_find.NodeHasKey)
            {
                if (bst_find.Node.Equals(Root))
                {
                    Root = null;
                }
                else
                {
                    BSTNode <T> node = bst_find.Node;
                    BSTNode <T> successor;
                    if (node.RightChild != null)
                    {
                        successor = FindSuccessor(node.RightChild);
                        RemoveBounds(successor);
                        RebindChildren(node, successor);
                        ReplaceChildWith(node, successor);
                    }
                    else if (node.LeftChild != null)
                    {
                        ReplaceChildWith(node, node.LeftChild);
                    }
                    else if (node.NodeKey < node.Parent.NodeKey)
                    {
                        node.Parent.LeftChild = null;
                    }
                    else
                    {
                        node.Parent.RightChild = null;
                    }
                }
                return(true);
            }
            return(false);
        }
Esempio n. 14
0
        public bool DeleteNodeByKey(int key) // удаляем узел по ключу, return false если узел не найден
        {
            BSTFind <T> bst_find = FindNodeByKey(key);

            if (bst_find.NodeHasKey)
            {
                if (bst_find.Node.Equals(Root))
                {
                    Root = null;
                }
                else
                {
                    BSTNode <T> node = bst_find.Node;
                    BSTNode <T> inheritor;
                    if (node.RightChild != null)
                    {
                        inheritor = Find(node.RightChild);
                        Remove(inheritor);
                        RebindChild(node, inheritor);
                        ReplaceNode(node, inheritor);
                    }
                    else if (node.LeftChild != null)
                    {
                        ReplaceNode(node, node.LeftChild);
                    }
                    else if (node.NodeKey < node.Parent.NodeKey)
                    {
                        node.Parent.LeftChild = null;
                    }
                    else
                    {
                        node.Parent.RightChild = null;
                    }
                }
                return(true);
            }
            return(false);
        }
Esempio n. 15
0
        public bool AddKeyValue(int key, T val)
        {
            // добавляем ключ-значение в дерево
            BSTNode <T> Node     = Root;
            BSTFind <T> FindNode = FindNodeByKey(key);

            if (FindNode.Node != null)
            {
                if (FindNode.NodeHasKey == false)
                {
                    if (FindNode.ToLeft == true)
                    {
                        FindNode.Node.LeftChild = new BSTNode <T>(key, val, FindNode.Node);
                        return(true);
                    }
                    else
                    {
                        FindNode.Node.RightChild = new BSTNode <T>(key, val, FindNode.Node);
                        return(true);
                    }
                }
            }
            return(false);
        }
 public bool AddKeyValue(int key, T val) // add key to tree
 {
     if (Root == null)
     {
         BSTNode <T> node = new BSTNode <T>(key, val, null);
         Root = node;
         amount++;
         return(true);
     }
     else
     {
         BSTFind <T> node = FindNodeByKey(key);
         if (node.NodeHasKey)
         {
             return(false);
         }
         else
         {
             Add(Root, key, val);
             amount++;
             return(true);
         }
     }
 }
        public bool DeleteNodeByKey(int key)
        {
            // удаляем узел по ключу
            BSTFind <T> foundNode = FindNodeByKey(key);

            if (foundNode.NodeHasKey)
            {
                // удаляемый узел не имеет потомков
                if (foundNode.Node.LeftChild == null && foundNode.Node.RightChild == null)
                {
                    if (foundNode.Node.Parent.LeftChild != null && foundNode.Node.Parent.LeftChild.Equals(foundNode.Node))
                    {
                        foundNode.Node.Parent.LeftChild = null;
                    }
                    else if (foundNode.Node.Parent.RightChild != null && foundNode.Node.Parent.RightChild.Equals(foundNode.Node))
                    {
                        foundNode.Node.Parent.RightChild = null;
                    }
                }
                // удаляемый узел имеет только одного потомка
                else if (foundNode.Node.LeftChild == null ^ foundNode.Node.RightChild == null)
                {
                    if (foundNode.Node.LeftChild != null) // левый потомок привязываем к родителю удаленного узла
                    {
                        if (foundNode.Node.Parent.LeftChild != null && foundNode.Node.Parent.LeftChild.Equals(foundNode.Node))
                        {
                            foundNode.Node.Parent.LeftChild = foundNode.Node.LeftChild;
                        }
                        else
                        {
                            foundNode.Node.Parent.RightChild = foundNode.Node.LeftChild;
                        }

                        foundNode.Node.LeftChild.Parent = foundNode.Node.Parent;
                    }
                    else // правый потомок привязываем к родителю удаленного узла
                    {
                        if (foundNode.Node.Parent.LeftChild != null && foundNode.Node.Parent.LeftChild.Equals(foundNode.Node))
                        {
                            foundNode.Node.Parent.LeftChild = foundNode.Node.RightChild;
                        }
                        else
                        {
                            foundNode.Node.Parent.RightChild = foundNode.Node.RightChild;
                        }

                        foundNode.Node.RightChild.Parent = foundNode.Node.Parent;
                    }
                }
                // удаляемый узел имеет двух потомков
                else
                {
                    BSTNode <T> successorNode = FinMinMax(foundNode.Node.RightChild, false); // наименьший потомок, который больше удаляемого узла

                    if (successorNode.RightChild != null)                                    // если наименьший потомок не является листом
                    {
                        successorNode.Parent.LeftChild  = successorNode.RightChild;          // передаем его правого потомка левым узлом родителю
                        successorNode.RightChild.Parent = successorNode.Parent;              // правому потомку назначаем родителя
                    }
                    else
                    {
                        if (successorNode.Parent.LeftChild == successorNode)
                        {
                            successorNode.Parent.LeftChild = null; // удаляем левый лист
                        }
                        else
                        {
                            successorNode.Parent.RightChild = null; // удаляем правый лист
                        }
                    }
                    // преемник замещает удаленный узел
                    if (foundNode.Node.Parent.RightChild == foundNode.Node)
                    {
                        foundNode.Node.Parent.RightChild = successorNode;
                    }
                    else
                    {
                        foundNode.Node.Parent.LeftChild = successorNode;
                    }

                    successorNode.Parent = foundNode.Node.Parent;         // новый родитель для узла-преемника

                    successorNode.LeftChild  = foundNode.Node.LeftChild;  // левый потомок удаленного узла становится потомком узла-преемника
                    successorNode.RightChild = foundNode.Node.RightChild; // правый потомок удаленного узла становится потомком узла-преемника

                    // связать потомков удаленного узла с новым родителем
                    if (foundNode.Node.RightChild != null)
                    {
                        foundNode.Node.RightChild.Parent = successorNode;
                    }
                    if (foundNode.Node.LeftChild != null)
                    {
                        foundNode.Node.LeftChild.Parent = successorNode;
                    }
                }

                return(true);
            }

            return(false); // если узел не найден
        }
Esempio n. 18
0
        public bool DeleteNodeByKey(int key)
        {
            // удаляем узел по ключу
            BSTFind <T> foundNode = FindNodeByKey(key);
            BSTNode <T> Node      = foundNode.Node;
            BSTNode <T> successorNode;


            if (foundNode.NodeHasKey == true)
            {
                if (Node.LeftChild != null && Node.RightChild != null) // CASE (node has R & L children)
                {
                    if (Node == Root)
                    {
                        successorNode = FinMinMax(Node.RightChild, false); // searching for a successor
                        if (GetAllNodes(successorNode).Count == 1)         // if successor have 0 children
                        {
                            Node.RightChild.Parent  = null;
                            Node.LeftChild.Parent   = successorNode;
                            successorNode.LeftChild = Node.LeftChild;
                            Root = Node.RightChild;
                        }
                        else
                        {
                            if (successorNode != Node.RightChild)
                            {
                                successorNode.Parent.LeftChild = null;
                                successorNode.Parent           = null;

                                Root.LeftChild.Parent   = successorNode;
                                successorNode.LeftChild = Root.LeftChild;

                                Root.RightChild.Parent   = successorNode;
                                successorNode.RightChild = Root.RightChild;
                                Root = successorNode;
                            }
                            else
                            {
                                Root.LeftChild.Parent   = successorNode;
                                successorNode.LeftChild = Root.LeftChild;

                                Root.RightChild.Parent = null;
                                Root = successorNode;
                            }
                        }
                    }
                    else
                    {
                        successorNode = FinMinMax(Node.RightChild, false); // searching for a successor

                        if (successorNode != Node.RightChild)
                        {
                            successorNode.Parent.LeftChild = null;
                            successorNode.Parent           = Node.Parent;

                            if (Node.Parent.LeftChild == Node)
                            {
                                Node.Parent.LeftChild = successorNode;
                            }
                            else
                            {
                                Node.Parent.RightChild = successorNode;
                            }

                            if (Node.LeftChild != null)
                            {
                                Node.LeftChild.Parent   = successorNode;
                                successorNode.LeftChild = Node.LeftChild;
                            }
                            if (Node.RightChild != null)
                            {
                                Node.RightChild.Parent   = successorNode;
                                successorNode.RightChild = Node.RightChild;
                            }
                        }
                        else
                        {
                            successorNode.Parent = Node.Parent;

                            if (Node.Parent.LeftChild == Node)
                            {
                                Node.Parent.LeftChild = successorNode;
                            }
                            else
                            {
                                Node.Parent.RightChild = successorNode;
                            }

                            successorNode.LeftChild = Node.LeftChild;
                            Node.LeftChild.Parent   = successorNode;
                        }
                    }
                }
                else // CASE (node has R/L || 0 children)
                {
                    if (Node == Root)
                    {
                        if (GetAllNodes(Root).Count == 1) // CASE (node = root && has 0 children)
                        {
                            Root = null;
                        }
                        else // CASE (node = root && has 1 child)
                        {
                            if (Node.LeftChild != null)
                            {
                                Node.LeftChild.Parent = null;
                                Root = Node.LeftChild;
                            }
                            else
                            {
                                Node.RightChild.Parent = null;
                                Root = Node.RightChild;
                            }
                        }
                    }
                    else // CASE (node has 1 child)
                    {
                        if (Node.LeftChild == null && Node.RightChild == null)
                        {
                            if (Node.Parent.LeftChild == Node)
                            {
                                Node.Parent.LeftChild = null;
                            }
                            else
                            {
                                Node.Parent.RightChild = null;
                            }
                            Node.Parent = null;
                        }

                        else if (Node.LeftChild != null)
                        {
                            Node.LeftChild.Parent = Node.Parent;

                            if (Node.Parent.LeftChild == Node)
                            {
                                Node.Parent.LeftChild = Node.LeftChild;
                            }
                            else
                            {
                                Node.Parent.RightChild = Node.RightChild;
                            }
                        }
                        else
                        {
                            Node.RightChild.Parent = Node.Parent;

                            if (Node.Parent.LeftChild == Node)
                            {
                                Node.Parent.LeftChild = Node.LeftChild;
                            }
                            else
                            {
                                Node.Parent.RightChild = Node.RightChild;
                            }
                        }
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 19
0
        public BSTFind <T> FindNodeByKey(int key)
        {
            // ищем в дереве узел и сопутствующую информацию по ключу

            BSTFind <T> found = new BSTFind <T>();
            BSTNode <T> Node  = Root;

            if (Node.NodeKey == key && Node.LeftChild == null && Node.RightChild == null)
            {
                found.Node       = null;
                found.NodeHasKey = true;
            }
            else
            {
                if (Node.LeftChild == null && Node.RightChild == null)
                {
                    if (key > Node.NodeKey)
                    {
                        found.Node       = Node;
                        found.NodeHasKey = false;
                        found.ToLeft     = false;
                    }
                    else
                    {
                        found.Node       = Node;
                        found.NodeHasKey = false;
                        found.ToLeft     = true;
                    }
                    return(found);
                }
                while (Node.LeftChild != null || Node.RightChild != null)
                {
                    if (Node.LeftChild != null && Node.RightChild != null)
                    {
                        if (key == Node.NodeKey)
                        {
                            found.Node       = Node;
                            found.NodeHasKey = true;
                            break;
                        }
                        else if (key > Node.NodeKey)
                        {
                            Node = Node.RightChild;
                        }
                        else
                        {
                            Node = Node.LeftChild;
                        }
                    }
                    else if (Node.LeftChild != null && Node.RightChild == null)
                    {
                        if (key == Node.NodeKey)
                        {
                            found.Node       = Node;
                            found.NodeHasKey = true;
                            return(found);
                        }
                        else if (key > Node.NodeKey)
                        {
                            found.Node       = Node;
                            found.NodeHasKey = false;
                            found.ToLeft     = false;
                            return(found);
                        }
                        else
                        {
                            Node = Node.LeftChild;
                        }
                    }
                    else if (Node.LeftChild == null && Node.RightChild != null)
                    {
                        if (key == Node.NodeKey)
                        {
                            found.Node       = Node;
                            found.NodeHasKey = true;
                            return(found);
                        }
                        else if (key > Node.NodeKey)
                        {
                            Node = Node.RightChild;
                        }
                        else
                        {
                            found.Node       = Node;
                            found.NodeHasKey = false;
                            found.ToLeft     = true;
                            return(found);
                        }
                    }
                }
                if (Node.LeftChild == null && Node.RightChild == null)
                {
                    if (key > Node.NodeKey)
                    {
                        found.Node       = Node;
                        found.NodeHasKey = false;
                        found.ToLeft     = false;
                    }
                    else
                    {
                        found.Node       = Node;
                        found.NodeHasKey = false;
                        found.ToLeft     = true;
                    }
                    return(found);
                }
                else if (Node.LeftChild != null && Node.RightChild == null)
                {
                    if (key == Node.NodeKey)
                    {
                        found.Node       = Node;
                        found.NodeHasKey = true;
                    }
                    else if (key > Node.NodeKey)
                    {
                        found.Node       = Node;
                        found.NodeHasKey = false;
                        found.ToLeft     = false;
                    }
                }
                else if (Node.LeftChild == null && Node.RightChild != null)
                {
                    if (key == Node.NodeKey)
                    {
                        found.Node       = Node;
                        found.NodeHasKey = true;
                    }
                    else if (key > Node.NodeKey)
                    {
                    }
                    else
                    {
                        found.Node       = Node;
                        found.NodeHasKey = false;
                        found.ToLeft     = true;
                    }
                }
            }

            return(found);
        }
        public bool DeleteNodeByKey(int key) // delete Node from tree
        {
            BSTFind <T> findnode = FindNodeByKey(key);

            if (!findnode.NodeHasKey)
            {
                return(false);
            }
            else
            {
                if (findnode.Node.LeftChild == null && findnode.Node.RightChild == null) // когда лист
                {
                    if (findnode.Node.Parent == null)
                    {
                        findnode.Node = null;
                        Root          = null;
                        amount--;
                        return(true);
                    }
                    else
                    {
                        if (findnode.Node.NodeKey < findnode.Node.Parent.NodeKey)
                        {
                            findnode.Node.Parent.LeftChild = null;
                            amount--;
                            return(true);
                        }
                        else
                        {
                            findnode.Node.Parent.RightChild = null;
                            amount--;
                            return(true);
                        }
                    }
                }
                else
                {
                    if (findnode.Node.LeftChild == null && findnode.Node.RightChild != null) // когда только правый потомок
                    {
                        if (findnode.Node.Parent == null)
                        {
                            Root = Root.RightChild;
                            amount--;
                            return(true);
                        }
                        else
                        {
                            if (findnode.Node.NodeKey < findnode.Node.Parent.NodeKey)
                            {
                                findnode.Node.RightChild.Parent = findnode.Node.Parent;
                                findnode.Node.Parent.LeftChild  = findnode.Node.RightChild;
                                amount--;
                                return(true);
                            }
                            else
                            {
                                findnode.Node.RightChild.Parent = findnode.Node.Parent;
                                findnode.Node.Parent.RightChild = findnode.Node.RightChild;
                                amount--;
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        if (findnode.Node.LeftChild != null && findnode.Node.RightChild == null)
                        {
                            if (findnode.Node.Parent == null)
                            {
                                Root = Root.LeftChild;
                                amount--;
                                return(true);
                            }
                            else
                            {
                                if (findnode.Node.NodeKey < findnode.Node.Parent.NodeKey)
                                {
                                    findnode.Node.LeftChild.Parent = findnode.Node.Parent;
                                    findnode.Node.Parent.LeftChild = findnode.Node.LeftChild;
                                    amount--;
                                    return(true);
                                }
                                else
                                {
                                    findnode.Node.LeftChild.Parent  = findnode.Node.Parent;
                                    findnode.Node.Parent.RightChild = findnode.Node.LeftChild;
                                    amount--;
                                    return(true);
                                }
                            }
                        }
                        else // when both children are not null
                        {
                            BSTNode <T> replace = leftTravel(findnode.Node.RightChild);
                            if (findnode.Node.Parent == null)
                            {
                                Root = replace;
                                amount--;
                                return(true);
                            }
                            else
                            {
                                if (findnode.Node.NodeKey < findnode.Node.Parent.NodeKey)
                                {
                                    replace.Parent = findnode.Node.Parent;
                                    findnode.Node.Parent.LeftChild = replace;
                                    amount--;
                                    return(true);
                                }
                                else
                                {
                                    replace.Parent = findnode.Node.Parent;
                                    findnode.Node.Parent.RightChild = replace;
                                    amount--;
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }
        }