Skip to content

Commit f05e99a

Browse files
committed
adding attribution for Gathros and new c# tree traversal method
1 parent 12be564 commit f05e99a

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

chapters/computational_physics/verlet.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ Submitted by P. Mekhail
436436
#### C
437437

438438
```
439+
// Submitted by Gathros
439440
#include <stdio.h>
440441
441442
// Simple function for velocity-verlet

chapters/fundamental_algorithms/tree_traversal.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ main()
331331
### C
332332

333333
```C
334+
// Submitted by Gathros
334335
#include <stdio.h>
335336
#include <stdlib.h>
336337
#include <string.h>
@@ -488,3 +489,109 @@ int main() {
488489
return 0;
489490
}
490491
```
492+
493+
```cs
494+
// submitted by Julian Schacher‏
495+
using System;
496+
using System.Collections.Generic;
497+
498+
namespace Tree_Traversal
499+
{
500+
class Program
501+
{
502+
static void Main(string[] args)
503+
{
504+
var root = TreeCreator.CreateTree(3, 3);
505+
Console.WriteLine("DFSRecursive:");
506+
Algorithms.DFSRecursive(root);
507+
Console.WriteLine("DFSStack:");
508+
Algorithms.DFSStack(root);
509+
Console.WriteLine("DFSQueue");
510+
Algorithms.BFSQueue(root);
511+
}
512+
}
513+
514+
public static class TreeCreator
515+
{
516+
public static Node CreateTree(int depthCount, int childrenCount)
517+
{
518+
var root = new Node
519+
{
520+
Id = 1
521+
};
522+
CreateAllChildren(root, depthCount, childrenCount);
523+
return root;
524+
}
525+
526+
private static void CreateAllChildren(Node node, int rowCount, int childrenCount)
527+
{
528+
if (rowCount <= 1)
529+
return;
530+
531+
for (int i = 0; i < childrenCount; i++)
532+
{
533+
node.Children.Add(new Node
534+
{
535+
Id = node.Id * 10 + i + 1
536+
});
537+
CreateAllChildren(node.Children[i], rowCount - 1, childrenCount);
538+
}
539+
}
540+
}
541+
542+
public static class Algorithms
543+
{
544+
public static void DFSRecursive(Node node)
545+
{
546+
Console.WriteLine(node.Id);
547+
548+
foreach (var c in node.Children)
549+
{
550+
DFSRecursive(c);
551+
}
552+
}
553+
554+
public static void DFSStack(Node node)
555+
{
556+
var stack = new Stack<Node>();
557+
stack.Push(node);
558+
Node temp;
559+
560+
while (stack.Count != 0)
561+
{
562+
Console.WriteLine(stack.Peek().Id);
563+
temp = stack.Pop();
564+
565+
foreach (var c in temp.Children)
566+
{
567+
stack.Push(c);
568+
}
569+
}
570+
}
571+
572+
public static void BFSQueue(Node node)
573+
{
574+
var queue = new Queue<Node>();
575+
queue.Enqueue(node);
576+
Node temp;
577+
578+
while (queue.Count != 0)
579+
{
580+
Console.WriteLine(queue.Peek().Id);
581+
temp = queue.Dequeue();
582+
583+
foreach (var c in temp.Children)
584+
{
585+
queue.Enqueue(c);
586+
}
587+
}
588+
}
589+
}
590+
591+
public class Node
592+
{
593+
public List<Node> Children { get; set; } = new List<Node>();
594+
public int Id { get; set; }
595+
}
596+
}
597+
```

0 commit comments

Comments
 (0)