public void PrintLevelOrder(BTNode node) { if (node == null) { return; } int height = GetTreeHeight(node); for (int i = 1; i <= height; i++) { PrintGivenLevel(node, i); } }
public void PrintPostOrderTraversal(BTNode node) { if (node == null) { return; } else { PrintPostOrderTraversal(node.left); PrintPostOrderTraversal(node.right); System.Console.Write(" " + node.data + " "); } }
public int GetTreeHeight(BTNode node) { int x = 0; if (node == null) { return 0; } else { x = 1 + Math.Max(GetTreeHeight(node.left), GetTreeHeight(node.right)); } return x; }
public void PrintGivenLevel(BTNode node, int level) { if (node == null || level < 1) { return; } if (level == 1) { System.Console.Write(" " + node.data + " "); } else { PrintGivenLevel(node.left, level - 1); PrintGivenLevel(node.right, level - 1); } }
public BTNode ReflectTree(BTNode node) { if (node == null) { return null; } BTNode tmp = node; BTNode swpNode = tmp.left; tmp.left = ReflectTree(tmp.right); tmp.right = ReflectTree(swpNode); return tmp; }
public BTNode AddNode(int value) { BTNode tmp = new BTNode(); tmp.data = value; return tmp; }
/* PreOrder Traversal Using Stacks */ /* Note if a queue was used instead, then it becomes a Breadth-First Search */ public void StackPreOrderTraversal(BTNode node) { if (node == null) { return; } else { Stack nodeStack = new Stack(); nodeStack.Push(node); BTNode tmp = node; while (nodeStack.Count > 0) { while (tmp.left != null) { tmp = tmp.left; nodeStack.Push(tmp); } while (nodeStack.Count > 0) { tmp = (BTNode) nodeStack.Pop(); System.Console.Write(" " + tmp.data + " "); if (tmp.right != null) { tmp = tmp.right; nodeStack.Push(tmp); break; } } } } }
/* PostOrder Traversal Using 2x Stacks */ public void StackPostOrderTraversal(BTNode node) { Stack s1 = new Stack(); Stack s2 = new Stack(); BTNode tmp; if (node != null) { s1.Push(node); } while (s1.Count > 0) { tmp = (BTNode) s1.Pop(); s2.Push(tmp); if (tmp.left != null) { s1.Push(tmp.left); } if (tmp.right != null) { s1.Push(tmp.right); } } foreach (BTNode item in s2) { System.Console.Write(" " + item.data); } }
public BTNode ReflectTreeIter(BTNode node) { BTNode tmp = node; BTNode holder; Queue que = new Queue(); que.Enqueue(tmp); while (que.Count > 0) { tmp = (BTNode) que.Dequeue(); if (tmp.left != null) { que.Enqueue(tmp.left); } if (tmp.right != null) { que.Enqueue(tmp.right); } holder = tmp.left; tmp.left = tmp.right; tmp.right = holder; } return node; }
static void Main(string[] args) { System.Console.WriteLine("Howdy\n"); if (CSci.Program.matchRegEx("aaacbbbb", "a.b")) { System.Console.WriteLine("Regex returned true"); } else { System.Console.WriteLine("Regex returned false"); } int y = 10; int x = Greedy.findMinSteps(y); System.Console.WriteLine("This greedy algorithm says it'll take " + x + " steps to get from " + y + " to 0"); int[] memo = null; memo = new int[y + 1]; x = Memoization.getMinSteps(y, memo); System.Console.WriteLine("This memoization approach says it'll take " + x + " steps to get from " + y + " to 0"); x = DynProgramming.getMinSteps(y); System.Console.WriteLine("This dynamic programming approach says it'll take " + x + " steps to get from " + y + " to 0"); int[] val = new int[] { 50, 3, 500, 20, 7 }; int[] wt = new int[] {10, 16, 30, 50, 4}; /* maxval = LngComSeq.lcs("abcdefxztgh", "beasvbfzh"); System.Console.WriteLine("The longest common sequence \ between abcdefxztgh and beasvbfzh is length " + maxval); */ /* int[] listest = new int[] { 7, 10, 6, 4, 17, 13, 20}; maxval = LngIncSeq.lis(listest); */ int[,] costPath = new int[3,3] { { 1, 2, 3 }, { 4, 8, 2 }, { 1, 5, 3 } }; for (int a = 0; a < 3; a++) { System.Console.WriteLine("For a = " + a); for (int b = 0; b < 3; b++) { System.Console.Write(" " + costPath[a,b] + " "); } System.Console.WriteLine(""); } int minval = MinCostPath.minCostPath(costPath, 2, 2); System.Console.WriteLine("The min value for the given MinCostPath problem is " + minval); BTNode test = new BTNode(); test.TestBTNode(); //StringPattern.testMatch(); RLE.testRLE(); //SnakeNLadder.TestGame(); //GraphBFS.testGraphBFS(); Combinations.testThis(); URLShortener.testURLShortener(); DishesForPeople dishtest = new DishesForPeople(); dishtest.testDishes(); }