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]; } }
public HeapNode(T value) { Value = value; Left = null; Right = null; Parent = null; }
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); }
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); } }