Beispiel #1
0
        public bool Remove(TKey key)
        {
            var node = Find(key, _root);

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

            Count--;

            if (node == _root)
            {
                _root = Next(node);
            }

            if (node.Left == null && node.Right == null)
            {
                if (node.Parent.Left == node)
                {
                    node.Parent.Left = null;
                }
                else
                {
                    node.Parent.Right = null;
                }
            }
            else
            {
                if (node.Left == null || node.Right == null)
                {
                    if (node.Right != null)
                    {
                        node.Right.Parent = node.Parent;

                        if (node.Parent != null)
                        {
                            if (node.Parent.Left == node)
                            {
                                node.Parent.Left = node.Right;
                            }
                            else
                            {
                                node.Parent.Right = node.Right;
                            }
                        }
                    }
                    else
                    {
                        node.Left.Parent = node.Parent;

                        if (node.Parent != null)
                        {
                            if (node.Parent.Left == node)
                            {
                                node.Parent.Left = node.Left;
                            }
                            else
                            {
                                node.Parent.Right = node.Left;
                            }
                        }
                    }
                }
                else
                {
                    var next = Next(node);

                    if (next.Parent == node)
                    {
                        next.Left        = node.Left;
                        node.Left.Parent = next;
                        next.Parent      = node.Parent;

                        if (node == node.Parent.Left)
                        {
                            node.Parent.Left = next;
                        }
                        else if (node == node.Parent.Right)
                        {
                            node.Parent.Right = next;
                        }
                    }
                    else
                    {
                        if (next.Right != null)
                        {
                            next.Right.Parent = next.Parent;
                        }

                        next.Parent.Left  = next.Right;
                        next.Right        = node.Right;
                        next.Left         = node.Left;
                        node.Left.Parent  = next;
                        node.Right.Parent = next;
                        next.Parent       = node.Parent;

                        if (node.Parent != null)
                        {
                            if (node == node.Parent.Left)
                            {
                                node.Parent.Left = next;
                            }
                            else if (node == node.Parent.Right)
                            {
                                node.Parent.Right = next;
                            }
                        }
                    }
                }
            }

            return(true);
        }
Beispiel #2
0
 public void Clear()
 {
     _root = null;
     Count = 0;
 }
 /// <summary>
 /// Creates a new, empty <see cref="T:Trees.BinaryTree"/> class. Like a sapling!
 /// </summary>
 public BinaryTree()
 {
     topNode = null;
 }
 /// <summary>
 /// Creates a new <see cref="T:Trees.BinaryTree"/> class with the first nodze. Like a sapling!
 /// </summary>
 /// <param name="firstNodeVal">Top/first node value.</param>
 public BinaryTree(T firstNodeVal)
 {
     topNode = new BinaryNode <T>(firstNodeVal);
 }
Beispiel #5
0
 /// <summary>
 /// Makes a new node.
 /// </summary>
 /// <param name="val">The value of the new node.</param>
 /// <param name="leftNode">The left of the new node.</param>
 /// <param name="rightNode">The right of the new node.</param>
 public BinaryNode(T val, BinaryNode <T> leftNode = null, BinaryNode <T> rightNode = null)
 {
     this.val = val;
     left     = leftNode;
     right    = rightNode;
 }