private Node Case1() { Node n1 = new Node(1); n1.Left = new Node(2); n1.Right = new Node(3); return n1; }
private Node Case2() { Node n2 = new Node(2); n2.Left = new Node(3); n2.Right = new Node(4); Node n1 = new Node(1); n1.Left = n2; return n1; }
private int SumDepth(Node current, int depth) { // base case if (current == null) { return 0; } // calc the product of current value and depth int currentSum = current.Value * depth; // calc the left subtree sum by passing left subtree and depth + 1 int leftSum = this.SumDepth(current.Left, depth + 1); // calc the right subtree sum by passing right subtree and depth + 1 int rightSum = this.SumDepth(current.Right, depth + 1); return currentSum + leftSum + rightSum; }
public void TestQueueUsingStack() { // Queue: First In First Out. QueueUsingStack q = new QueueUsingStack(); Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); q.Push(n1); q.Push(n2); q.Push(n3); Node n11 = q.Pop(); Assert.IsTrue(n11.Value == n1.Value); Node n22 = q.Pop(); Assert.IsTrue(n22.Value == n2.Value); Node n33 = q.Pop(); Assert.IsTrue(n33.Value == n3.Value); }
public void TestStackUsingQueue() { Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); // Stack prop: Last In First Out StackUsingQueue s = new StackUsingQueue(); s.Push(n1); s.Push(n2); s.Push(n3); Node n33 = s.Pop(); Assert.IsTrue(n33.Value == n3.Value); Node n22 = s.Pop(); Assert.IsTrue(n22.Value == n2.Value); Node n11 = s.Pop(); Assert.IsTrue(n11.Value == n1.Value); }
public int Sum(Node node) { return this.SumDepth(node, 1); }
public void Push(Node n) { src.Push(n); }
public static void Visit(Node node) { node.Visited = true; }
public static void Print(Node node) { Console.WriteLine(node.Value); }
public void Push(Node n) { src.Enqueue(n); }