Exemplo n.º 1
0
 public void Remove(T data)
 {
     try
     {
         if (_head.Compare(Convert.ToInt32(data)) == 0)
         {
             if (_head.Left == null)
             {
                 _head = _head.Rigth;
             }
             else if (_head.Rigth == null)
             {
                 _head = _head.Left;
             }
             else
             {
                 var replacement = _head.Left;
                 while (replacement.Rigth != null)
                 {
                     replacement = replacement.Rigth;
                 }
                 Remove(replacement.GetData());
                 _head.SetData(replacement.GetData());
             }
         }
         else
         {
             RemoveNode(_head, null, data);
         }
     }catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Exemplo n.º 2
0
        private void RemoveNode(BinTreeNode <T> current, BinTreeNode <T> parent, T data)
        {
            if (current == null)
            {
                throw new Exception($"No node with {data}");
            }
            switch (current.Compare(Convert.ToInt32(data)))
            {
            case -1:
                RemoveNode(current.Rigth, current, data);
                break;

            case 1:
                RemoveNode(current.Left, current, data);
                break;

            case 0:
                if (current.Left == null)
                {
                    Replace(parent, current.GetData(), current.Rigth);
                }
                else if (current.Rigth == null)
                {
                    Replace(parent, current.GetData(), current.Left);
                }
                else
                {
                    var replacement = current.Left;
                    while (replacement.Rigth != null)
                    {
                        replacement = replacement.Rigth;
                    }
                    Remove(replacement.GetData());
                    current.SetData(replacement.GetData());
                }
                break;
            }
        }