コード例 #1
0
 /// <summary>
 /// Public method to remove new node with given value;
 /// </summary>
 /// <param name="value">The value to be removed.</param>
 public void Remove(T value)
 {
     _root = Remove(value, _root);
 }
コード例 #2
0
 public BinaryNode()
 {
     _element = default(T);
     _left    = null;
     _right   = null;
 }
コード例 #3
0
 /// <summary>
 /// Public method to insert new node with given value.
 /// </summary>
 /// <param name="value">The value to be inserted.</param>
 public void Insert(T value)
 {
     _root = Insert(value, _root);
 }
コード例 #4
0
 /// <summary>
 /// Public method to empty tree.
 /// </summary>
 public void MakeEmpty()
 {
     _root = null;             //what about memory leak?
 }
コード例 #5
0
 public BinarySearchTree()
 {
     _root = null;
 }
コード例 #6
0
 public BinarySearchTree(BinaryNode <T> root)
 {
     Root = root;
 }
コード例 #7
0
        private void doRemove(BinaryNode <T> node)
        {
            var  parent           = node.Parent;
            bool nodeIsRoot       = (parent == null);
            bool nodeIsRightChild = ReferenceEquals(parent?.Right, node);
            bool leftIsNull       = (node.Left == null);
            bool rightIsNull      = (node.Right == null);

            if (leftIsNull && rightIsNull)
            {
                if (!nodeIsRoot)
                {
                    if (nodeIsRightChild)
                    {
                        parent.Right = null;
                    }
                    else
                    {
                        parent.Left = null;
                    }
                }
                else
                {
                    Root = null;
                }
            }
            else if (leftIsNull)
            {
                if (nodeIsRoot)
                {
                    Root = node.Right;
                    node.Right.Parent = null;
                }
                else // parent!=null
                {
                    if (nodeIsRightChild)
                    {
                        parent.Right = node.Right;
                    }
                    else
                    {
                        parent.Left = node.Right;
                    }
                }
            }
            else if (rightIsNull)
            {
                if (nodeIsRoot)
                {
                    Root             = node.Left;
                    node.Left.Parent = null;
                }
                else // parent!=null
                {
                    if (nodeIsRightChild)
                    {
                        parent.Right = node.Left;
                    }
                    else
                    {
                        parent.Left = node.Left;
                    }
                }
            }
            else // both children are !=null
            {
                var newNode = DeleteMax(node.Left);
                if (parent == null)
                {
                    Root           = newNode;
                    newNode.Parent = null;
                }
                else if (nodeIsRightChild)
                {
                    parent.Right = newNode;
                }
                else
                {
                    parent.Left = newNode;
                }
                newNode.Left  = node.Left;
                newNode.Right = node.Right;
                if (newNode.Parent != null)
                {
                    Splay(newNode.Parent);
                }
            }
        }
コード例 #8
0
 public BinaryNode(T value, BinaryNode <T> left, BinaryNode <T> right) : this(value)
 {
     Left  = left;
     Right = right;
 }