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 commitpull/381/head5eae708
(#317) * Add Zig code blocks. * fix: resolve build error for commit5eae708
(#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>
parent
a383cb7ddf
commit
f0b092fec4
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* File: heap.cs
|
||||
* Created Time: 2023-02-06
|
||||
* Author: zjkung1123 (zjkung1123@gmail.com)
|
||||
*/
|
||||
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_heap;
|
||||
|
||||
public class heap
|
||||
{
|
||||
public void testPush(PriorityQueue<int, int> heap, int val)
|
||||
{
|
||||
heap.Enqueue(val, val); // 元素入堆
|
||||
Console.WriteLine($"\n元素 {val} 入堆后\n");
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
|
||||
public void testPoll(PriorityQueue<int, int> heap)
|
||||
{
|
||||
int val = heap.Dequeue(); // 堆顶元素出堆
|
||||
Console.WriteLine($"\n堆顶元素 {val} 出堆后\n");
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
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));
|
||||
Console.WriteLine("以下测试样例为大顶堆");
|
||||
|
||||
/* 元素入堆 */
|
||||
testPush(maxHeap, 1);
|
||||
testPush(maxHeap, 3);
|
||||
testPush(maxHeap, 2);
|
||||
testPush(maxHeap, 5);
|
||||
testPush(maxHeap, 4);
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
int peek = maxHeap.Peek();
|
||||
Console.WriteLine($"堆顶元素为 {peek}");
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
// 出堆元素会形成一个从大到小的序列
|
||||
testPoll(maxHeap);
|
||||
testPoll(maxHeap);
|
||||
testPoll(maxHeap);
|
||||
testPoll(maxHeap);
|
||||
testPoll(maxHeap);
|
||||
|
||||
/* 获取堆大小 */
|
||||
int size = maxHeap.Count;
|
||||
Console.WriteLine($"堆元素数量为 {size}");
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
bool isEmpty = maxHeap.Count == 0;
|
||||
Console.WriteLine($"堆是否为空 {isEmpty}");
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
var list = new int[] { 1, 3, 2, 5, 4 };
|
||||
minHeap = new PriorityQueue<int, int>(list.Select(x => (x, x)));
|
||||
Console.WriteLine("输入列表并建立小顶堆后");
|
||||
PrintUtil.printHeap(minHeap);
|
||||
}
|
||||
}
|
@ -0,0 +1,183 @@
|
||||
/**
|
||||
* File: my_heap.cs
|
||||
* Created Time: 2023-02-06
|
||||
* Author: zjkung1123 (zjkung1123@gmail.com)
|
||||
*/
|
||||
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_heap;
|
||||
|
||||
/* 大顶堆 */
|
||||
class MaxHeap
|
||||
{
|
||||
// 使用列表而非数组,这样无需考虑扩容问题
|
||||
private readonly List<int> maxHeap;
|
||||
|
||||
/* 构造函数,建立空堆 */
|
||||
public MaxHeap()
|
||||
{
|
||||
maxHeap = new List<int>();
|
||||
}
|
||||
|
||||
/* 构造函数,根据输入列表建堆 */
|
||||
public MaxHeap(IEnumerable<int> nums)
|
||||
{
|
||||
// 将列表元素原封不动添加进堆
|
||||
maxHeap = new List<int>(nums);
|
||||
// 堆化除叶结点以外的其他所有结点
|
||||
var size = parent(this.size() - 1);
|
||||
for (int i = size; i >= 0; i--)
|
||||
{
|
||||
siftDown(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取左子结点索引 */
|
||||
int left(int i)
|
||||
{
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 获取右子结点索引 */
|
||||
int right(int i)
|
||||
{
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父结点索引 */
|
||||
int parent(int i)
|
||||
{
|
||||
return (i - 1) / 2; // 向下整除
|
||||
}
|
||||
|
||||
/* 访问堆顶元素 */
|
||||
public int peek()
|
||||
{
|
||||
return maxHeap[0];
|
||||
}
|
||||
|
||||
/* 元素入堆 */
|
||||
public void push(int val)
|
||||
{
|
||||
// 添加结点
|
||||
maxHeap.Add(val);
|
||||
// 从底至顶堆化
|
||||
siftUp(size() - 1);
|
||||
}
|
||||
|
||||
/* 获取堆大小 */
|
||||
public int size()
|
||||
{
|
||||
return maxHeap.Count;
|
||||
}
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 从结点 i 开始,从底至顶堆化 */
|
||||
void siftUp(int i)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// 获取结点 i 的父结点
|
||||
int p = parent(i);
|
||||
// 若“越过根结点”或“结点无需修复”,则结束堆化
|
||||
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
||||
break;
|
||||
// 交换两结点
|
||||
swap(i, p);
|
||||
// 循环向上堆化
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* 元素出堆 */
|
||||
public int poll()
|
||||
{
|
||||
// 判空处理
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfRangeException();
|
||||
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
||||
swap(0, size() - 1);
|
||||
// 删除结点
|
||||
int val = maxHeap.Last();
|
||||
maxHeap.RemoveAt(size() - 1);
|
||||
// 从顶至底堆化
|
||||
siftDown(0);
|
||||
// 返回堆顶元素
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 从结点 i 开始,从顶至底堆化 */
|
||||
void siftDown(int i)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// 判断结点 i, l, r 中值最大的结点,记为 ma
|
||||
int l = left(i), r = right(i), ma = i;
|
||||
if (l < size() && maxHeap[l] > maxHeap[ma])
|
||||
ma = l;
|
||||
if (r < size() && maxHeap[r] > maxHeap[ma])
|
||||
ma = r;
|
||||
// 若“结点 i 最大”或“越过叶结点”,则结束堆化
|
||||
if (ma == i) break;
|
||||
// 交换两结点
|
||||
swap(i, ma);
|
||||
// 循环向下堆化
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* 交换元素 */
|
||||
void swap(int i, int p)
|
||||
{
|
||||
(maxHeap[i], maxHeap[p]) = (maxHeap[p], maxHeap[i]);
|
||||
}
|
||||
|
||||
/* 打印堆(二叉树) */
|
||||
public void print()
|
||||
{
|
||||
var queue = new Queue<int>(maxHeap);
|
||||
PrintUtil.printHeap(queue);
|
||||
}
|
||||
}
|
||||
|
||||
public class my_heap
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化大顶堆 */
|
||||
MaxHeap maxHeap = new MaxHeap(new int[] { 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
|
||||
Console.WriteLine("\n输入列表并建堆后");
|
||||
maxHeap.print();
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
int peek = maxHeap.peek();
|
||||
Console.WriteLine($"堆顶元素为 {peek}");
|
||||
|
||||
/* 元素入堆 */
|
||||
int val = 7;
|
||||
maxHeap.push(val);
|
||||
Console.WriteLine($"元素 {val} 入堆后");
|
||||
maxHeap.print();
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
peek = maxHeap.poll();
|
||||
Console.WriteLine($"堆顶元素 {peek} 出堆后");
|
||||
maxHeap.print();
|
||||
|
||||
/* 获取堆大小 */
|
||||
int size = maxHeap.size();
|
||||
Console.WriteLine($"堆元素数量为 {size}");
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
bool isEmpty = maxHeap.isEmpty();
|
||||
Console.WriteLine($"堆是否为空 {isEmpty}");
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: graph_adjacency_list.cs
|
||||
* Created Time: 2023-02-06
|
||||
* Author: zjkung1123 (zjkung1123@gmail.com), krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
/* 顶点类 */
|
||||
class Vertex
|
||||
{
|
||||
public int Val { get; init; }
|
||||
public Vertex(int val)
|
||||
{
|
||||
Val = val;
|
||||
}
|
||||
|
||||
/* 输入值列表 vals ,返回顶点列表 vets */
|
||||
public static Vertex[] valsToVets(int[] vals)
|
||||
{
|
||||
Vertex[] vets = new Vertex[vals.Length];
|
||||
for (int i = 0; i < vals.Length; i++)
|
||||
{
|
||||
vets[i] = new Vertex(vals[i]);
|
||||
}
|
||||
return vets;
|
||||
}
|
||||
|
||||
/* 输入顶点列表 vets ,返回值列表 vals */
|
||||
public static List<int> vetsToVals(List<Vertex> vets)
|
||||
{
|
||||
List<int> vals = new List<int>();
|
||||
foreach (Vertex vet in vets)
|
||||
{
|
||||
vals.Add(vet.Val);
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue