예제 #1
0
        public void Add(T value)
        {
            var node = new BinTreeNode <T>(value);

            if (root == null)
            {
                root = node;
            }
            else
            {
                BinTreeNode <T> current = root;
                BinTreeNode <T> parent  = null;

                while (current != null)
                {
                    parent = current;
                    if (comparer.Compare(value, current.Value) < 0)
                    {
                        current = current.Left;
                    }
                    else
                    {
                        current = current.Right;
                    }
                }

                if (comparer.Compare(value, parent.Value) < 0)
                {
                    parent.Left = node;
                }
                else
                {
                    parent.Right = node;
                }
            }
        }
예제 #2
0
        public bool Remove(T item)
        {
            if (root == null)
            {
                return(false);
            }

            BinTreeNode <T> current = root;
            BinTreeNode <T> parent  = null;

            int result = 1;

            //найдём элемент в дереве.
            do
            {
                result = comparer.Compare(item, current.Value);
                if (result < 0)
                {
                    parent  = current;
                    current = current.Left;
                }
                else if (result > 0)
                {
                    parent  = current;
                    current = current.Right;
                }
                if (current == null)
                {
                    return(false);
                }
            }while (result != 0);

            //если у элемента нет правого чилда
            if (current.Right == null)
            {
                if (current == root)
                {
                    root = current.Left;
                }
                else
                {
                    result = comparer.Compare(current.Value, parent.Value);
                    if (result < 0)
                    {
                        parent.Left = current.Left;
                    }
                    else
                    {
                        parent.Right = current.Left;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;
                if (current == root)
                {
                    root = current.Right;
                }
                else
                {
                    result = comparer.Compare(current.Value, parent.Value);
                    if (result < 0)
                    {
                        parent.Left = current.Right;
                    }
                    else
                    {
                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                BinTreeNode <T> min = current.Right.Left, prev = current.Right;
                while (min.Left != null)
                {
                    prev = min;
                    min  = min.Left;
                }
                prev.Left = min.Right;
                min.Left  = current.Left;
                min.Right = current.Right;

                if (current == root)
                {
                    root = min;
                }
                else
                {
                    result = comparer.Compare(current.Value, parent.Value);
                    if (result < 0)
                    {
                        parent.Left = min;
                    }
                    else
                    {
                        parent.Right = min;
                    }
                }
            }
            --Count;
            return(true);
        }
예제 #3
0
 public BinaryTree() : this(Comparer <T> .Default)
 {
     root = null;
 }
예제 #4
0
 public void Clear()
 {
     root  = null;
     Count = 0;
 }