Ejemplo n.º 1
0
        private Node Case1()
        {
            Node n1 = new Node(1);
            n1.Left = new Node(2);
            n1.Right = new Node(3);

            return n1;
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
 public int Sum(Node node)
 {
     return this.SumDepth(node, 1);
 }
Ejemplo n.º 7
0
 public void Push(Node n)
 {
     src.Push(n);
 }
Ejemplo n.º 8
0
Archivo: Node.cs Proyecto: ksivam/code
 public static void Visit(Node node)
 {
     node.Visited = true;
 }
Ejemplo n.º 9
0
Archivo: Node.cs Proyecto: ksivam/code
 public static void Print(Node node)
 {
     Console.WriteLine(node.Value);
 }
Ejemplo n.º 10
0
 public void Push(Node n)
 {
     src.Enqueue(n);
 }