Ejemplo n.º 1
0
        public AvlNode <TKey, TValue> AddOrReplace([NotNull] TKey key, TValue value)
        {
            var compare = key.CompareTo(this.key);

            if (compare == 0)
            {
                this.value = value;
                return(this);
            }

            if (compare < 0)
            {
                if (this.left != null)
                {
                    this.left = this.left.AddOrReplace(key, value);

                    // Check whether rebalance is necessary and update height
                    var node = Rebalance(this);
                    node.height = CalculateHeight(node);
                    return(node);
                }
                else
                {
                    this.left   = new AvlNode <TKey, TValue>(key, value);
                    this.height = 2;
                    return(this);
                }
            }
            else
            {
                if (this.right != null)
                {
                    this.right = this.right.AddOrReplace(key, value);

                    var node = Rebalance(this);
                    node.height = CalculateHeight(node);
                    return(node);
                }
                else
                {
                    this.right  = new AvlNode <TKey, TValue>(key, value);
                    this.height = 2;
                    return(this);
                }
            }
        }
Ejemplo n.º 2
0
 private static AvlNode <TKey, TValue> RightLeftRotation(AvlNode <TKey, TValue> avlNode)
 {
     avlNode.right = RightRotation(avlNode.right);
     return(LeftRotation(avlNode));
 }
Ejemplo n.º 3
0
 private static AvlNode <TKey, TValue> LeftRightRotation(AvlNode <TKey, TValue> avlNode)
 {
     avlNode.left = LeftRotation(avlNode.left);
     return(RightRotation(avlNode));
 }
Ejemplo n.º 4
0
 private static int CalculateHeight(AvlNode <TKey, TValue> avlNode)
 {
     return(Math.Max(Height(avlNode.left), Height(avlNode.right)) + 1);
 }
Ejemplo n.º 5
0
        public bool Remove([NotNull] TKey key, out AvlNode <TKey, TValue> node)
        {
            var compare = key.CompareTo(this.key);

            if (compare == 0)
            {
                if (this.left == null)
                {
                    node = this.right;
                }
                else
                {
                    if (this.right == null)
                    {
                        node = this.left;
                    }
                    else
                    {
                        var top = MinValueNode(this.right);
                        this.right.Remove(top.key, out top);
                        top.left   = this.left;
                        top.right  = this.right;
                        top        = Rebalance(top);
                        top.height = CalculateHeight(top);
                        node       = top;
                    }
                }

                return(true);
            }

            if (compare < 0)
            {
                if (this.left != null)
                {
                    var result = this.left.Remove(key, out node);
                    if (result)
                    {
                        this.left   = node;
                        node        = Rebalance(this);
                        node.height = CalculateHeight(node);
                    }

                    return(result);
                }

                node = this;
                return(false);
            }

            if (this.right != null)
            {
                var result = this.right.Remove(key, out node);
                if (result)
                {
                    this.right  = node;
                    node        = Rebalance(this);
                    node.height = CalculateHeight(node);
                }

                return(result);
            }

            node = this;
            return(false);
        }