using System;
public class BinaryTree
{
public BinaryTree(
T value,
BinaryTree leftNode = null,
BinaryTree rightNode = null)
{
this.Value = value;
this.LeftNode = leftNode;
this.RightNode = rightNode;
}
public T Value { get; private set; }
public BinaryTree LeftNode { get; private set; }
public BinaryTree RightNode { get; private set; }
public void EachPreOrder(Action action)
{
action(this.Value);
if (this.LeftNode != null)
{
this.LeftNode.EachPreOrder(action);
}
if (this.RightNode != null)
{
this.RightNode.EachPreOrder(action);
}
}
public void EachInOrder(Action action)
{
if (this.LeftNode != null)
{
this.LeftNode.EachPreOrder(action);
}
action(this.Value);
if (this.RightNode != null)
{
this.RightNode.EachPreOrder(action);
}
}
public void EachPostOrder(Action action)
{
if (this.LeftNode != null)
{
this.LeftNode.EachPreOrder(action);
}
if (this.RightNode != null)
{
this.RightNode.EachPreOrder(action);
}
action(this.Value);
}
}
Tag: how to
Simple dynamic implementation of generic STACK in C#
using System;
public class LinkedStack<T>
{
private LinkedStackNode<T> firstNode;
public int Count { get; private set; }
public void Push(T element)
{
var newNode = new LinkedStackNode<T>(element);
newNode.NextNode = this.firstNode;
this.firstNode = newNode;
this.Count++;
}
public T Pop()
{
if (this.Count <= 0)
{
throw new InvalidOperationException("Stack is empty.");
}
var nodeToPop = this.firstNode;
this.firstNode = this.firstNode.NextNode;
this.Count--;
return nodeToPop.Value;
}
public T Peek()
{
var nodeToPeek = this.firstNode;
return nodeToPeek.Value;
}
public T[] ToArray()
{
var arr = new T[this.Count];
var currentNode = this.firstNode;
var arrIndex = 0;
while (currentNode != null)
{
arr[arrIndex] = currentNode.Value;
arrIndex++;
currentNode = currentNode.NextNode;
}
return arr;
}
private class LinkedStackNode<T>
{
public LinkedStackNode(
T value,
LinkedStackNode<T> nextNode = null)
{
this.Value = value;
}
public T Value { get; private set; }
public LinkedStackNode<T> NextNode { get; set; }
}
}
Simple static implementation of generic STACK in C#
using System;
public class ArrayStack<T> : IArrayStack<T>
{
private const int InitialCapacity = 16;
private T[] internalStorage;
private int capacity;
public ArrayStack(int capacity = InitialCapacity)
{
this.Capacity = capacity;
this.internalStorage = new T[this.Capacity];
}
public int Count { get; private set; }
private int Capacity
{
get
{
return this.capacity;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(value),
"Capacity should be positive integer number.");
}
this.capacity = value;
}
}
public void Push(T element)
{
if (this.GrowNeeded())
{
this.Grow();
}
this.internalStorage[this.Count] = element;
this.Count++;
}
public T Pop()
{
if (this.Count <= 0)
{
throw new InvalidOperationException("Stack is empty.");
}
this.Count--;
var element = this.internalStorage[this.Count];
this.internalStorage[this.Count] = default(T);
return element;
}
public T Peek()
{
var element = this.internalStorage[this.Count - 1];
return element;
}
public T[] ToArray()
{
var arr = new T[this.Count];
this.FillStorage(arr);
Array.Reverse(arr);
return arr;
}
private bool GrowNeeded()
{
var result = this.Count >= this.Capacity;
return result;
}
private void Grow()
{
this.Capacity *= 2;
var newStorage = new T[this.Capacity];
this.FillStorage(newStorage);
this.internalStorage = newStorage;
}
private void FillStorage(T[] array)
{
Array.Copy(this.internalStorage, array, this.Count);
}
}
Simple dynamic implementation of generic QUEUE in C#
using System;
public class LinkedQueue<T>
{
private LinkedQueueNode<T> start;
private LinkedQueueNode<T> end;
public LinkedQueue()
{
this.Count = 0;
}
public int Count { get; private set; }
public void Enqueue(T element)
{
var newElement = new LinkedQueueNode<T>(element);
if (this.Count == 0)
{
this.start = this.end = newElement;
}
else
{
this.end.Next = newElement;
this.end = newElement;
}
this.Count++;
}
public T Dequeue()
{
if (this.Count <= 0)
{
throw new InvalidOperationException("Queue is empty.");
}
var elementToReturn = this.start;
this.start = this.start.Next;
this.Count--;
return elementToReturn.Value;
}
public T Peak()
{
if (this.Count <= 0)
{
throw new InvalidOperationException("Queue is empty.");
}
var currentElement = this.start.Value;
return currentElement;
}
public T[] ToArray()
{
var arrToReturn = new T[this.Count];
var currentNode = this.start;
var arrIndex = 0;
while (currentNode != null)
{
arrToReturn[arrIndex] = currentNode.Value;
arrIndex++;
currentNode = currentNode.Next;
}
return arrToReturn;
}
private class LinkedQueueNode<T>
{
public LinkedQueueNode(T value)
{
this.Value = value;
}
public T Value { get; private set; }
public LinkedQueueNode<T> Next { get; set; }
}
}
Simple static implementation of generic QUEUE in C#
public class CircularQueue<T>
{
private const int InitialCapacity = 16;
private T[] storage;
private int capacity;
private int startIndex;
private int endIndex;
public CircularQueue(int capacity = InitialCapacity)
{
this.Capacity = capacity;
this.storage = new T[this.Capacity];
}
public int Count { get; private set; }
private int Capacity
{
get
{
return this.capacity;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(value),
"Queue capacity should be a positive integer");
}
this.capacity = value;
}
}
public void Enqueue(T element)
{
if (this.GrowNeeded())
{
this.Grow();
}
this.storage[this.endIndex] = element;
this.endIndex = (this.endIndex + 1) % this.Capacity;
this.Count++;
}
public T Dequeue()
{
if (this.Count <= 0)
{
throw new InvalidOperationException("Queue is empty.");
}
var element = this.storage[this.startIndex];
this.storage[this.startIndex] = default(T);
this.startIndex = (this.startIndex + 1) % this.Capacity;
this.Count--;
return element;
}
public T[] ToArray()
{
var resultArray = new T[this.Count];
this.ResizeArrayStorage(resultArray);
return resultArray;
}
private bool GrowNeeded()
{
var result = this.Count >= this.Capacity;
return result;
}
private void Grow()
{
var newStorage = new T[this.Capacity * 2];
this.ResizeArrayStorage(newStorage);
this.startIndex = 0;
this.endIndex = this.Capacity;
this.Capacity *= 2;
this.storage = newStorage;
}
private void ResizeArrayStorage(T[] array)
{
for (int i = 0; i < this.Count; i++)
{
var currentIndex = (this.startIndex + i) % this.Capacity;
array[i] = this.storage[currentIndex];
}
}
}
How to calculate factorial with single line of code in C#
using System;
using System.Linq;
using System.Numerics;
public static class Factorial
{
public static void Main(string[] args)
{
int num = 100;
Console.WriteLine(Enumerable.Range(1, num).Select(i => new BigInteger(i)).Aggregate((a, b) => a * b));
}
}
// 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
How to generate Permutations without repetition recursively in C#
using System;
class GroupPermutations
{
static void Main(string[] args)
{
string[] array = { "apple", "peach", "orange" };
Perm(array, 0);
}
static void Perm<T>(T[] arr, int k)
{
if (k >= arr.Length)
Print(arr);
else
{
Perm(arr, k + 1);
for (int i = k + 1; i < arr.Length; i++)
{
Swap(ref arr[k], ref arr[i]);
Perm(arr, k + 1);
Swap(ref arr[k], ref arr[i]);
}
}
}
private static void Swap<T>(ref T item1, ref T item2)
{
T temp = item1;
item1 = item2;
item2 = temp;
}
private static void Print<T>(T[] arr)
{
Console.WriteLine("{" + string.Join(", ", arr) + "}");
}
}
//Result:
//{apple, peach, orange}
//{apple, orange, peach}
//{peach, apple, orange}
//{peach, orange, apple}
//{orange, peach, apple}
//{orange, apple, peach}
How to fill matrix in spiral order Java
public class SpiralMatrix {
public static void main(String[] args) {
int[][] matrix = new int[7][7];
int top = 0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
int index = 1;
while (left <= right && top <= bottom) {
for (int col = left; col <= right; col++) {
matrix[top][col] = index++;
}
top++;
for (int row = top; row <= bottom; row++) {
matrix[row][right] = index++;
}
right--;
for (int col = right; col >= left ; col--) {
matrix[bottom][col] = index++;
}
bottom--;
for (int row = bottom; row >= top ; row--) {
matrix[row][left] = index++;
}
left++;
}
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
System.out.printf("%02d ", matrix[row][col]);
}
System.out.println();
}
// result:
// 01 02 03 04 05 06 07
// 24 25 26 27 28 29 08
// 23 40 41 42 43 30 09
// 22 39 48 49 44 31 10
// 21 38 47 46 45 32 11
// 20 37 36 35 34 33 12
// 19 18 17 16 15 14 13
}
}
How to convert decimal number to hexadecimal in C# using method
using System;
using System.Text;
class DecToHex
{
static void Main(string[] args)
{
long num = 100000;
Console.WriteLine(DecimalToHex(num)); // 186A0
}
private static string DecimalToHex(long num)
{
var res = new StringBuilder();
while (num > 0)
{
var reminder = num % 16;
if (reminder > 9)
{
res.Insert(0, (char)(reminder + 55));
}
else
{
res.Insert(0, reminder);
}
num /= 16;
}
return res.ToString();
}
}
How to convert decimal to binary number in C# using method
using System;
using System.Text;
class DecToBin
{
static void Main(string[] args)
{
long num = int.MaxValue * 2L;
Console.WriteLine(DecimalToBinary(num)); // 11111111111111111111111111111110
}
private static string DecimalToBinary(long num)
{
StringBuilder res = new StringBuilder();
while (num > 0)
{
res.Insert(0, num % 2);
num /= 2;
}
return res.ToString();
}
}