-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDURAKHeapNode.cs
More file actions
98 lines (78 loc) · 3.01 KB
/
DURAKHeapNode.cs
File metadata and controls
98 lines (78 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Text;
namespace Proje3
{
class DurakNode
{
private Durak dataItem;
public DurakNode(Durak key)
{ dataItem = key; }
public Durak getKey()
{ return dataItem; }
public void setKey(Durak id)
{ dataItem = id; }
}
class DurakHeap
{
private DurakNode[] heapDizi;
private int maksBoyut;
private int anlıkBoyut;
public DurakHeap(int maksboyut)
{
maksBoyut = maksboyut;
anlıkBoyut = 0;
heapDizi = new DurakNode[maksBoyut];
}
public bool isEmpty()
{ return anlıkBoyut == 0; }
public bool insert(Durak key)
{
if (anlıkBoyut == maksBoyut)
return false;
DurakNode node = new DurakNode(key);
heapDizi[anlıkBoyut] = node;
trickleUp(anlıkBoyut++);
return true;
}
public void trickleUp(int indeks)
{
int parent = (indeks - 1) / 2;
DurakNode altNode = heapDizi[indeks];
while (indeks > 0 && heapDizi[parent].getKey().NB < altNode.getKey().NB)
{
heapDizi[indeks] = heapDizi[parent];
indeks = parent;
parent = (parent - 1) / 2;
}
heapDizi[indeks] = altNode;
}
public DurakNode remove()
{
DurakNode root = heapDizi[0];
heapDizi[0] = heapDizi[--anlıkBoyut];
trickleDown(0);
return root;
}
public void trickleDown(int indeks)
{
int enBüyükChild;
DurakNode root2 = heapDizi[indeks];
while (indeks < anlıkBoyut / 2)
{
int leftChild = 2 * indeks + 1;
int rightChild = leftChild + 1;
//
if (rightChild < anlıkBoyut && heapDizi[leftChild].getKey().NB < heapDizi[rightChild].getKey().NB)
{ enBüyükChild = rightChild; }
else
enBüyükChild = leftChild;
if (root2.getKey().NB >= heapDizi[enBüyükChild].getKey().NB)
break;
heapDizi[indeks] = heapDizi[enBüyükChild];
indeks = enBüyükChild;
}
heapDizi[indeks] = root2;
}
}
}