DoubleLinkedList<GraphNode *> Dijkstra(GraphNode *Start, GraphNode * End) { Map<GraphNode *, DijkstraHelperNode *> NodeHelperMap; DoubleLinkedList<DijkstraHelperNode *> Visited; MinHeap<DijkstraHelperNode *> OpenHeap; DijkstraHelperNode *NewHelper = new DijkstraHelperNode(); NewHelper->m_Node = Start; NewHelper->m_Cost = 0; NodeHelperMap.Insert(Start, NewHelper); OpenHeap.Insert(NewHelper); while(!OpenHeap.IsEmpty()) { DijkstraHelperNode *CurrentHelperNode = OpenHeap.PopTop(); assert(CurrentHelperNode != NULL); GraphNode *CurrentGraphNode = CurrentHelperNode->m_Node; assert(CurrentGraphNode != NULL); DoubleLinkedList<GraphEdge *> *CurrendEdges = CurrentGraphNode->GetEdges(); DoubleListNode<GraphEdge *> *CurrentEdge = CurrentEdges.GetHead(); while(CurrentEdge != NULL) { GraphNode *OtherNode = CurrentEdge->m_End; if(OtherNode == CurrentGraphNode) { OtherNode = CurrentEdge->m_Start; } assert(OtherNode != CurrentGraphNode); DijkstraHelperNode *NodeHelper = NodeHelperMap.GetValue(OtherNode); if(NodeHelper == NULL) { NodeHelper = new DijkstraHelperNode(); NodeHelper->m_Node = OtherNode; NodeHelperMap.Insert(OtherNode, NodeHelper); OpenHeap.Insert(NodeHelper); } int CostToNode = CurrentHelperNode->m_Cost + CurrentEdge->m_Cost; if(CostToNode < NodeHelper->m_Cost) { NodeHelper->m_Cost = CostToNode; NodeHelper->m_Previous = CurrentGraphNode; OpenHeap.Update(NodeHelper); } if(OtherNode == End) { break; } } } DoubleLinkedList<GraphNode *> Path; DijkstraHelperNode *EndHelper = NodeHelperMap.GetValue(End); if(EndHelper != NULL) { DijkstraHelperNode *CurrentHelper = EndHelper; while(CurrentHelper != NULL) { Path.AddFront(CurrentHelper->m_Node); CurrentHelper = CurrentHelper->m_Previous; } } }
void RunOperation(char operation, const std::vector<std::string>& arguments, MinHeap<T>& heap, std::ostream& file) { if(operation == 'I') { auto value = ToInt(arguments[1]); heap.Insert(value); } else if(operation == 'D') { auto value = ToInt(arguments[1]); heap.Delete(value); } else if(operation == 'C') { auto oldValue = ToInt(arguments[1]); auto newValue = ToInt(arguments[2]); heap.Change(oldValue, newValue); } else if(operation == 'P') { heap.PrintPostOrder(file); file << std::endl; } }
int main() { int ar[]={56,34,90,23,18,100,89,10,45,78,65}; int n = sizeof(ar)/sizeof(ar[0]); MinHeap<int> mh; for(int i = 0;i<n;++i) { mh.Insert(ar[i]); } int x; while(mh.Remove(x)) { std::cout << x <<" "; } std::cout << std::endl; return 0; }