Exemple #1
0
        public bool Remove(T value)
        {
            BinaryTreeNode <T> parent;
            BinaryTreeNode <T> node = FindWithParent(value, out parent);

            if (node == null)
            {
                return(false);
            }
            else
            {
                Count--;
                #region case 1: When node does not have right child
                if (node.Right == null)
                {
                    //node left will replace the current node
                    if (parent == null)
                    {
                        head = node.Left;
                    }
                    else
                    {
                        var result = parent.CompareTo(node.Value);
                        if (result > 0)
                        {
                            parent.Left = node.Left;
                        }
                        else
                        {
                            parent.Right = node.Left;
                        }
                    }
                }
                #endregion

                #region case 2: When node has right child and node.right.left is null
                else if (node.Right.Left == null)
                {
                    node.Right.Left = node.Left;
                    if (parent == null)
                    {
                        head = node.Right;
                    }
                    else
                    {
                        var result = parent.CompareTo(node.Right.Value);
                        if (result > 0)
                        {
                            parent.Left = node.Right;
                        }
                        else
                        {
                            parent.Left = node.Right;
                        }
                    }
                }
                #endregion

                #region case 3: When node has right child and node.right.left is not null
                else
                {
                    BinaryTreeNode <T> leftMost       = node.Right.Left;
                    BinaryTreeNode <T> leftMostParent = node.Right;

                    while (leftMost.Left != null)
                    {
                        leftMostParent = leftMost;
                        leftMost       = leftMost.Left;
                    }
                    //Take care of leftmost right child
                    leftMostParent.Left = leftMost.Right;
                    leftMost.Right      = node.Right;
                    leftMost.Left       = node.Left;

                    if (parent == null)
                    {
                        head = leftMost;
                    }
                    else
                    {
                        var result = parent.CompareTo(leftMost.Value);
                        if (result > 0)
                        {
                            parent.Left = leftMost;
                        }
                        else
                        {
                            parent.Right = leftMost;
                        }
                    }
                }
                #endregion
            }
            return(true);
        }
 public BinaryTreeNode(char _id)
 {
     id   = _id;
     left = right = null;
 }
Exemple #3
0
 public void clear()
 {
     head = null;
 }
Exemple #4
0
        public bool Remove(T value)
        {
            BinaryTreeNode <T> current, parent;

            current = FindWithParent(value, out parent);

            if (current == null)
            {
                return(false);
            }
            count--;
            //case 1 current has no right node
            if (current.Right == null)
            {
                if (parent == null)
                {
                    head = current.Left;
                }
                else
                {
                    int res = current.CompareTo(value);

                    if (res > 0)
                    {
                        parent.Left = current.Left;
                    }
                    else
                    {
                        parent.Right = current.Left;
                    }
                }
            }
            //case 2 current has right and right has no left child
            //lmp -left most parent
            //lm - left most
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                if (parent == null)
                {
                    head = current.Right;
                }
                else
                {
                    int res = current.CompareTo(value);

                    if (res > 0)
                    {
                        parent.Left = current.Right;
                    }
                    else
                    {
                        parent.Right = current.Right;
                    }
                }
            }
            //case 3 current has right and right has left child
            else
            {
                BinaryTreeNode <T> lmp = current.Right;
                BinaryTreeNode <T> lm  = current.Right.Left;

                while (lm.Left != null)
                {
                    lmp = lm;
                    lm  = lm.Left;
                }

                lmp.Left = lm.Right;

                lm.Left  = current.Left;
                lm.Right = current.Right;

                if (parent == null)
                {
                    head = lm;
                }
                else
                {
                    int res = current.CompareTo(value);

                    if (res > 0)
                    {
                        parent.Left = lm;
                    }
                    else
                    {
                        parent.Right = lm;
                    }
                }
            }
            return(true);
        }
Exemple #5
0
 public Inorder(BinaryTreeNode <T> node, int count) : base(node, count)
 {
 }
Exemple #6
0
        public bool Remove(T value)
        {
            BinaryTreeNode <T> parent;

            var current = FindWithParent(value, out parent);

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

            Count--;

            if (current.Right == null)
            {
                if (parent == null)
                {
                    _head = current.Left;
                }
                else
                {
                    var result = parent.CompareTo(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)
                {
                    _head = current.Right;
                }
                else
                {
                    var result = parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        parent.Left = current.Right;
                    }
                    else if (result < 0)
                    {
                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                var leftMost       = current.Right.Left;
                var 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)
                {
                    _head = leftMost;
                }
                else
                {
                    var result = parent.CompareTo(current.Value);

                    if (result > 0)
                    {
                        parent.Left = leftMost;
                    }
                    else if (result < 0)
                    {
                        parent.Right = leftMost;
                    }
                }
            }
            return(true);
        }
 private T TreeMax(BinaryTreeNode <T> node)
 {
     if (node == null)
     {
         return(default);
Exemple #8
0
 public void Clear()
 {
     _head = null;
     //_count = 0;
 }
Exemple #9
0
 private static void Print(BinaryTreeNode root)
 {
     BinaryTree.Traverse(root, n => Console.WriteLine(n.Value));
 }
        /// <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);
        }
 public abstract IEnumerator <T> Traversal <T>(BinaryTreeNode <T> node) where T : IComparable <T>;
Exemple #12
0
 public BinaryTree(T firstValue)
 {
     Root = new BinaryTreeNode <T>(firstValue, null);
 }
Exemple #13
0
 public BinaryTreeNode(T data, BinaryTreeNode <T> left, BinaryTreeNode <T> right)
 {
     this.Data      = data;
     this.TreeRight = right;
     this.TreeRight = left;
 }
 public BinaryTree()
 {
     root = null;
 }
Exemple #15
0
 public BinaryTreeNode(T data = default, BinaryTreeNode <T> left = default, BinaryTreeNode <T> right = default)
 {
     Data  = data;
     Left  = left;
     Right = right;
 }
 public BinaryTreeNode()
 {
     left = right = null;
 }
Exemple #17
0
 public int CompareTo(BinaryTreeNode <T> other)
 {
     return(Data.CompareTo(other.Data));
 }
Exemple #18
0
        public void DeleteNode(T data)
        {
            if (rootNode == null)
            {
                return;
            }

            BinaryTreeNode <T>[] node       = FindNodeByData(data);
            BinaryTreeNode <T>   deleteNode = node[0];
            BinaryTreeNode <T>   parentNode = node[1];

            if (deleteNode == null)
            {
                return;
            }

            if (deleteNode.Left == null && deleteNode.Right == null)
            {
                if (parentNode == null)
                {
                    rootNode = null;
                    Count--;
                    return;
                }

                if (parentNode.Left != null && comparer.Compare(parentNode.Left.Data, deleteNode.Data) == 0)
                {
                    parentNode.Left = null;
                    Count--;
                }
                else
                {
                    parentNode.Right = null;
                    Count--;
                }
            }
            else if (deleteNode.Left != null && deleteNode.Right == null)
            {
                if (parentNode == null)
                {
                    rootNode = deleteNode.Left;
                    Count--;
                    return;
                }

                if (parentNode.Left != null && comparer.Compare(parentNode.Left.Data, deleteNode.Data) == 0)
                {
                    parentNode.Left = deleteNode.Left;
                    Count--;
                }
                else
                {
                    parentNode.Right = deleteNode.Left;
                    Count--;
                }
            }
            else if (deleteNode.Right != null && deleteNode.Left == null)
            {
                if (parentNode == null)
                {
                    rootNode = deleteNode.Right;
                    Count--;
                    return;
                }

                if (parentNode.Left != null && comparer.Compare(parentNode.Left.Data, deleteNode.Data) == 0)
                {
                    parentNode.Left = deleteNode.Right;
                    Count--;
                }
                else
                {
                    parentNode.Right = deleteNode.Right;
                    Count--;
                }
            }
            else
            {
                BinaryTreeNode <T> minimumNode       = deleteNode.Right;
                BinaryTreeNode <T> parentMinimumNode = minimumNode;

                while (minimumNode.Left != null)
                {
                    if (minimumNode.Left.Left == null)
                    {
                        parentMinimumNode = minimumNode;
                    }

                    minimumNode = minimumNode.Left;
                }

                deleteNode.Data = minimumNode.Data;
                Count--;
                parentMinimumNode.Left = minimumNode.Right;
            }
        }
Exemple #19
0
        // Removes the first occurrence of of the specified value from the tree
        public bool Remove(T value)
        {
            BinaryTreeNode <T> current, parent;

            current = FindWithParent(value, out parent);

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

            count--;

            // Case 1: If current has no right child, then the current's left child replaces current
            if (current.Right == null)
            {
                // If true, then we remove the root node
                if (parent == null)
                {
                    head = current.Left;
                }

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

                    // If parent value is greater than current value, make the current left child a left child of parent
                    if (result > 0)
                    {
                        parent.Left = current.Left;
                    }

                    // If parent value is less than current value, make the current left child a right child of parent
                    else if (result < 0)
                    {
                        parent.Right = current.Right;
                    }
                }
            }

            // Case 2: If the current's right child has no left, then current's right child replaces current.
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                if (parent == null)
                {
                    head = current.Right;
                }

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

                    // If parent value is greater than current value,
                    // make the current right child a left child of parent
                    if (result > 0)
                    {
                        parent.Left = current.Right;
                    }

                    // If parent value is less than current value,
                    // make the current right child a right child of parent
                    else if (result < 0)
                    {
                        parent.Right = current.Right;
                    }
                }
            }

            // Case 3: If current's right child has a left child,
            // replace current with current's right child's left-most child.
            else
            {
                // Find the right's left-most child
                BinaryTreeNode <T> leftmost       = current.Right.Left;
                BinaryTreeNode <T> leftmostParent = current.Right;

                while (leftmost.Left != null)
                {
                    leftmostParent = leftmost;
                    leftmost       = leftmost.Left;
                }

                // The parent's left subtree becomes the leftmost's right subtree
                leftmostParent.Left = leftmost.Right;

                // Assign leftmost's left and right to current's left and right children
                leftmost.Left  = current.Left;
                leftmost.Right = current.Right;

                if (parent == null)
                {
                    head = leftmost;
                }

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

                    // If parent value is greater than current value, make leftmost the parent's left child
                    if (result > 0)
                    {
                        parent.Left = leftmost;
                    }

                    // If parent value is right than current value, make leftmost the parent's right child
                    else if (result < 0)
                    {
                        parent.Right = leftmost;
                    }
                }
            }

            return(true);
        }
Exemple #20
0
 public abstract void Execute(BinaryTreeNode <T> enode);
Exemple #21
0
 public void Clear()
 {
     _head = null;
     Count = 0;
 }