public static void DeliteMin(this HeapD heap) { int name0 = heap.names[0]; int key0 = heap.keys[0]; heap.names[0] = heap.names[heap.countNotVisit - 1]; heap.keys[0] = heap.keys[heap.countNotVisit - 1]; heap.names[heap.countNotVisit - 1] = name0; heap.keys[heap.countNotVisit - 1] = key0; heap.countNotVisit--; if (heap.countNotVisit > 0) { heap.Dive(0); } }
public void Test1() { int length = 4; heap = new HeapD(sizeHeap, length); result = new Result(length); graph = new Node[length]; graph[0] = new Node(2, 5, 2, new Node(1, 1, 1, null)); graph[1] = new Node(0, 1, 1, new Node(2, 5, 5, null)); graph[2] = new Node(0, 2, 5, new Node(1, 5, 5, new Node(3, 2, 4, new Node(3, 5, 5, null)))); graph[3] = new Node(2, 4, 2, new Node(2, 5, 5, null)); }
//погружение public static void Dive(this HeapD heap, int thisIndex) { int indexChild = heap.GetIndexMinChild(thisIndex); int thisKey = heap.keys[thisIndex]; int thisName = heap.names[thisIndex]; int i = thisIndex; while (indexChild != i && thisKey > heap.keys[indexChild]) { heap.keys[i] = heap.keys[indexChild]; heap.names[i] = heap.names[indexChild]; heap.index[heap.names[i]] = i; i = indexChild; indexChild = heap.GetIndexMinChild(i); } heap.keys[i] = thisKey; heap.names[i] = thisName; heap.index[heap.names[i]] = i; }
//всплытие public static void Emersion(this HeapD heap, int thisIndex) { int indexFather = GetIndexFather(thisIndex, heap.d); int thisKey = heap.keys[thisIndex]; int thisName = heap.names[thisIndex]; int i = thisIndex; while (i != 0 && heap.keys[indexFather] > thisKey) { heap.keys[i] = heap.keys[indexFather]; heap.names[i] = heap.names[indexFather]; heap.index[heap.names[i]] = i; i = indexFather; indexFather = GetIndexFather(i, heap.d); } heap.keys[i] = thisKey; heap.names[i] = thisName; heap.index[heap.names[i]] = i; }
public static int GetIndexMinChild(this HeapD heap, int thisIndex) { int indexFisrtChild = GetIndexFirstChild(heap.countNotVisit, heap.d, thisIndex); int result = indexFisrtChild; if (result != -1) { int indexLastChild = GetIndexLastChild(heap.countNotVisit, heap.d, thisIndex); int minKey = heap.keys[indexFisrtChild]; for (int i = indexFisrtChild + 1; i <= indexLastChild; i++) { if (heap.keys[i] < minKey) { minKey = heap.keys[i]; result = i; } } } else { result = thisIndex; } return(result); }