Skip to content

Commit 234b802

Browse files
Adding descriptions.
1 parent c3e2e76 commit 234b802

File tree

2 files changed

+80
-43
lines changed

2 files changed

+80
-43
lines changed

Algorithms/Easy_Algorithms.Tests/UnitTest1.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ public class TestBinaryTree : BranchSumsClass.BinaryTree
384384
{
385385
public TestBinaryTree(int value) : base(value) { }
386386

387-
public TestBinaryTree Insert(List<int> values)
387+
public TestBinaryTree? Insert(List<int> values)
388388
{
389389
return Insert(values, 0);
390390
}
391391

392-
public TestBinaryTree Insert(List<int> values, int i)
392+
public TestBinaryTree? Insert(List<int> values, int i)
393393
{
394394
if (i >= values.Count) return null;
395395

@@ -399,18 +399,18 @@ public TestBinaryTree Insert(List<int> values, int i)
399399
{
400400
TestBinaryTree current = queue[0];
401401
queue.RemoveAt(0);
402-
if (current.left == null)
402+
if (current.Left == null)
403403
{
404-
current.left = new TestBinaryTree(values[i]);
404+
current.Left = new TestBinaryTree(values[i]);
405405
break;
406406
}
407-
queue.Add((TestBinaryTree)current.left);
408-
if (current.right == null)
407+
queue.Add((TestBinaryTree)current.Left);
408+
if (current.Right == null)
409409
{
410-
current.right = new TestBinaryTree(values[i]);
410+
current.Right = new TestBinaryTree(values[i]);
411411
break;
412412
}
413-
queue.Add((TestBinaryTree)current.right);
413+
queue.Add((TestBinaryTree)current.Right);
414414
}
415415
Insert(values, i + 1);
416416
return this;
@@ -420,7 +420,7 @@ public TestBinaryTree Insert(List<int> values, int i)
420420
[Fact]
421421
public void BranchSumsTest1()
422422
{
423-
TestBinaryTree tree = new TestBinaryTree(1).Insert(
423+
TestBinaryTree? tree = new TestBinaryTree(1).Insert(
424424
new List<int>() { 2, 3, 4, 5, 6, 7, 8, 9, 10 });
425425
List<int> expected = new List<int>() { 15, 16, 18, 10, 11 };
426426
Assert.True(BranchSumsClass.BranchSums(tree).SequenceEqual(expected));

Algorithms/Easy_Algorithms/EasyAlgorithmsClass.cs

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -252,36 +252,37 @@ public class BranchSumsClass
252252
{
253253
public class BinaryTree
254254
{
255-
public int value;
256-
public BinaryTree? left;
257-
public BinaryTree? right;
258-
public BinaryTree(int value)
255+
public readonly int Value;
256+
public BinaryTree? Left;
257+
public BinaryTree? Right;
258+
259+
protected BinaryTree(int value)
259260
{
260-
this.value = value;
261-
this.left = null;
262-
this.right = null;
261+
this.Value = value;
262+
this.Left = null;
263+
this.Right = null;
263264
}
264265
}
265-
public static List<int> BranchSums(BinaryTree root)
266+
public static List<int> BranchSums(BinaryTree? root)
266267
{
267268
List<int> sums = new List<int>();
268-
calculateBranchSums(root, 0, sums);
269+
CalculateBranchSums(root, 0, sums);
269270
return sums;
270271
}
271272

272-
public static void calculateBranchSums(
273-
BinaryTree node, int runningSum, List<int> sums)
273+
public static void CalculateBranchSums(
274+
BinaryTree? node, int runningSum, List<int> sums)
274275
{
275276
if (node == null) return;
276-
int newRunningSum = runningSum + node.value;
277-
if (node.left == null && node.right == null)
277+
int newRunningSum = runningSum + node.Value;
278+
if (node.Left == null && node.Right == null)
278279
{
279280
sums.Add(newRunningSum);
280281
return;
281282
}
282283

283-
calculateBranchSums(node.left,newRunningSum, sums);
284-
calculateBranchSums(node.right, newRunningSum, sums);
284+
CalculateBranchSums(node.Left,newRunningSum, sums);
285+
CalculateBranchSums(node.Right, newRunningSum, sums);
285286
}
286287

287288

@@ -302,7 +303,7 @@ public class NodeDepthsClass
302303
// the Binary Tree and h is the height of the Binary Tree
303304
public class BinaryTree
304305
{
305-
public int value;
306+
private int value;
306307
public BinaryTree left;
307308
public BinaryTree right;
308309
public BinaryTree(int value)
@@ -356,7 +357,7 @@ public static int NodeDepths(BinaryTree root)
356357
public static int nodeDepthsHelper(BinaryTree root, int depth)
357358
{
358359
if (root == null) return 0;
359-
return depth + nodeDepthsHelper(root.left, depth + 1) + nodeDepthsHelper(root.right,
360+
return depth + nodeDepthsHelper(root.Left, depth + 1) + nodeDepthsHelper(root.Right,
360361
depth + 1);
361362
}
362363
}
@@ -741,7 +742,16 @@ public LinkedList(int value)
741742

742743
#region MiddleNode
743744
/// <summary>
744-
///
745+
/// You're given a Linked List with at least one node.Write a function that returns
746+
/// the middle node if the Linked List. if there are two middle nodes(i.e an even lenth list)
747+
/// your function should return the second of these nodes.
748+
///
749+
/// Each Linked List node has an integer value as well as a next node pointing to the next
750+
/// node in the list or to None/null it's the tail of the list.
751+
///
752+
/// Linked List = 2->7->3->5
753+
///
754+
/// 3->5
745755
/// </summary>
746756
public class MiddleNodeClass
747757
{
@@ -774,7 +784,17 @@ public LinkedList MiddleNode(LinkedList linkedList)
774784

775785
#region NthFibonacci
776786
/// <summary>
777-
///
787+
/// The Fibonacci sequence is defined as follows: the first number of the sequence is 0,
788+
/// the second is 1, and the nth number is the sum of the (n-1)th and (n-2)th numbers.
789+
/// Write a function that takes in an integer n and returns the nth fibonacci number.
790+
///
791+
/// Important note: the Fibonacci sequence is often defined with its first two numbers
792+
/// as F0 = 0 and F1=1. For the purpose if this question, the first fibinacci number is F0;
793+
/// therefore. getNthFib(1) is equal to F0, getNFib(2) is equal ti F1, etc
794+
///
795+
/// Sample Input
796+
/// n=2
797+
/// Sample Output = 1
778798
/// </summary>
779799
public static class NthFibonacciClass
780800
{
@@ -796,7 +816,24 @@ public static int GetNthFib(int n)
796816

797817
#region ProductSum
798818
/// <summary>
799-
///
819+
/// Write a function that takes in a "special" array and returns it product sum.
820+
///
821+
/// A "special" array is an non-empty array that constains either integers or other
822+
/// "spacial" arrays. The product sum of a "special" array is the sum of its elements,
823+
/// where "special" arrays inside it are summed themselves and them multiplied by thier
824+
/// level of depth.
825+
///
826+
/// The depth of a "special" array is how far nested it is. For instance, the depth of [] is 1:
827+
/// the depth of the inner array in [[]] is 2: the depth of the most innermost array in [[[]]]
828+
/// is 3.
829+
///
830+
/// Therefore, the product sum of [x,y] is x+y; product sum of [x,[y,z]] is x+ 2* (y+z): product
831+
/// sum of [x,[y,[z]]] is x+2*(y+3z).
832+
///
833+
/// Sample Input:
834+
/// array = [5,2,[7,1],3,[6,[-13,8]4]]
835+
/// Sample Output:
836+
/// 12
800837
/// </summary>
801838
public static class ProductSumClass
802839
{
@@ -848,19 +885,19 @@ public int BinarySearchIteritevely(int[] inputArray, int numberOfElements, int k
848885
return -1;
849886
}
850887

851-
public int BinarysearchRecursion(int[] A, int key, int l, int r)
888+
public int BinarysearchRecursion(int[] array, int key, int l, int r)
852889
{
853890
if (l > r)
854891
return -1;
855892
else
856893
{
857894
int mid = (l + r) / 2;
858-
if (key == A[mid])
895+
if (key == array[mid])
859896
return mid;
860-
else if (key < A[mid])
861-
return BinarysearchRecursion(A, key, l, mid - 1);
862-
else if (key > A[mid])
863-
return BinarysearchRecursion(A, key, mid + 1, r);
897+
else if (key < array[mid])
898+
return BinarysearchRecursion(array, key, l, mid - 1);
899+
else if (key > array[mid])
900+
return BinarysearchRecursion(array, key, mid + 1, r);
864901
}
865902
return -1;
866903
}
@@ -2371,8 +2408,8 @@ public class LinkedListClass
23712408
private class Node
23722409
{
23732410
public int element;
2374-
public Node next;
2375-
public Node(int e, Node n)
2411+
public Node? next;
2412+
public Node(int e, Node? n)
23762413
{
23772414
element = e;
23782415
next = n;
@@ -2401,7 +2438,7 @@ public bool isEmpty()
24012438

24022439
public void AddLast(int e)
24032440
{
2404-
Node newest = new Node(e, null);
2441+
Node? newest = new Node(e, null);
24052442
if (isEmpty())
24062443
first = newest;
24072444
else
@@ -2433,8 +2470,8 @@ public void AddAny(int e, int position)
24332470
Console.WriteLine("Invalid Position");
24342471
return;
24352472
}
2436-
Node newest = new Node(e, null);
2437-
Node p = first;
2473+
Node? newest = new Node(e, null);
2474+
Node? p = first;
24382475
int i = 1;
24392476
while (i < position - 1)
24402477
{
@@ -2471,7 +2508,7 @@ public int RemoveLast()
24712508
Console.WriteLine("List is Empty");
24722509
return -1;
24732510
}
2474-
Node p = first;
2511+
Node? p = first;
24752512
int i = 1;
24762513
while (i < size - 1)
24772514
{
@@ -2493,7 +2530,7 @@ public int RemoveAny(int position)
24932530
Console.WriteLine("Invalid Position");
24942531
return -1;
24952532
}
2496-
Node p = first;
2533+
Node? p = first;
24972534
int i = 1;
24982535
while (i < position - 1)
24992536
{
@@ -2512,7 +2549,7 @@ public int RemoveAny(int position)
25122549
/// <returns></returns>
25132550
public int Search(int key)
25142551
{
2515-
Node p = first;
2552+
Node? p = first;
25162553
int index = 0;
25172554
while (p != null)
25182555
{

0 commit comments

Comments
 (0)