Esempio n. 1
0
        private void TwoChildsDelete(BinaryNode node)
        {
            BinaryNode next   = FindSuccessor(node);
            BinaryNode parent = next.Parent;

            if (parent == node)
            {
                parent.SetKey(next.Key);
                parent.SetValue(next.Value);
                parent.SetRightNode(next.Right);

                if (next.Right != null)
                {
                    next.Right.SetParentNode(parent);
                }
            }
            else
            {
                parent.SetLeftNode(next.Right);
                if (next.Right != null)
                {
                    next.Right.SetParentNode(parent);
                }

                node.SetKey(next.Key);
                node.SetValue(next.Value);
            }

            next.SetParentNode(null);
            next.SetLeftNode(null);
            next.SetRightNode(null);
        }
Esempio n. 2
0
        public void Insert(int key, object value)
        {
            if (this.root == null)
            {
                root = new BinaryNode(key, value);
                root.SetParentNode(null);
            }
            else
            {
                Insert(key, value, this.root);
            }

            nodesCount++;
        }
Esempio n. 3
0
        private void NoChildsDelete(BinaryNode nodeToDelete)
        {
            if (nodeToDelete.Parent != null)
            {
                if (nodeToDelete.Parent.Left == nodeToDelete)
                {
                    nodeToDelete.Parent.SetLeftNode(null);
                }
                else
                {
                    nodeToDelete.Parent.SetRightNode(null);
                }

                nodeToDelete.SetParentNode(null);
            }
        }
Esempio n. 4
0
        private void OneChildDelete(BinaryNode nodeToDelete)
        {
            BinaryNode child = null;

            if (nodeToDelete.Left != null)
            {
                child = nodeToDelete.Left;
            }
            else
            {
                child = nodeToDelete.Right;
            }

            nodeToDelete.SetKey(child.Key);
            nodeToDelete.SetValue(child.Value);
            nodeToDelete.SetLeftNode(child.Left);
            nodeToDelete.SetRightNode(child.Right);

            child.SetParentNode(null);
            child.SetRightNode(null);
            child.SetLeftNode(null);
        }
Esempio n. 5
0
        private void Insert(int key, object value, BinaryNode root)
        {
            BinaryNode parent = root;
            BinaryNode child  = parent;

            while (child != null)
            {
                parent = child;

                if (child.Key == key)
                {
                    throw new Exception("Tree can contains only unique values ");
                }

                if (child.Key < key)
                {
                    child = child.Right;
                }
                else                //else if(child.Key > key)
                {
                    child = child.Left;
                }
            }

            BinaryNode newNode = new BinaryNode(key, value);

            if (parent.Key < key)
            {
                parent.SetRightNode(newNode);
            }
            else
            {
                parent.SetLeftNode(newNode);
            }
            newNode.SetParentNode(parent);
        }