Add C# code for the chapter Heap and Graph (#324)

* add : C# heap ,graph, fix type "sift"=>"shift"

* chore: rename "shift" to "sift"

* add: heap,graph C# sample code ,fix format

* fix md format

* fix md intend foramt

* fix basic_operation_of_graph.md format

* fix md format

* fix md format

* fix indentation format

* chore: fix my_heap.cs test

* fix: test and doc typo

* fix bug for commit 5eae708 (#317)

* Add Zig code blocks.

* fix: resolve build error for commit 5eae708 (#318)

* Unify the function naming of
queue from `offer()` to `push()`

* Update TypeScript codes.

* Update binary_search_tree

* Update graph operations.

* Fix code indentation.

* Update worst_best_time_complexity,
leetcode_two_sum

* Update avl_tree

* copy zig codes of chapter_array_and_linkedlist and chapter_computatio… (#319)

* copy zig codes of chapter_array_and_linkedlist and chapter_computational_complexity to markdown files

* Update time_complexity.md

---------

Co-authored-by: Yudong Jin <krahets@163.com>

* Fix Python code styles.
Update hash_map.

* chore: fix heap logic

* Update graph_adjacency_matrix.cs

* Update graph_adjacency_matrix.cs

* Update my_heap.cs

* fix: heap test

* fix naming format

* merge markdown

* fix markdown format

* Update graph_adjacency_list.cs

* Update graph_adjacency_matrix.cs

* Update PrintUtil.cs

* Create Vertex.cs

* Update heap.cs

---------

Co-authored-by: zjkung1123 <zjkung1123@fugle.tw>
Co-authored-by: sjinzh <99076655+sjinzh@users.noreply.github.com>
Co-authored-by: Yudong Jin <krahets@163.com>
Co-authored-by: nuomi1 <nuomi1@qq.com>
This commit is contained in:
ZJKung
2023-02-15 21:24:24 +08:00
committed by GitHub
parent a383cb7ddf
commit f0b092fec4
7 changed files with 652 additions and 5 deletions

View File

@ -226,7 +226,39 @@ comments: true
=== "C#"
```csharp title="heap.cs"
/* 初始化堆 */
// 初始化小顶堆
PriorityQueue<int, int> minHeap = new PriorityQueue<int, int>();
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
PriorityQueue<int, int> maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));
/* 元素入堆 */
maxHeap.Enqueue(1, 1);
maxHeap.Enqueue(3, 3);
maxHeap.Enqueue(2, 2);
maxHeap.Enqueue(5, 5);
maxHeap.Enqueue(4, 4);
/* 获取堆顶元素 */
int peek = maxHeap.Peek();//5
/* 堆顶元素出堆 */
// 出堆元素会形成一个从大到小的序列
peek = maxHeap.Dequeue(); // 5
peek = maxHeap.Dequeue(); // 4
peek = maxHeap.Dequeue(); // 3
peek = maxHeap.Dequeue(); // 2
peek = maxHeap.Dequeue(); // 1
/* 获取堆大小 */
int size = maxHeap.Count;
/* 判断堆是否为空 */
bool isEmpty = maxHeap.Count == 0;
/* 输入列表并建堆 */
minHeap = new PriorityQueue<int, int>(new List<(int, int)> { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), });
```
=== "Swift"