Ejemplo 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);
     }
 }
Ejemplo n.º 2
0
        private void AddNode(BinTreeNode <T> current, BinTreeNode <T> parent, T data)
        {
            if (current == null)
            {
                if (parent == null)
                {
                    _head = new BinTreeNode <T>(data);
                }
                else
                {
                    Replace(parent, data, new BinTreeNode <T>(data));
                }
            }
            else
            {
                switch (current.Compare(Convert.ToInt32(data)))
                {
                case -1:
                    AddNode(current.Rigth, current, data);
                    break;

                case 1:
                    AddNode(current.Left, current, data);
                    break;
                }
            }
        }
Ejemplo n.º 3
0
 private string SearchNode(BinTreeNode <T> current, T data)
 {
     if (current == null)
     {
         throw new Exception($"No node with {data}");
     }
     return(current.Compare(Convert.ToInt32(data)) switch
     {
         -1 => current + SearchNode(current.Rigth, data),
         1 => current + SearchNode(current.Left, data),
         0 => current + " ",
         _ => throw new Exception("Compare error")
     });
Ejemplo n.º 4
0
        private static void Replace(BinTreeNode <T> parent, T target, BinTreeNode <T> replacement)
        {
            switch (parent.Compare(Convert.ToInt32(target)))
            {
            case -1:
                parent.Rigth = replacement;
                break;

            case 1:
                parent.Left = replacement;
                break;
            }
        }
Ejemplo n.º 5
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;
            }
        }