fix(csharp): Modify method name to PascalCase, simplify new expression (#840)

* Modify method name to PascalCase(array and linked list)

* Modify method name to PascalCase(backtracking)

* Modify method name to PascalCase(computational complexity)

* Modify method name to PascalCase(divide and conquer)

* Modify method name to PascalCase(dynamic programming)

* Modify method name to PascalCase(graph)

* Modify method name to PascalCase(greedy)

* Modify method name to PascalCase(hashing)

* Modify method name to PascalCase(heap)

* Modify method name to PascalCase(searching)

* Modify method name to PascalCase(sorting)

* Modify method name to PascalCase(stack and queue)

* Modify method name to PascalCase(tree)

* local check
This commit is contained in:
hpstory
2023-10-08 01:33:46 +08:00
committed by GitHub
parent 6f7e768cb7
commit f62256bee1
129 changed files with 1186 additions and 1192 deletions
+15 -14
View File
@@ -7,32 +7,33 @@
namespace hello_algo.chapter_heap;
public class heap {
public void testPush(PriorityQueue<int, int> heap, int val) {
public void TestPush(PriorityQueue<int, int> heap, int val) {
heap.Enqueue(val, val); // 元素入堆
Console.WriteLine($"\n元素 {val} 入堆后\n");
PrintUtil.PrintHeap(heap);
}
public void testPop(PriorityQueue<int, int> heap) {
public void TestPop(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>();
PriorityQueue<int, int> minHeap = new();
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
PriorityQueue<int, int> maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));
Console.WriteLine("以下测试样例为大顶堆");
/* 元素入堆 */
testPush(maxHeap, 1);
testPush(maxHeap, 3);
testPush(maxHeap, 2);
testPush(maxHeap, 5);
testPush(maxHeap, 4);
TestPush(maxHeap, 1);
TestPush(maxHeap, 3);
TestPush(maxHeap, 2);
TestPush(maxHeap, 5);
TestPush(maxHeap, 4);
/* 获取堆顶元素 */
int peek = maxHeap.Peek();
@@ -40,11 +41,11 @@ public class heap {
/* 堆顶元素出堆 */
// 出堆元素会形成一个从大到小的序列
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
/* 获取堆大小 */
int size = maxHeap.Count;
+35 -35
View File
@@ -21,106 +21,106 @@ class MaxHeap {
// 将列表元素原封不动添加进堆
maxHeap = new List<int>(nums);
// 堆化除叶节点以外的其他所有节点
var size = parent(this.size() - 1);
var size = Parent(this.Size() - 1);
for (int i = size; i >= 0; i--) {
siftDown(i);
SiftDown(i);
}
}
/* 获取左子节点索引 */
int left(int i) {
int Left(int i) {
return 2 * i + 1;
}
/* 获取右子节点索引 */
int right(int i) {
int Right(int i) {
return 2 * i + 2;
}
/* 获取父节点索引 */
int parent(int i) {
int Parent(int i) {
return (i - 1) / 2; // 向下整除
}
/* 访问堆顶元素 */
public int peek() {
public int Peek() {
return maxHeap[0];
}
/* 元素入堆 */
public void push(int val) {
public void Push(int val) {
// 添加节点
maxHeap.Add(val);
// 从底至顶堆化
siftUp(size() - 1);
SiftUp(Size() - 1);
}
/* 获取堆大小 */
public int size() {
public int Size() {
return maxHeap.Count;
}
/* 判断堆是否为空 */
public bool isEmpty() {
return size() == 0;
public bool IsEmpty() {
return Size() == 0;
}
/* 从节点 i 开始,从底至顶堆化 */
void siftUp(int i) {
void SiftUp(int i) {
while (true) {
// 获取节点 i 的父节点
int p = parent(i);
int p = Parent(i);
// 若“越过根节点”或“节点无须修复”,则结束堆化
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
// 交换两节点
swap(i, p);
Swap(i, p);
// 循环向上堆化
i = p;
}
}
/* 元素出堆 */
public int pop() {
public int Pop() {
// 判空处理
if (isEmpty())
if (IsEmpty())
throw new IndexOutOfRangeException();
// 交换根节点与最右叶节点(即交换首元素与尾元素)
swap(0, size() - 1);
Swap(0, Size() - 1);
// 删除节点
int val = maxHeap.Last();
maxHeap.RemoveAt(size() - 1);
maxHeap.RemoveAt(Size() - 1);
// 从顶至底堆化
siftDown(0);
SiftDown(0);
// 返回堆顶元素
return val;
}
/* 从节点 i 开始,从顶至底堆化 */
void siftDown(int 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])
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])
if (r < Size() && maxHeap[r] > maxHeap[ma])
ma = r;
// 若“节点 i 最大”或“越过叶节点”,则结束堆化
if (ma == i) break;
// 交换两节点
swap(i, ma);
Swap(i, ma);
// 循环向下堆化
i = ma;
}
}
/* 交换元素 */
void swap(int i, int p) {
void Swap(int i, int p) {
(maxHeap[i], maxHeap[p]) = (maxHeap[p], maxHeap[i]);
}
/* 打印堆(二叉树) */
public void print() {
public void Print() {
var queue = new Queue<int>(maxHeap);
PrintUtil.PrintHeap(queue);
}
@@ -130,31 +130,31 @@ 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 });
MaxHeap maxHeap = new(new int[] { 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
Console.WriteLine("\n输入列表并建堆后");
maxHeap.print();
maxHeap.Print();
/* 获取堆顶元素 */
int peek = maxHeap.peek();
int peek = maxHeap.Peek();
Console.WriteLine($"堆顶元素为 {peek}");
/* 元素入堆 */
int val = 7;
maxHeap.push(val);
maxHeap.Push(val);
Console.WriteLine($"元素 {val} 入堆后");
maxHeap.print();
maxHeap.Print();
/* 堆顶元素出堆 */
peek = maxHeap.pop();
peek = maxHeap.Pop();
Console.WriteLine($"堆顶元素 {peek} 出堆后");
maxHeap.print();
maxHeap.Print();
/* 获取堆大小 */
int size = maxHeap.size();
int size = maxHeap.Size();
Console.WriteLine($"堆元素数量为 {size}");
/* 判断堆是否为空 */
bool isEmpty = maxHeap.isEmpty();
bool isEmpty = maxHeap.IsEmpty();
Console.WriteLine($"堆是否为空 {isEmpty}");
}
}
+3 -3
View File
@@ -8,8 +8,8 @@ namespace hello_algo.chapter_heap;
public class top_k {
/* 基于堆查找数组中最大的 k 个元素 */
public static PriorityQueue<int, int> topKHeap(int[] nums, int k) {
PriorityQueue<int, int> heap = new PriorityQueue<int, int>();
public static PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
PriorityQueue<int, int> heap = new();
// 将数组的前 k 个元素入堆
for (int i = 0; i < k; i++) {
heap.Enqueue(nums[i], nums[i]);
@@ -29,7 +29,7 @@ public class top_k {
public void Test() {
int[] nums = { 1, 7, 6, 3, 2 };
int k = 3;
PriorityQueue<int, int> res = topKHeap(nums, k);
PriorityQueue<int, int> res = TopKHeap(nums, k);
Console.WriteLine("最大的 " + k + " 个元素为");
PrintUtil.PrintHeap(res);
}