Exemplo n.º 1
0
        private void RemoveCurrentNode()
        {
            TreeNode selected = treeBookmarks.SelectedNode;
            string   id;

            if (selected == null)
            {
                id = null;
            }
            else
            {
                id = (string)selected.Tag;
            }

            RemoveNode?.Invoke(this, new RemoveNodeEventArgs(id));
        }
Exemplo n.º 2
0
 public void Clear()
 {
     _head  = null;
     _count = 0;
     RemoveNode?.Invoke("Work Clear Method");
 }
Exemplo n.º 3
0
        public bool Remove(T value)
        {
            BinaryTreeNode <T> current;
            BinaryTreeNode <T> parent;

            // Search the NODE for removal.

            current = FindWithParent(value, out parent);

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

            _count--;

            // The first option: the deleted node does not have a right descendant.

            if (current.Right == null)
            {
                // Remove the root
                if (parent == null)
                {
                    _head = current.Left;
                    RemoveNode?.Invoke("The head changed");
                }
                else
                {
                    int result = parent.CompareTo(current.Value);

                    if (result > 0)
                    {
                        //If the value of the parent node is greater
                        //than the value of the node being deleted,
                        //make the left descendant of the current node
                        // to the left descendant of the parent node.

                        parent.Left = current.Left;
                        RemoveNode?.Invoke("make the left descendant of the current node to the left descendant of the parent node");
                    }

                    else if (result < 0)
                    {
                        // If the value of the parent node is less than the value
                        //of the node being deleted, make the left descendant
                        //of the current node the right descendant of the
                        //parent node.

                        parent.Right = current.Left;
                        RemoveNode?.Invoke("make the left descendant of the current node to the right descendant of the parent node");
                    }
                }
            }
            // The second option: the deleted node has a right descendant, which has no left descendant.
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                // Remove the root
                if (parent == null)
                {
                    _head = current.Right;
                    RemoveNode?.Invoke("The head changed");
                }

                else
                {
                    int result = parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        // If the value of the parent node is greater than the value of the node being deleted -
                        // make the right descendant of the current node the left descendant of the parent node.

                        parent.Left = current.Right;
                        RemoveNode?.Invoke("make the right descendant of the current node the left descendant of the parent node");
                    }
                    else if (result < 0)
                    {
                        // If the value of the parent node is less than the value of the node being deleted -
                        // make the right descendant of the current node the right descendant of the parent node.

                        parent.Right = current.Right;
                        RemoveNode?.Invoke("make the right descendant of the current node the right descendant of the parent node");
                    }
                }
            }
            // The third option: the deleted node has a right descendant that has a left descendant.
            else
            {
                BinaryTreeNode <T> leftmostParent = current.Right;
                BinaryTreeNode <T> leftmost       = current.Right.Left;

                // search for the leftmost descendant
                while (leftmost.Left != null)

                {
                    leftmostParent = leftmost;
                    leftmost       = leftmost.Left;
                }


                // The right subtree of the leftmost node becomes the left subtree of its parent node.
                leftmostParent.Left = leftmost.Right;
                RemoveNode?.Invoke("The right subtree of the leftmost node becomes the left subtree of its parent node");

                // Assign the leftmost node as the left descendant - the left descendant of the deleted node,
                // and as the right descendant is the right descendant of the node to be deleted.

                leftmost.Left  = current.Left;
                leftmost.Right = current.Right;
                RemoveNode?.Invoke("Assign the leftmost node as the left descendant - the left descendant of the deleted node," +
                                   "and as the right descendant is the right descendant of the node to be deleted. ");
                if (parent == null)
                {
                    _head = leftmost;
                    RemoveNode?.Invoke("The head changed");
                }
                else

                {
                    int result = parent.CompareTo(current.Value);

                    if (result > 0)
                    {
                        // If the value of the parent node (parent) is greater than the value of the node being deleted (current) -
                        // make the leftmost descendant of the deleted node (leftmost) - the left descendant of its parent (parent).

                        parent.Left = leftmost;
                        RemoveNode?.Invoke("make the leftmost descendant of the deleted node (leftmost) - the left descendant of its parent (parent).");
                    }

                    else if (result < 0)

                    {
                        // If the value of the parent node (parent) is less than the value of the node being deleted (current) -
                        // make the leftmost descendant of the deleted node (leftmost) - the right descendant of its parent (parent).

                        parent.Right = leftmost;
                        RemoveNode?.Invoke("make the leftmost descendant of the deleted node(leftmost) -the right descendant of its parent(parent).");
                    }
                }
            }

            return(true);
        }