/// <summary> /// 向上修正节点(向树根方向修正节点) /// </summary> /// <returns>The to root.</returns> /// <param name="node">Node.</param> private BinaryHeapNode ModifyToRoot(BinaryHeapNode node) { var currentNodeData = node.Data; var currentNodeValue = currentNodeData.F; var parentNode = node.ParentNode; while (parentNode != null) { if (currentNodeValue < parentNode.Data.F) { node.Data = parentNode.Data; node.Data.BinaryHeapNode = node; node = node.ParentNode; parentNode = node.ParentNode; } else { break; } } node.Data = currentNodeData; node.Data.BinaryHeapNode = node; return(node); }
/// <summary> /// 添加新节点 /// </summary> /// <returns>The node.</returns> /// <param name="data">Data.</param> public BinaryHeapNode InsertNode(AStarNode data) { if(this.headNode != null) { BinaryHeapNode parentNode = this.nodes[this.nodeLength >> 1]; BinaryHeapNode node = this.GetNode(data, parentNode); node.data.binaryHeapNode = node; if(parentNode.leftNode == null) { parentNode.leftNode = node; }else { parentNode.rightNode = node; } this.nodes[this.nodeLength] = node; this.nodeLength ++; return this.ModifyToRoot(node); }else { this.nodes[1] = this.headNode = this.GetNode(data, null); this.nodes.Add(this.headNode); this.headNode.data.binaryHeapNode = this.headNode; this.nodeLength = 2; return this.headNode; } }
/// <summary> /// 向上修正节点(向树根方向修正节点) /// </summary> /// <returns>The to root.</returns> /// <param name="node">Node.</param> private BinaryHeapNode ModifyToRoot(BinaryHeapNode node) { AStarNode currentNodeData = node.data; int currentNodeValue = currentNodeData.f; BinaryHeapNode parentNode = node.parentNode; while (parentNode != null) { if (currentNodeValue < parentNode.data.f) { node.data = parentNode.data; node.data.binaryHeapNode = node; node = node.parentNode; parentNode = node.parentNode; } else { break; } } node.data = currentNodeData; node.data.binaryHeapNode = node; return(node); }
/// <summary> /// 添加新节点 /// </summary> /// <returns>The node.</returns> /// <param name="data">Data.</param> public BinaryHeapNode InsertNode(AStarNode data) { if (this.headNode != null) { BinaryHeapNode parentNode = this.nodes[this.nodeLength >> 1]; BinaryHeapNode node = this.GetNode(data, parentNode); node.data.binaryHeapNode = node; if (parentNode.leftNode == null) { parentNode.leftNode = node; } else { parentNode.rightNode = node; } this.nodes[this.nodeLength] = node; this.nodeLength++; return(this.ModifyToRoot(node)); } else { this.nodes[1] = this.headNode = this.GetNode(data, null); this.nodes.Add(this.headNode); this.headNode.data.binaryHeapNode = this.headNode; this.nodeLength = 2; return(this.headNode); } }
private void SetLastChildAsParent(BinaryHeapNode <T> lastChild) { BinaryHeapNode <T> lastChildParent = lastChild.Parent; if (lastChildParent == null) { return; } if (lastChildParent.LeftChild == lastChild) { lastChildParent.LeftChild = null; } if (lastChildParent.RightChild == lastChild) { lastChildParent.RightChild = null; } lastChild.Parent = null; lastChild.LeftChild = this.Root.LeftChild; lastChild.RightChild = this.Root.RightChild; this.NodeOrEmptyChangeParent(this.Root.LeftChild, lastChild); this.NodeOrEmptyChangeParent(this.Root.RightChild, lastChild); this.Root = lastChild; }
/// <summary> /// 存储节点 /// </summary> /// <param name="node">Node.</param> private void CacheNode(BinaryHeapNode node) { node.parentNode = node.leftNode = node.rightNode = null; node.data = null; this.cacheNodes.Add (node); }
/// <summary> /// 存储节点 /// </summary> /// <param name="node">Node.</param> private void CacheNode(BinaryHeapNode node) { node.parentNode = node.leftNode = node.rightNode = null; node.data = null; this.cacheNodes.Add(node); }
public void Insert(T addValue) { this.Count++; BinaryHeapNode <T> newNode = new BinaryHeapNode <T>(null, addValue); BinaryHeapNode <T> parentForAdd = this.BfsFirstNodeNoneOrOneChild(); if (parentForAdd == null) { this.Root = newNode; return; } if (parentForAdd.LeftChild == null) { newNode.Parent = parentForAdd; parentForAdd.LeftChild = newNode; } else if (parentForAdd.RightChild == null) { newNode.Parent = parentForAdd; parentForAdd.RightChild = newNode; } this.BubbleNodeUp(newNode); }
private void ReplaceParentChildRight(BinaryHeapNode <T> newParent, BinaryHeapNode <T> newChild) { BinaryHeapNode <T> parentParent = newChild.Parent; if (parentParent != null) { if (parentParent.LeftChild == newChild) { parentParent.LeftChild = newParent; } else { parentParent.RightChild = newParent; } } BinaryHeapNode <T> newChildCopy = new BinaryHeapNode <T>(newChild.Parent, newChild.Value); newChildCopy.LeftChild = newChild.LeftChild; newChildCopy.RightChild = newChild.RightChild; newChild.Parent = newParent; this.NodeOrEmptyChangeParent(newChild.LeftChild, newParent); newChild.LeftChild = newParent.LeftChild; newChild.RightChild = newParent.RightChild; this.NodeOrEmptyChangeParent(newParent.LeftChild, newChild); this.NodeOrEmptyChangeParent(newParent.RightChild, newChild); newParent.Parent = newChildCopy.Parent; newParent.LeftChild = newChildCopy.LeftChild; newParent.RightChild = newChild; }
private BinaryHeapNode <T> GetBiggerChild(BinaryHeapNode <T> currentNode) { BinaryHeapNode <T> biggerChild = null; if ((currentNode.LeftChild != null) && (currentNode.LeftChild.CompareTo(currentNode) > 0)) { biggerChild = currentNode.LeftChild; } if (biggerChild != null) { if ((currentNode.RightChild != null) && (currentNode.RightChild.CompareTo(biggerChild) > 0)) { biggerChild = currentNode.RightChild; } } else { if ((currentNode.RightChild != null) && (currentNode.RightChild.CompareTo(currentNode) > 0)) { biggerChild = currentNode.RightChild; } } return(biggerChild); }
public void Remove(IPriorityQueueEntry <TItem> entry) { if (entry == null) { throw new ArgumentNullException("entry"); } BinaryHeapNode temp = entry as BinaryHeapNode; if (temp == null) { throw new InvalidCastException("Invalid heap entry format!"); } if (temp.HeapIdentifier != identifier) { throw new ArgumentException("Heap does not contain this node!"); } if (temp.Index == Count) { temp.HeapIdentifier = Guid.Empty; heap[Count--] = null; return; } MoveNode(heap[Count], temp.Index); heap[Count--] = null; HeapifyUp(heap[HeapifyDown(heap[temp.Index])]); temp.HeapIdentifier = Guid.Empty; }
/// <summary> /// 存储节点 /// </summary> /// <param name="node">Node.</param> private void CacheNode(BinaryHeapNode node) { node.ParentNode = node.LeftNode = node.RightNode = null; node.Data = null; _cacheNodes.Add(node); }
/// <summary> /// 取出最小值 /// </summary> /// <returns>The node.</returns> public AStarNode PopNode() { AStarNode minValue = this.headNode.data; BinaryHeapNode lastNode = this.nodes[--this.nodeLength]; if (lastNode != this.headNode) { BinaryHeapNode parentNode = lastNode.parentNode; if (parentNode.leftNode == lastNode) { parentNode.leftNode = null; } else { parentNode.rightNode = null; } this.headNode.data = lastNode.data; this.headNode.data.binaryHeapNode = this.headNode; this.ModifyToLeaf(this.headNode); } else { this.headNode = null; } this.CacheNode(this.nodes[this.nodeLength]); this.nodes[this.nodeLength] = null; return(minValue); }
private BinaryHeapNode <T> BfsLastChild() { Queue <BinaryHeapNode <T> > queue = new Queue <BinaryHeapNode <T> >(); BinaryHeapNode <T> currentNode = this.Root; BinaryHeapNode <T> searchedNode = currentNode; while (currentNode != null) { searchedNode = currentNode; BinaryHeapNode <T> leftNode = currentNode.LeftChild; if (leftNode != null) { queue.Enqueue(leftNode); } BinaryHeapNode <T> rightNode = currentNode.RightChild; if (rightNode != null) { queue.Enqueue(rightNode); } try { currentNode = queue.Dequeue(); } catch (InvalidOperationException) { break; } } return(searchedNode); }
private void NodeOrEmptyChangeParent(BinaryHeapNode <T> node, BinaryHeapNode <T> parent) { if (node != null) { node.Parent = parent; } }
/// <summary> /// 取出最小值 /// </summary> /// <returns>The Node.</returns> public AStarNode PopNode() { var minValue = HeadNode.Data; var lastNode = Nodes[--_nodeLength]; if (lastNode != HeadNode) { var parentNode = lastNode.ParentNode; if (parentNode.LeftNode == lastNode) { parentNode.LeftNode = null; } else { parentNode.RightNode = null; } HeadNode.Data = lastNode.Data; HeadNode.Data.BinaryHeapNode = HeadNode; ModifyToLeaf(HeadNode); } else { HeadNode = null; } CacheNode(Nodes[_nodeLength]); Nodes[_nodeLength] = null; return(minValue); }
/// <summary> /// 添加新节点 /// </summary> /// <returns>The Node.</returns> /// <param name="data">Data.</param> public BinaryHeapNode InsertNode(AStarNode data) { if (HeadNode != null) { var parentNode = Nodes[_nodeLength >> 1]; var node = GetNode(data, parentNode); node.Data.BinaryHeapNode = node; if (parentNode.LeftNode == null) { parentNode.LeftNode = node; } else { parentNode.RightNode = node; } Nodes[_nodeLength] = node; _nodeLength++; return(ModifyToRoot(node)); } Nodes[1] = HeadNode = GetNode(data, null); Nodes.Add(HeadNode); HeadNode.Data.BinaryHeapNode = HeadNode; _nodeLength = 2; return(HeadNode); }
/// <summary> /// 修正节点 /// </summary> /// <returns>The Node.</returns> /// <param name="node">Node.</param> public BinaryHeapNode ModifyNode(BinaryHeapNode node) { if (node.ParentNode != null && node.ParentNode.Data.F > node.Data.F) { return(ModifyToRoot(node)); } return(ModifyToLeaf(node)); }
public void Dispose() { iStrand.ScheduleExclusive( ()=> { iTimerThread.Cancel(iNode); iNode = null; }); }
/// <summary> /// 重置 /// </summary> public void Reset() { for (var index = 1; index < _nodeLength; index++) { CacheNode(Nodes[index]); Nodes[index] = null; } _nodeLength = 1; HeadNode = null; }
private void BubbleNodeUp(BinaryHeapNode <T> node) { BinaryHeapNode <T> parent = node.Parent; while ((parent != null) && (node.CompareTo(parent) > 0)) { this.ReplaceParentChild(node, parent); parent = node.Parent; } }
/// <summary> /// 重置 /// </summary> public void Reset() { for (int index = 1; index < this.nodeLength; index++) { this.CacheNode(this.nodes[index]); this.nodes[index] = null; } this.nodeLength = 1; this.headNode = null; }
/// <summary> /// 修正节点 /// </summary> /// <returns>The node.</returns> /// <param name="node">Node.</param> public BinaryHeapNode ModifyNode(BinaryHeapNode node) { if (node.parentNode != null && node.parentNode.data.f > node.data.f) { return(this.ModifyToRoot(node)); } else { return(this.ModifyToLeaf(node)); } }
public TItem Dequeue() { if (Count == 0) { throw new InvalidOperationException("Binary heap is empty!"); } BinaryHeapNode head = heap[1]; Remove(heap[1]); return(head.Item); }
private void RootFallDown() { BinaryHeapNode <T> currentNode = this.Root; BinaryHeapNode <T> biggerChild = this.GetBiggerChild(currentNode); while (biggerChild != null) { this.ReplaceParentChild(biggerChild, currentNode); biggerChild = this.GetBiggerChild(currentNode); } }
private int HeapifyDown(BinaryHeapNode node) { BinaryHeapNode child = BestChild(node.Index); int index = node.Index; while (child != null) { index = child.Index; MoveNode(child, child.Index / Degree); child = BestChild(index); } MoveNode(node, index); return(index); }
/// <summary> /// 添加对象到堆中 /// </summary> /// <param name="obj"></param> public void Add(T obj) { // 创建节点 BinaryHeapNode node = new BinaryHeapNode(obj); // 设置节点的排序值 node.sort = CalculateSort(node.obj); // 加入到节点列表最后面,四叉树的结构导致越靠后的节点在越深的层,这样可以保证存到最底层 nodes.Add(node); // 以新节点为起点,自下向上调整结构 BottomToTop(nodes.Count - 1); }
private void HeapifyUp(BinaryHeapNode node) { BinaryHeapNode parent = Parent(node.Index); int to = node.Index; while (parent != null && Compare(parent.Priority, node.Priority) > 0) { int grandParent = parent.Index / Degree; int temp = parent.Index; MoveNode(parent, to); to = temp; parent = heap[grandParent]; } MoveNode(node, to); }
private void ReplaceParentChild(BinaryHeapNode <T> newParent, BinaryHeapNode <T> newChild) { if (this.Root == newChild) { this.Root = newParent; } if (newChild.LeftChild == newParent) { this.ReplaceParentChildLeft(newParent, newChild); } else { this.ReplaceParentChildRight(newParent, newChild); } }
static void Main(string[] args) { var BH = new BinaryHeapNode(10); BH.Insert(10); BH.Insert(11); BH.Insert(1); BH.Insert(13); BH.Insert(6); BH.Insert(4); BH.PrintHeap(); var Maxval = BH.DeleteMax(); Console.WriteLine("Maximum values is " + Maxval); BH.PrintHeap(); Maxval = BH.DeleteMax(); Console.WriteLine("Maximum values is " + Maxval); }
/// <summary> /// 获得一个节点 /// </summary> /// <returns>The Node.</returns> /// <param name="data">Data.</param> /// <param name="parentNode">Parent Node.</param> private BinaryHeapNode GetNode(AStarNode data, BinaryHeapNode parentNode) { BinaryHeapNode binaryHeapNode; if (_cacheNodes.Count > 0) { binaryHeapNode = _cacheNodes[_cacheNodes.Count - 1]; binaryHeapNode.Data = data; binaryHeapNode.ParentNode = parentNode; _cacheNodes.RemoveAt(_cacheNodes.Count - 1); } else { binaryHeapNode = new BinaryHeapNode(data, parentNode); } return(binaryHeapNode); }
/// <summary> /// 获得一个节点 /// </summary> /// <returns>The node.</returns> /// <param name="data">Data.</param> /// <param name="parentNode">Parent node.</param> private BinaryHeapNode GetNode(AStarNode data, BinaryHeapNode parentNode) { BinaryHeapNode binaryHeapNode = null; if(this.cacheNodes.Count > 0) { binaryHeapNode = this.cacheNodes[this.cacheNodes.Count - 1]; binaryHeapNode.data = data; binaryHeapNode.parentNode = parentNode; this.cacheNodes.RemoveAt(this.cacheNodes.Count - 1); } else { binaryHeapNode = new BinaryHeapNode(data, parentNode); } return binaryHeapNode; }
/// <summary> /// 获得一个节点 /// </summary> /// <returns>The node.</returns> /// <param name="data">Data.</param> /// <param name="parentNode">Parent node.</param> private BinaryHeapNode GetNode(AStarNode data, BinaryHeapNode parentNode) { BinaryHeapNode binaryHeapNode = null; if (this.cacheNodes.Count > 0) { binaryHeapNode = this.cacheNodes[this.cacheNodes.Count - 1]; binaryHeapNode.data = data; binaryHeapNode.parentNode = parentNode; this.cacheNodes.RemoveAt(this.cacheNodes.Count - 1); } else { binaryHeapNode = new BinaryHeapNode(data, parentNode); } return(binaryHeapNode); }
/// <summary> /// 向下修正节点(向树叶方向修正节点) /// </summary> /// <returns>The to leaf.</returns> /// <param name="node">Node.</param> private BinaryHeapNode ModifyToLeaf(BinaryHeapNode node) { AStarNode currentNodeData = node.data; int currentNodeValue = currentNodeData.f; BinaryHeapNode leftNode = null; BinaryHeapNode rightNode = null; while (true) { leftNode = node.leftNode; rightNode = node.rightNode; if (rightNode != null && leftNode != null && rightNode.data.f < leftNode.data.f) { if (currentNodeValue > rightNode.data.f) { node.data = rightNode.data; node.data.binaryHeapNode = node; node = rightNode; } else { break; } } else if (leftNode != null && leftNode.data.f < currentNodeValue) { node.data = leftNode.data; node.data.binaryHeapNode = node; node = leftNode; } else { break; } } node.data = currentNodeData; node.data.binaryHeapNode = node; return(node); }
/// <summary> /// 向下修正节点(向树叶方向修正节点) /// </summary> /// <returns>The to leaf.</returns> /// <param name="node">Node.</param> private BinaryHeapNode ModifyToLeaf(BinaryHeapNode node) { AStarNode currentNodeData = node.data; int currentNodeValue = currentNodeData.f; BinaryHeapNode leftNode = null; BinaryHeapNode rightNode = null; while (true) { leftNode = node.leftNode; rightNode = node.rightNode; if (rightNode != null && leftNode != null && rightNode.data.f < leftNode.data.f) { if (currentNodeValue > rightNode.data.f) { node.data = rightNode.data; node.data.binaryHeapNode = node; node = rightNode; } else { break; } } else if (leftNode != null && leftNode.data.f < currentNodeValue) { node.data = leftNode.data; node.data.binaryHeapNode = node; node = leftNode; } else { break; } } node.data = currentNodeData; node.data.binaryHeapNode = node; return node; }
internal void Cancel(BinaryHeapNode<DateTime> aNode) { iCallbacks.Remove(aNode); aNode.Remove(); }
/// <summary> /// 重置 /// </summary> public void Reset() { for(int index = 1; index < this.nodeLength; index++) { this.CacheNode(this.nodes[index]); this.nodes[index] = null; } this.nodeLength = 1; this.headNode = null; }
/// <summary> /// Cancel any outstanding invocation and schedule a new invocation /// for the specified time. As the reschedule is asynchronous, it /// might not take effect immediately. Use the returned Task to /// know when it has taken effect. (If the invocation is scheduled /// now or in the past, it will take effect as soon as possible.) /// </summary> /// <param name="aDateTime"></param> public Task Reschedule(DateTime aDateTime) { return iStrand.ScheduleExclusive( ()=> { if (iNode == null) { iNode = iTimerThread.CreateNode(aDateTime, this); } else { iNode.Value = aDateTime; } iTimerThread.RefreshTimer(); }); }
/// <summary> /// 向上修正节点(向树根方向修正节点) /// </summary> /// <returns>The to root.</returns> /// <param name="node">Node.</param> private BinaryHeapNode ModifyToRoot(BinaryHeapNode node) { AStarNode currentNodeData = node.data; int currentNodeValue = currentNodeData.f; BinaryHeapNode parentNode = node.parentNode; while(parentNode != null) { if(currentNodeValue < parentNode.data.f) { node.data = parentNode.data; node.data.binaryHeapNode = node; node = node.parentNode; parentNode = node.parentNode; }else { break; } } node.data = currentNodeData; node.data.binaryHeapNode = node; return node; }
/// <summary> /// 修正节点 /// </summary> /// <returns>The node.</returns> /// <param name="node">Node.</param> public BinaryHeapNode ModifyNode(BinaryHeapNode node) { if(node.parentNode != null && node.parentNode.data.f > node.data.f) { return this.ModifyToRoot(node); }else{ return this.ModifyToLeaf(node); } }
/// <summary> /// 取出最小值 /// </summary> /// <returns>The node.</returns> public AStarNode PopNode() { AStarNode minValue = this.headNode.data; BinaryHeapNode lastNode = this.nodes[--this.nodeLength]; if(lastNode != this.headNode) { BinaryHeapNode parentNode = lastNode.parentNode; if(parentNode.leftNode == lastNode) { parentNode.leftNode = null; }else{ parentNode.rightNode = null; } this.headNode.data = lastNode.data; this.headNode.data.binaryHeapNode = this.headNode; this.ModifyToLeaf(this.headNode); }else { this.headNode = null; } this.CacheNode(this.nodes[this.nodeLength]); this.nodes[this.nodeLength] = null; return minValue; }
public BinaryHeapNode(AStarNode data, BinaryHeapNode parentNode) { this.data = data; this.parentNode = parentNode; }