Skip to content

Commit 2c6e29f

Browse files
committed
Kickoff
1 parent 2c27413 commit 2c6e29f

11 files changed

+1299
-0
lines changed

.editorconfig

+462
Large diffs are not rendered by default.

.gitignore

+480
Large diffs are not rendered by default.

.vscode/launch.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "MaxHeap",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceFolder}/src/Demo.MaxHeap/bin/Debug/net8.0/Demo.MaxHeap.dll",
10+
"args": [],
11+
"cwd": "${workspaceFolder}/src/Demo.MaxHeap",
12+
"console": "internalConsole",
13+
"stopAtEntry": false
14+
},
15+
{
16+
"name": "MinHeap",
17+
"type": "coreclr",
18+
"request": "launch",
19+
"preLaunchTask": "build",
20+
"program": "${workspaceFolder}/src/Demo.MinHeap/bin/Debug/net8.0/Demo.MinHeap.dll",
21+
"args": [],
22+
"cwd": "${workspaceFolder}/src/Demo.MinHeap",
23+
"console": "internalConsole",
24+
"stopAtEntry": false
25+
},
26+
{
27+
"name": "QuaternaryMinHeap",
28+
"type": "coreclr",
29+
"request": "launch",
30+
"preLaunchTask": "build",
31+
"program": "${workspaceFolder}/src/Demo.QuaternaryMinHeap/bin/Debug/net8.0/Demo.QuaternaryMinHeap.dll",
32+
"args": [],
33+
"cwd": "${workspaceFolder}/src/Demo.QuaternaryMinHeap",
34+
"console": "internalConsole",
35+
"stopAtEntry": false
36+
}
37+
]
38+
}

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dotnet.defaultSolution": "algorithms-data-structures-heap.sln"
3+
}

.vscode/tasks.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/algorithms-data-structures-binary-heap.sln",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/algorithms-data-structures-binary-heap.sln",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/algorithms-data-structures-binary-heap.sln"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Algorithms and Data Structures - Heap
2+
3+
// TODO: in progress

algorithms-data-structures-heap.sln

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.MaxHeap", "src\Demo.MaxHeap\Demo.MaxHeap.csproj", "{B5E42ECC-D291-448E-94C5-739885D8F82D}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.MinHeap", "src\Demo.MinHeap\Demo.MinHeap.csproj", "{41A362EB-AEC6-45DA-9E0B-1B2EAABB8C6A}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B5E42ECC-D291-448E-94C5-739885D8F82D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B5E42ECC-D291-448E-94C5-739885D8F82D}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B5E42ECC-D291-448E-94C5-739885D8F82D}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B5E42ECC-D291-448E-94C5-739885D8F82D}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{41A362EB-AEC6-45DA-9E0B-1B2EAABB8C6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{41A362EB-AEC6-45DA-9E0B-1B2EAABB8C6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{41A362EB-AEC6-45DA-9E0B-1B2EAABB8C6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{41A362EB-AEC6-45DA-9E0B-1B2EAABB8C6A}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal

src/Demo.MaxHeap/Demo.MaxHeap.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/Demo.MaxHeap/Program.cs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using static System.Console;
2+
3+
var maxHeap = new MaxHeap();
4+
5+
6+
for(var i = 0; i < 10; i++)
7+
{
8+
maxHeap.Push(Random.Shared.Next(0, 100));
9+
}
10+
11+
while(maxHeap.Count > 0)
12+
{
13+
WriteLine(maxHeap.Pop());
14+
}
15+
16+
17+
18+
public class MaxHeap
19+
{
20+
public int Count { get; private set; }
21+
22+
private int[] _heap;
23+
private int _size;
24+
25+
public MaxHeap(int size = 4)
26+
{
27+
Count = 0;
28+
_size = size;
29+
_heap = new int[_size];
30+
}
31+
32+
private static int _parent(int index)
33+
=> (index - 1) / 2;
34+
35+
private static int _leftChild(int index)
36+
=> (index << 1) + 1;
37+
38+
private static int _rightChild(int index)
39+
=> (index << 1) + 2;
40+
41+
private bool _isBigger(int firstIndex, int secondIndex)
42+
=> _heap[firstIndex] > _heap[secondIndex];
43+
44+
private void _swap(int firstIndex, int secondIndex)
45+
=> (_heap[firstIndex], _heap[secondIndex]) = (_heap[secondIndex], _heap[firstIndex]);
46+
47+
private void _resize()
48+
{
49+
var newHeap = new int[_size * 2];
50+
Array.Copy(_heap, newHeap, _size);
51+
_heap = newHeap;
52+
_size *= 2;
53+
}
54+
55+
public void Push(int element)
56+
{
57+
if(Count >= _size)
58+
{
59+
_resize();
60+
}
61+
62+
_heap[Count] = element;
63+
64+
var current = Count;
65+
var parent = _parent(current);
66+
67+
while(_isBigger(current, parent))
68+
{
69+
_swap(current, parent);
70+
current = parent;
71+
parent = _parent(current);
72+
}
73+
Count++;
74+
}
75+
76+
77+
public int Pop()
78+
{
79+
if(Count <= 0)
80+
{
81+
throw new InvalidOperationException("Heap is empty");
82+
}
83+
84+
Count--;
85+
var result = _heap[0];
86+
_heap[0] = _heap[Count];
87+
88+
89+
var current = 0;
90+
var leftChild = _leftChild(current);
91+
var rightChild = _rightChild(current);
92+
93+
while(leftChild < Count)
94+
{
95+
var smallestChild = rightChild < Count && _isBigger(rightChild, leftChild) ?
96+
rightChild :
97+
leftChild;
98+
99+
if(_isBigger(current, smallestChild))
100+
{
101+
break;
102+
}
103+
104+
_swap(current, smallestChild);
105+
current = smallestChild;
106+
leftChild = _leftChild(current);
107+
rightChild = _rightChild(current);
108+
}
109+
110+
return result;
111+
}
112+
}

src/Demo.MinHeap/Demo.MinHeap.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/Demo.MinHeap/Program.cs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using static System.Console;
2+
3+
var minHeap = new MinHeap(10);
4+
5+
minHeap.Push(20);
6+
minHeap.Push(30);
7+
minHeap.Push(300);
8+
minHeap.Push(1);
9+
minHeap.Push(40);
10+
minHeap.Push(5);
11+
minHeap.Push(7);
12+
minHeap.Push(9);
13+
minHeap.Push(9);
14+
minHeap.Push(19);
15+
16+
WriteLine(minHeap.Pop());
17+
WriteLine(minHeap.Pop());
18+
WriteLine(minHeap.Pop());
19+
WriteLine(minHeap.Pop());
20+
WriteLine(minHeap.Pop());
21+
WriteLine(minHeap.Pop());
22+
WriteLine(minHeap.Pop());
23+
WriteLine(minHeap.Pop());
24+
WriteLine(minHeap.Pop());
25+
WriteLine(minHeap.Pop());
26+
27+
28+
public class MinHeap
29+
{
30+
private readonly int[] _heap;
31+
private readonly int _maxSize;
32+
private int _size;
33+
34+
public MinHeap(int maxSize)
35+
{
36+
_maxSize = maxSize;
37+
_size = 0;
38+
_heap = new int[_maxSize];
39+
}
40+
41+
private static int _parent(int index)
42+
=> (index - 1) / 2;
43+
44+
private static int _leftChild(int index)
45+
=> (index * 2) + 1;
46+
47+
private static int _rightChild(int index)
48+
=> (index * 2) + 2;
49+
50+
private bool _isSmaller(int firstIndex, int secondIndex)
51+
=> _heap[firstIndex] < _heap[secondIndex];
52+
53+
private void _swap(int firstIndex, int secondIndex)
54+
=> (_heap[firstIndex], _heap[secondIndex]) = (_heap[secondIndex], _heap[firstIndex]);
55+
56+
public void Push(int element)
57+
{
58+
if(_size >= _maxSize)
59+
{
60+
throw new InvalidOperationException("Heap is full");
61+
}
62+
63+
_heap[_size] = element;
64+
65+
var current = _size;
66+
var parent = _parent(current);
67+
68+
while(_isSmaller(current, parent))
69+
{
70+
_swap(current, parent);
71+
current = parent;
72+
parent = _parent(current);
73+
}
74+
_size++;
75+
}
76+
77+
public int Peek()
78+
=> _size > 0 ? _heap[0] : throw new InvalidOperationException("Heap is empty");
79+
80+
public int Pop()
81+
{
82+
if(_size <= 0)
83+
{
84+
throw new InvalidOperationException("Heap is empty");
85+
}
86+
87+
_size--;
88+
var result = _heap[0];
89+
_heap[0] = _heap[_size];
90+
91+
92+
var current = 0;
93+
var leftChild = _leftChild(current);
94+
var rightChild = _rightChild(current);
95+
96+
while(leftChild < _size)
97+
{
98+
var smallestChild = rightChild < _size && _isSmaller(rightChild, leftChild) ? rightChild : leftChild;
99+
if(_isSmaller(current, smallestChild))
100+
{
101+
break;
102+
}
103+
104+
_swap(current, smallestChild);
105+
current = smallestChild;
106+
leftChild = _leftChild(current);
107+
rightChild = _rightChild(current);
108+
}
109+
110+
return result;
111+
}
112+
}

0 commit comments

Comments
 (0)