From 1f5071f31859496cfce153664418fd042b18a35a Mon Sep 17 00:00:00 2001 From: syneffort Date: Wed, 19 Apr 2023 09:48:51 +0900 Subject: [PATCH] heap --- .vscode/launch.json | 17 ++++ .vscode/tasks.json | 6 +- DataStructure/heapStructure/Program.cs | 19 ++++- DataStructure/heapStructure/Structure/Heap.cs | 85 +++++++++++++++++++ .../heapStructure/Structure/HeapClient.cs | 22 +++++ 5 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 DataStructure/heapStructure/Structure/Heap.cs create mode 100644 DataStructure/heapStructure/Structure/HeapClient.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 834caad..6bba057 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,23 @@ // 자세한 내용을 보려면 https://go.microsoft.com/fwlink/?linkid=830387을(를) 방문하세요. "version": "0.2.0", "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": "heapStructure (Console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/DataStructure/heapStructure/bin/Debug/net7.0/heapStructure.dll", + "args": [], + "cwd": "${workspaceFolder}/DataStructure/heapStructure", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { // Use IntelliSense to find out which attributes exist for C# debugging // Use hover for the description of the existing attributes diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a730df7..fc34f3b 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,7 +7,7 @@ "type": "process", "args": [ "build", - "${workspaceFolder}/DataStructure/graphStructure/graphStructure.csproj", + "${workspaceFolder}/DataStructure/heapStructure/heapStructure.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], @@ -19,7 +19,7 @@ "type": "process", "args": [ "publish", - "${workspaceFolder}/DataStructure/treeStructure/treeStructure.csproj", + "${workspaceFolder}/DataStructure/heapStructure/heapStructure.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], @@ -33,7 +33,7 @@ "watch", "run", "--project", - "${workspaceFolder}/DataStructure/hashTableStructure/hashTableStructure.csproj" + "${workspaceFolder}/DataStructure/heapStructure/heapStructure.csproj" ], "problemMatcher": "$msCompile" } diff --git a/DataStructure/heapStructure/Program.cs b/DataStructure/heapStructure/Program.cs index 3751555..914c939 100644 --- a/DataStructure/heapStructure/Program.cs +++ b/DataStructure/heapStructure/Program.cs @@ -1,2 +1,17 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using MainNamespace.Structure; + +namespace MainNamespace +{ + static class Program + { + static void Main(string[] args) + { + System.Console.WriteLine("---- Max Heap ---"); + HeapClient.DoWork(true); + + System.Console.WriteLine(); + System.Console.WriteLine("--- Min Heap ---"); + HeapClient.DoWork(false); + } + } +} \ No newline at end of file diff --git a/DataStructure/heapStructure/Structure/Heap.cs b/DataStructure/heapStructure/Structure/Heap.cs new file mode 100644 index 0000000..5a28650 --- /dev/null +++ b/DataStructure/heapStructure/Structure/Heap.cs @@ -0,0 +1,85 @@ +namespace MainNamespace.Structure +{ + class Heap + { + private List _nodeList; + + private Comparer _comparer = Comparer.Default; + private bool _maxHeap = true; + private readonly int COMPARER_SIGN = 1; + + public bool MaxHeap { get { return _maxHeap; } } + + public Heap(bool maxHeap = true) + { + _nodeList = new List(); + _maxHeap = maxHeap; + COMPARER_SIGN = maxHeap ? 1 : -1; + } + public void Add(T value) + { + _nodeList.Add(value); + + // bubble up + int i = _nodeList.Count - 1; + while (i > 0) + { + int parent = (i - 1) / 2; + if (_comparer.Compare(_nodeList[parent], _nodeList[i]) * COMPARER_SIGN >= 0) + break; + + Swap(parent, i); + i = parent; + } + } + + public T RemoveOne() + { + if (_nodeList.Count == 0) + throw new InvalidOperationException(); + + T root = _nodeList[0]; + + // move last to first and remove last one + _nodeList[0] = _nodeList[_nodeList.Count - 1]; + _nodeList.RemoveAt(_nodeList.Count - 1); + + int i = 0; + int last = _nodeList.Count - 1; + + // bubble down + while (i < last) + { + // left child index + int child = i * 2 + 1; + + // use right child if it is bigger + if (child < last && + _comparer.Compare(_nodeList[child], _nodeList[child + 1]) * COMPARER_SIGN < 0) + child = child + 1; + + // if parent is bigger or equal, than stop + // if (!MaxHeap) + // compareResult *= -1; + if (child > last || + _comparer.Compare(_nodeList[i], _nodeList[child]) * COMPARER_SIGN >= 0) + break; + + // swap parent and child + Swap(i, child); + i = child; + } + + return root; + } + + public int Count { get { return _nodeList.Count; } } + + private void Swap(int i, int j) + { + T temp = _nodeList[i]; + _nodeList[i] = _nodeList[j]; + _nodeList[j] = temp; + } + } +} \ No newline at end of file diff --git a/DataStructure/heapStructure/Structure/HeapClient.cs b/DataStructure/heapStructure/Structure/HeapClient.cs new file mode 100644 index 0000000..6481a55 --- /dev/null +++ b/DataStructure/heapStructure/Structure/HeapClient.cs @@ -0,0 +1,22 @@ +namespace MainNamespace.Structure +{ + class HeapClient + { + public static void DoWork(bool maxHeap = true) + { + Heap heap = new Heap(maxHeap); + heap.Add(11); + heap.Add(15); + heap.Add(3); + heap.Add(5); + heap.Add(9); + heap.Add(7); + heap.Add(1); + + while (heap.Count > 0) + { + System.Console.WriteLine(heap.RemoveOne()); + } + } + } +} \ No newline at end of file