Merge branch 'master' into binary_search_tree

This commit is contained in:
Yudong Jin
2023-01-10 13:30:38 +08:00
committed by GitHub
215 changed files with 5618 additions and 1642 deletions
@@ -8,9 +8,7 @@ namespace hello_algo.chapter_array_and_linkedlist
{
public class Array
{
/// <summary>
/// 随机返回一个数组元素
/// </summary>
/* 随机返回一个数组元素 */
public static int RandomAccess(int[] nums)
{
Random random = new();
@@ -19,9 +17,7 @@ namespace hello_algo.chapter_array_and_linkedlist
return randomNum;
}
/// <summary>
/// 扩展数组长度
/// </summary>
/* 扩展数组长度 */
public static int[] Extend(int[] nums, int enlarge)
{
// 初始化一个扩展长度后的数组
@@ -35,9 +31,7 @@ namespace hello_algo.chapter_array_and_linkedlist
return res;
}
/// <summary>
/// 在数组的索引 index 处插入元素 num
/// </summary>
/* 在数组的索引 index 处插入元素 num */
public static void Insert(int[] nums, int num, int index)
{
// 把索引 index 以及之后的所有元素向后移动一位
@@ -49,9 +43,7 @@ namespace hello_algo.chapter_array_and_linkedlist
nums[index] = num;
}
/// <summary>
/// 删除索引 index 处元素
/// </summary>
/* 删除索引 index 处元素 */
public static void Remove(int[] nums, int index)
{
// 把索引 index 之后的所有元素向前移动一位
@@ -61,9 +53,7 @@ namespace hello_algo.chapter_array_and_linkedlist
}
}
/// <summary>
/// 遍历数组
/// </summary>
/* 遍历数组 */
public static void Traverse(int[] nums)
{
int count = 0;
@@ -79,9 +69,7 @@ namespace hello_algo.chapter_array_and_linkedlist
}
}
/// <summary>
/// 在数组中查找指定元素
/// </summary>
/* 在数组中查找指定元素 */
public static int Find(int[] nums, int target)
{
for (int i = 0; i < nums.Length; i++)
@@ -92,15 +80,13 @@ namespace hello_algo.chapter_array_and_linkedlist
return -1;
}
/// <summary>
/// 辅助函数,数组转字符串
/// </summary>
/* 辅助函数,数组转字符串 */
public static string ToString(int[] nums)
{
return string.Join(",", nums);
}
// Driver Code
[Test]
public static void Test()
{
@@ -9,9 +9,7 @@ namespace hello_algo.chapter_array_and_linkedlist
{
public class linked_list
{
/// <summary>
/// 在链表的结点 n0 之后插入结点 P
/// </summary>
/* 在链表的结点 n0 之后插入结点 P */
public static void Insert(ListNode n0, ListNode P)
{
ListNode? n1 = n0.next;
@@ -19,9 +17,7 @@ namespace hello_algo.chapter_array_and_linkedlist
P.next = n1;
}
/// <summary>
/// 删除链表的结点 n0 之后的首个结点
/// </summary>
/* 删除链表的结点 n0 之后的首个结点 */
public static void Remove(ListNode n0)
{
if (n0.next == null)
@@ -32,23 +28,19 @@ namespace hello_algo.chapter_array_and_linkedlist
n0.next = n1;
}
/// <summary>
/// 访问链表中索引为 index 的结点
/// </summary>
/* 访问链表中索引为 index 的结点 */
public static ListNode? Access(ListNode head, int index)
{
for (int i = 0; i < index; i++)
{
head = head.next;
if (head == null)
return null;
head = head.next;
}
return head;
}
/// <summary>
/// 在链表中查找值为 target 的首个结点
/// </summary>
/* 在链表中查找值为 target 的首个结点 */
public static int Find(ListNode head, int target)
{
int index = 0;
@@ -62,7 +54,7 @@ namespace hello_algo.chapter_array_and_linkedlist
return -1;
}
// Driver Code
[Test]
public void Test()
{
+2 -2
View File
@@ -31,10 +31,10 @@ namespace hello_algo.chapter_sorting
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
if (i > leftEnd)
nums[k] = tmp[j++];
// 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
else if (j > rightEnd || tmp[i] <= tmp[j])
nums[k] = tmp[i++];
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
else
nums[k] = tmp[j++];
}
@@ -0,0 +1,49 @@
/**
* File: deque.cs
* Created Time: 2022-12-30
* Author: moonache (microin1301@outlook.com)
*/
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue
{
public class deque
{
[Test]
public void Test()
{
/* 初始化双向队列 */
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
LinkedList<int> deque = new LinkedList<int>();
/* 元素入队 */
deque.AddLast(2); // 添加至队尾
deque.AddLast(5);
deque.AddLast(4);
deque.AddFirst(3); // 添加至队首
deque.AddFirst(1);
Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray()));
/* 访问元素 */
int peekFirst = deque.First.Value; // 队首元素
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
int peekLast = deque.Last.Value; // 队尾元素
Console.WriteLine("队尾元素 peekLast = " + peekLast);
/* 元素出队 */
deque.RemoveFirst(); // 队首元素出队
Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray()));
deque.RemoveLast(); // 队尾元素出队
Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray()));
/* 获取双向队列的长度 */
int size = deque.Count;
Console.WriteLine("双向队列长度 size = " + size);
/* 判断双向队列是否为空 */
bool isEmpty = deque.Count == 0;
Console.WriteLine("双向队列是否为空 = " + isEmpty);
}
}
}
+3 -3
View File
@@ -162,7 +162,7 @@ namespace hello_algo.chapter_tree
else
{
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
TreeNode? temp = minNode(node.right);
TreeNode? temp = getInOrderNext(node.right);
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
@@ -174,8 +174,8 @@ namespace hello_algo.chapter_tree
return node;
}
/* 获取最小结点 */
private TreeNode? minNode(TreeNode? node)
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
private TreeNode? getInOrderNext(TreeNode? node)
{
if (node == null) return node;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
@@ -35,11 +35,7 @@ namespace hello_algo.chapter_tree
return root;
}
/// <summary>
/// 查找结点
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
/* 查找结点 */
public TreeNode? search(int num)
{
TreeNode? cur = root;
@@ -125,7 +121,7 @@ namespace hello_algo.chapter_tree
else
{
// 获取中序遍历中 cur 的下一个结点
TreeNode? nex = min(cur.right);
TreeNode? nex = getInOrderNext(cur.right);
if (nex != null)
{
int tmp = nex.val;
@@ -138,8 +134,8 @@ namespace hello_algo.chapter_tree
return cur;
}
/* 获取最小结点 */
private TreeNode? min(TreeNode? root)
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
private TreeNode? getInOrderNext(TreeNode? root)
{
if (root == null) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
+2 -7
View File
@@ -12,11 +12,7 @@ namespace hello_algo.chapter_tree
public class binary_tree_bfs
{
/// <summary>
/// 层序遍历
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
/* 层序遍历 */
public List<int> hierOrder(TreeNode root)
{
// 初始化队列,加入根结点
@@ -41,8 +37,7 @@ namespace hello_algo.chapter_tree
{
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
TreeNode? root = TreeNode.ArrToTree(new int?[] {
1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null});
TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root);
+4 -14
View File
@@ -13,10 +13,7 @@ namespace hello_algo.chapter_tree
{
List<int> list = new();
/// <summary>
/// 前序遍历
/// </summary>
/// <param name="root"></param>
/* 前序遍历 */
void preOrder(TreeNode? root)
{
if (root == null) return;
@@ -26,10 +23,7 @@ namespace hello_algo.chapter_tree
preOrder(root.right);
}
/// <summary>
/// 中序遍历
/// </summary>
/// <param name="root"></param>
/* 中序遍历 */
void inOrder(TreeNode? root)
{
if (root == null) return;
@@ -39,10 +33,7 @@ namespace hello_algo.chapter_tree
inOrder(root.right);
}
/// <summary>
/// 后序遍历
/// </summary>
/// <param name="root"></param>
/* 后序遍历 */
void postOrder(TreeNode? root)
{
if (root == null) return;
@@ -57,8 +48,7 @@ namespace hello_algo.chapter_tree
{
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
TreeNode? root = TreeNode.ArrToTree(new int?[] {
1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null});
TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root);
+2 -2
View File
@@ -11,8 +11,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>
</Project>
+5 -5
View File
@@ -19,7 +19,7 @@ namespace hello_algo.include
}
/**
* Generate a binary tree with an array
* Generate a binary tree given an array
* @param arr
* @return
*/
@@ -31,22 +31,22 @@ namespace hello_algo.include
TreeNode root = new TreeNode((int) arr[0]);
Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(root);
int i = 1;
while (queue.Count!=0)
int i = 0;
while (queue.Count != 0)
{
TreeNode node = queue.Dequeue();
if (++i >= arr.Length) break;
if (arr[i] != null)
{
node.left = new TreeNode((int) arr[i]);
queue.Enqueue(node.left);
}
i++;
if (++i >= arr.Length) break;
if (arr[i] != null)
{
node.right = new TreeNode((int) arr[i]);
queue.Enqueue(node.right);
}
i++;
}
return root;
}