Exemple #1
0
        public void AddingRecursively(Node <T> node, T data)
        {
            switch (ComparerT.Compare(node.Data, data) <= 0)
            {
            case true:
                if (node.Right == null)
                {
                    node.Right = new Node <T>(data);
                    Count++;
                    TreeEventArgs <T> args = new TreeEventArgs <T>(data, $"Element {data} was added into the tree");
                    ElementAdded?.Invoke(this, args);
                }
                else
                {
                    AddingRecursively(node.Right, data);
                }
                break;

            case false:
                if (node.Left == null)
                {
                    node.Left = new Node <T>(data);
                    Count++;
                    TreeEventArgs <T> args = new TreeEventArgs <T>(data, $"Element {data} was added into the tree");
                    ElementAdded?.Invoke(this, args);
                }
                else
                {
                    AddingRecursively(node.Left, data);
                }
                break;
            }
        }
Exemple #2
0
 /// <summary>
 /// Adds element to the tree according to comparer
 /// </summary>
 /// <param name="item">Object that should be added in tree</param>
 /// <exception cref="ArgumentNullException">Thrown if parameter was null</exception>
 public void Add(T item)
 {
     if (item is null)
     {
         throw new ArgumentNullException("item", "item can`t be null");
     }
     if (Root is null)
     {
         Root = new Node <T>(item);
         Count++;
         TreeEventArgs <T> args = new TreeEventArgs <T>(item, $"Element {item} was added into the tree(Root)");
         ElementAdded?.Invoke(this, args);
     }
     else
     {
         AddingRecursively(Root, item);
     }
 }
        /// <summary>
        /// Adds element to the tree according to comparer
        /// </summary>
        /// <param name="item">Object that should be added in tree</param>
        /// <exception cref="ArgumentNullException">Thrown if parameter was null</exception>
        public void Add(T item)
        {
            if (item is null)
            {
                throw new ArgumentNullException("item");
            }

            var node     = new BinaryTreeNode <T>(item);
            var EventArg = new TreeEventArgs <T>(item, "Item has been added");

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

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

                if (Comparer.Compare(item, parent.Value) < 0)
                {
                    parent.Left = node;
                }
                else
                {
                    parent.Right = node;
                }
            }
            Count++;
            ElementAdded?.Invoke(this, EventArg);
        }
Exemple #4
0
        /// <summary>
        /// Removes element from tree by its reference
        /// </summary>
        /// <param name="item">Object that should be removed from tree</param>
        /// <returns>True if element was deleted succesfully, false if element wasn't found in tree</returns>
        public bool Remove(T item)
        {
            Node <T> current = Root;
            Node <T> father  = null;

            while (current != null)
            {
                if (ComparerT.Compare(current.Data, item) < 0)
                {
                    father  = current;
                    current = current.Right;
                }
                else if (ComparerT.Compare(current.Data, item) > 0)
                {
                    father  = current;
                    current = current.Left;
                }
                else
                {
                    break;
                }
            }
            if (current == null)
            {
                return(false);
            }

            Count--;

            // checking if node is a leaf and deleting it
            if (current.Right == null && current.Left == null)
            {
                // checking if node to delete is root
                if (father == null)
                {
                    Root = null;
                }
                // comparing what node of children should we use for replacing
                else if (ComparerT.Compare(father.Data, current.Data) > 0)
                {
                    father.Left = null;
                }
                else
                {
                    father.Right = null;
                }
            }
            //checking if node has only left child
            else if (current.Right == null)
            {
                // checking if node to delete is root
                if (father == null)
                {
                    Root = current.Left;
                }
                else
                {
                    father.Left = current.Left;
                }
            }
            //checking if node has only right child
            else if (current.Left == null)
            {
                // checking if node to delete is root
                if (father == null)
                {
                    Root = current.Right;
                }
                else
                {
                    father.Left = current.Right;
                }
            }
            // if node has both left and right
            // we should replace node with the smallest node of right sub-tree
            else
            {
                // temporary node to replace
                Node <T> tmp = current.Right;
                if (tmp.Left == null)
                {
                    tmp.Left = current.Left;
                }
                else
                {
                    while (tmp.Left.Left != null)
                    {
                        tmp = tmp.Left;
                    }
                    // one more temporary node, we need it for saving node to replace
                    Node <T> tmp2 = tmp.Left;
                    // deleting node from this place
                    tmp.Left   = null;
                    tmp2.Left  = current.Left;
                    tmp2.Right = current.Right;
                    tmp        = tmp2;
                }
                if (father == null)
                {
                    Root = tmp;
                }
                else if (ComparerT.Compare(father.Data, current.Data) > 0)
                {
                    father.Left = tmp;
                }
                else
                {
                    father.Right = tmp;
                }
            }
            TreeEventArgs <T> args = new TreeEventArgs <T>(item, $"Element {item} was removed from tree");

            ElementRemoved?.Invoke(this, args);
            return(true);
        }
        /// <summary>
        /// Removes element from tree by its reference
        /// </summary>
        /// <param name="item">Object that should be removed from tree</param>
        /// <returns>True if element was deleted succesfully, false if element wasn't found in tree</returns>
        public bool Remove(T item)
        {
            BinaryTreeNode <T> current;

            // Find removal node
            current = FindWithParent(item, out BinaryTreeNode <T> parent);

            if (current == null)
            {
                return(false);
            }

            Count--;

            if (current.Right == null)
            {
                if (parent == null)
                {
                    root = current.Left;
                }
                else
                {
                    int result = Comparer.Compare(parent.Value, current.Value);
                    if (result > 0)
                    {
                        parent.Left = current.Left;
                    }
                    else if (result < 0)
                    {
                        parent.Right = current.Left;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left; if (parent == null)
                {
                    root = current.Right;
                }
                else
                {
                    int result = Comparer.Compare(parent.Value, current.Value);
                    if (result > 0)
                    {
                        parent.Left = current.Right;
                    }
                    else if (result < 0)
                    {
                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                BinaryTreeNode <T> leftmost       = current.Right.Left;
                BinaryTreeNode <T> leftmostParent = current.Right;
                while (leftmost.Left != null)
                {
                    leftmostParent = leftmost;
                    leftmost       = leftmost.Left;
                }

                leftmostParent.Left = leftmost.Right;

                leftmost.Left  = current.Left;
                leftmost.Right = current.Right;
                if (parent == null)
                {
                    root = leftmost;
                }
                else
                {
                    int result = Comparer.Compare(parent.Value, current.Value);
                    if (result > 0)
                    {
                        parent.Left = leftmost;
                    }
                    else if (result < 0)
                    {
                        parent.Right = leftmost;
                    }
                }
            }
            var EventArg = new TreeEventArgs <T>(item, "Item has been removed");

            ElementRemoved?.Invoke(this, EventArg);
            return(true);
        }