Ejemplo n.º 1
0
        public void Insert(T value)
        {
            ++Length;

            if (_root == null)
            {
                _root = new HeapNode <T>(value);
                _nodes.Add(_root);
                _currentAdding    = _root;
                _currentNodeIndex = 0;

                return;
            }

            var temp = new HeapNode <T>(value);

            temp.Parent = _currentAdding;
            _nodes.Add(temp);

            if (_currentAdding.Left == null)
            {
                _currentAdding.Left = temp;
                BalanceNodeBranch(_currentAdding.Left);
            }
            else
            {
                _currentAdding.Right = temp;
                BalanceNodeBranch(_currentAdding.Right);

                ++_currentNodeIndex;
                _currentAdding = _nodes[_currentNodeIndex];
            }
        }
Ejemplo n.º 2
0
 public HeapNode(T value)
 {
     Value  = value;
     Left   = null;
     Right  = null;
     Parent = null;
 }
Ejemplo n.º 3
0
        private void RemoveAndBalance(HeapNode <T> node)
        {
            var last = _nodes[_nodes.Count - 1];

            (node.Value, last.Value) = (last.Value, node.Value);

            var temp = last.Parent;

            if (temp.Right.Equals(temp))
            {
                temp.Right = null;
            }
            else
            {
                temp.Left = null;
            }

            _nodes.RemoveAt(_nodes.Count - 1);

            last.Dispose();

            --Length;

            BalanceNodeBranch(node);
        }
Ejemplo n.º 4
0
        private void BalanceNodeBranch(HeapNode <T> node)
        {
            if (node.Parent != null)
            {
                if (node.Value.CompareTo(node.Parent.Value) < 0)
                {
                    (node.Value, node.Parent.Value) = (node.Parent.Value, node.Value);

                    BalanceNodeBranch(node.Parent);
                }
            }

            if (node.Left != null && node.Value.CompareTo(node.Left) > 0)
            {
                (node.Value, node.Left.Value) = (node.Left.Value, node.Value);
                BalanceNodeBranch(node.Left);
            }

            if (node.Right != null && node.Value.CompareTo(node.Right) > 0)
            {
                (node.Value, node.Right.Value) = (node.Right.Value, node.Value);
                BalanceNodeBranch(node.Right);
            }
        }