Пример #1
0
 private AvlTreeNode <T> SearchRecursive(T value, AvlTreeNode <T> node)
 {
     if (node == null)
     {
         return(null);
     }
     if (node.GetValue().CompareTo(value) > 0)
     {
         return(SearchRecursive(value, node.GetLeft()));
     }
     if (node.GetValue().CompareTo(value) < 0)
     {
         return(SearchRecursive(value, node.GetRight()));
     }
     return(node);
 }
Пример #2
0
        private AvlTreeNode <T> DeleteRecursive(T value, AvlTreeNode <T> node)
        {
            if (node == null)
            {
                return(null);
            }
            if (node.GetValue().CompareTo(value) > 0)
            {
                node.SetLeft(DeleteRecursive(value, node.GetLeft()));
            }
            else if (node.GetValue().CompareTo(value) < 0)
            {
                node.SetRight(DeleteRecursive(value, node.GetRight()));
            }
            else if (node.GetLeft() != null && node.GetRight() != null)
            {
                T replace = FindMin(node.GetRight()).GetValue();

                node.SetValue(replace);
                node.SetRight(DeleteRecursive(replace, node.GetRight()));
            }
            else
            {
                if (node == _root)
                {
                    node  = (node.GetLeft() == null) ? node.GetRight() : node.GetLeft();
                    _root = node;
                }
                else
                {
                    node = (node.GetLeft() == null) ? node.GetRight() : node.GetLeft();
                }
            }

            node = Balance(node);
            return(node);
        }