예제 #1
0
        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;
                }
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
        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;
                }
            }
        }