public void AddNode(int w, T data) { HeapNode <T> node = new HeapNode <T>(); node.Weight = w; node.Data = data; allNodes.Add(node); int size = allNodes.Count; int currentIndex = size - 1; int parentIndex = (currentIndex - 1) / 2; while (parentIndex >= 0) { HeapNode <T> parentNode = allNodes[parentIndex]; HeapNode <T> currentNode = allNodes[currentIndex]; if (parentNode.Weight < currentNode.Weight) { Swap(parentIndex, currentIndex); currentIndex = parentIndex; parentIndex = (currentIndex - 1) / 2; } else { break; } } }
public void AddNode(int w, T data) { HeapNode <T> node = new HeapNode <T>(); node.Weight = w; node.Data = data; allNodes.Add(node); int size = allNodes.Count; int currentIndex = size - 1; nodeMap.Add(node.Data, currentIndex); HeapifyNode(currentIndex); }
private void HeapifyNode(int currentIndex) { int parentIndex = (currentIndex - 1) / 2; while (parentIndex >= 0) { HeapNode <T> parentNode = allNodes[parentIndex]; HeapNode <T> currentNode = allNodes[currentIndex]; if (parentNode.Weight > currentNode.Weight) { SwapandUpdate(parentIndex, currentIndex); currentIndex = parentIndex; parentIndex = (currentIndex - 1) / 2; } else { break; } } }