Iterative Method for Finding Height Of Tree

For finding the height of the tree we can use level order traversal of three. We  need to traverse the tree level by level and increment the height count.

        public int IterativeHeightOfTree(TreeNode root)
        {
            int height = 0;
            if (root == null)
            {
                return height;
            }
            int nodes = 0;
            Queue<TreeNode> queue = new Queue<TreeNode>();
            queue.Enqueue(root);
 
            while (true)
            {
                nodes = queue.Count();
                if (nodes == 0)
                {
                    return height;
                }
                height = height + 1;
                while (nodes > 0)
                {
                    var temp = queue.Dequeue();
                    if (temp.Left != null)
                    {
                        queue.Enqueue(temp.Left);
                    }
                    if (temp.Right != null)
                    {
                        queue.Enqueue(temp.Right);
                    }
                    nodes = nodes - 1;
                }
 
            }
 
            //return height;
        }

Test Case

Tree Node count is :  20
5,3,9,15,13,9,7,18,2,13,1,5,7,19,13,13,6,12,14,14,
Height of Tree is 5

Tree Node count is :  1
1,
Height of Tree is 1

Tree Node count is :  0
Height of Tree is 0

Write a Program to Print String in Triangular Format

Today we will write a program for printing a given string’s in triangle.

For Example

input : Independence

Output

image

Code

For First Image

        public static void PrintStringInTriangle(this string input)
        {
            int length = input.Length;

            for (int i = 0; i < length; i++)
            {
                for (int spaceCounter =0; spaceCounter <=length-i; spaceCounter++)
                {
                    Console.Write(" ");
                }
                for (int charCounter = 0; charCounter <=i; charCounter++)
                {
                    Console.Write(input[charCounter]);
                    Console.Write(" ");
                }
                Console.WriteLine("");
            }


        }

For Second Image

        public static void PrintStringInLeftTriangle(this string input)
        {
            int length = input.Length;

            for (int i = 0; i < length; i++)
            {
                for (int spaceCounter = 0; spaceCounter <= length - i; spaceCounter++)
                {
                    Console.Write(" ");
                }
                for (int charCounter = 0; charCounter <= i; charCounter++)
                {
                    Console.Write(input[charCounter]);
                     
                }
                Console.WriteLine("");
            }


        }

For Third Image

        public static void PrintStringInRightTriangle(this string input)
        {
            int length = input.Length;

            for (int i = 0; i < length; i++)
            {
                for (int spaceCounter = 0; spaceCounter <= length; spaceCounter++)
                {
                    Console.Write(" ");
                }
                for (int charCounter = 0; charCounter <= i; charCounter++)
                {
                    Console.Write(input[charCounter]);

                }
                Console.WriteLine("");
            }


        }

Write a Program to Print Following Number Pattern

Input : Number of Rows

Output :

1
22
333
4444
55555

Code

        public static void PrintNumberPattern4(int rows)
        {
            for (int i = 1; i <= rows; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine("");
            }
        }

Designing XAML without the XAML designer

XAML designer is a very a powerful tool for designing XAML files in Visual Studio however it comes with a cost. If you are not using XAML designer, so you can disable XAML designer to Save your computer resources.

Steps to Disable XAML Designer in Visual Studio

1. Go to Tools – Option –“Search Xaml” – Miscellaneous – Default View – Check “Always open documents in full XAML view”

image

Download Yammer SDK for Windows Phone 8 and iOS

yammer-logoYammer the private social network, release their SDK for Windows Phone 8 and iOS. The SDKs enable you to add Yammer OAuth 2-based authentication to your smartphone and tablet apps. Once authenticated, you can use the SDKs to make REST calls to read/write from the Yammer API, thus integrating the full spectrum of Yammer’s social services into your mobile app.

To start building your Yammer Mobile App, visit the Yammer Developer Site for more information:https://developer.yammer.com/mobile.

Write a Program to Print Following Number Pattern

Input  : Number of Rows

Output:

1
121
12321
1234321
123454321

Code

 

        public static void PrintNumberPattern2(int rows)
        {
            int i, j, k;
            //Loop for number of Rows
            for (i = 1; i <= rows; i++)
            {
                // Loop for printing numbers in increasing order
                for (j = 1; j < 2 * i + 1; j++)
                {
                    Console.Write(j);
                    if (j >= i)
                    {
                        break;
                    }
                }
                // Loop for printing numbers in decreasing order
                for (k = j - 1; k > 0; k--)
                {
                    Console.Write(k);
                }
                Console.WriteLine("");
            }
        }

Write a Program for Rotate a Sqaure Matrix by 90 Degree

Today we have to write a program for rotating a given matrix by 90 degree. This code only works with square matrix.

Code

        public static void MatrixRotation<T>(T[,] matrix)
        {
            Console.WriteLine("\n Input Matrix");
            Print2DMatrix(matrix);
            int row = matrix.GetLength(0);
            int column = matrix.GetLength(1);

            T temp;
            for (int i = 0; i < row; i++)
            {
                for (int j = i + 1; j < column; j++)
                {
                    temp = matrix[j, i];
                    matrix[j, i] = matrix[i, j];
                    matrix[i, j] = temp;
                }
            }


            for (int i = 0; i < row / 2; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    temp = matrix[j, i];
                    matrix[j, i] = matrix[j, row - i - 1];
                    matrix[j, row - i - 1] = temp;
                }
            }
            Console.WriteLine("\n Rotation matrix");
            Print2DMatrix(matrix);

        }

        private static void Print2DMatrix<T>(T[,] matrix)
        {
            Console.WriteLine("--------------\n");
            int row = matrix.GetLength(0);
            int column = matrix.GetLength(1);

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine(" ");
            }

        }

Result


Input Matrix

————–

11 12 13 14 15

21 22 23 24 25

31 32 33 34 35

41 42 43 44 45

51 52 53 54 55

Rotation matrix

————–

51 41 31 21 11

52 42 32 22 12

53 43 33 23 13

54 44 34 24 14

55 45 35 25 15

Input Matrix

————–

A B C D

E F G H

I J K L

M N O P

Rotation matrix

————–

M I E A

N J F B

O K G C

P L H D

Write a Program for Finding Duplicate Elements in Array

Given an array of n elements and elements are  from 1 to n-1. that is

clip_image002[6]

Code

        public static void PrintDuplicate(int[] input)
        {
            int length = input.Length;
            Console.WriteLine(string.Join(";", input));
            for (int i = 0; i < length; i++)
            {
                if (input[Math.Abs(input[i])] >= 0)
                {
                    input[Math.Abs(input[i])] = -input[Math.Abs(input[i])];

                }
                else
                {
                    Console.WriteLine("duplicate number {0} and it's index is {1} ",
Math.Abs(input[i]), i + " ; "); } } }

Today We will write a program for converting matrix’s row and columns to zero if any one element is zero.

Example

Input Output

2 2 2 2

2 0 2 3

2 2 2 2

2 0 2 2

0 0 0 0

2 0 2 2

Code

        public static void RowColumnZero(int[,] matrix)
        {
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Before");
            Print2DMatrix(matrix);
            int rowConter;
            int columnCounter;

            rowConter = columnCounter = 0;
            int[] rowArray = new int[matrix.GetLength(0)];
            int[] columnArray = new int[matrix.GetLength(1)];

            for (rowConter = 0; rowConter < rowArray.Length; rowConter++)
            {

                for (columnCounter = 0; columnCounter < columnArray.Length; 
columnCounter++) { if (matrix[rowConter, columnCounter] == 0) { rowArray[rowConter] = 1; columnArray[columnCounter] = 1; } } } for (rowConter = 0; rowConter < rowArray.Length; rowConter++) { for (columnCounter = 0; columnCounter < columnArray.Length;
columnCounter++) { if (rowArray[rowConter] == 1 || columnArray[columnCounter] == 1) { matrix[rowConter, columnCounter] = 0; } } } //Print output Console.WriteLine("After"); Print2DMatrix(matrix); Console.WriteLine("------------------------------------------------------");
        }
Program For Creating the Matrix 
        public static int[,] CreateTestMatrix(int row, int column)
        {
            int[,] matrix = new int[row, column];
            int i, j;
            Random rnd = new Random();
            for (i = 0; i < row; i++)
            {
                //create a test matrix
                for (j = 0; j < column; j++)
                {
                    matrix[i, j] = rnd.Next(0, row * column);
                }
            }
            return matrix;
        }

Program for Printing the Matrix

        private static void Print2DMatrix(int[,] matrix)
        {
            for (int rowConter = 0; rowConter < matrix.GetLength(0); rowConter++)
            {
                {
                    for (int columnCounter = 0; columnCounter < matrix.GetLength(1); 
columnCounter++) { Console.Write(matrix[rowConter, columnCounter] + " "); } Console.WriteLine(Environment.NewLine); } } }
Test
Input Output

6 0 9 11

7 3 10 5

10 5 11 3

0 0 0 0

7 0 10 5

10 0 11 3

7 8 5 3

8 8 11 19

15 19 0 19

7 7 9 11

14 8 16 6

7 8 0 3

8 8 0 19

0 0 0 0

7 7 0 11

14 8 0 6

6 0 3

5 5 3

6 8 6

0 0 0

5 0 3

6 0 6

5 11 6 8

9 5 7 5

13 7 1 7

0 12 9 12

0 11 6 8

0 5 7 5

0 7 1 7

0 0 0 0

2 6 3

4 5 3

4 3 7

2 6 3

4 5 3

4 3 7

1 1

3 1

1 1

3 1

Today We will write a program for converting matrix’s row and columns to zero if any one element is zero.

Example

Input Output

2 2 2 2

2 0 2 3

2 2 2 2

2 0 2 2

0 0 0 0

2 0 2 2

Code

        public static void RowColumnZero(int[,] matrix)
        {
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Before");
            Print2DMatrix(matrix);
            int rowConter;
            int columnCounter;

            rowConter = columnCounter = 0;
            int[] rowArray = new int[matrix.GetLength(0)];
            int[] columnArray = new int[matrix.GetLength(1)];

            for (rowConter = 0; rowConter < rowArray.Length; rowConter++)
            {

                for (columnCounter = 0; columnCounter < columnArray.Length; 
columnCounter++) { if (matrix[rowConter, columnCounter] == 0) { rowArray[rowConter] = 1; columnArray[columnCounter] = 1; } } } for (rowConter = 0; rowConter < rowArray.Length; rowConter++) { for (columnCounter = 0; columnCounter < columnArray.Length;
columnCounter++) { if (rowArray[rowConter] == 1 || columnArray[columnCounter] == 1) { matrix[rowConter, columnCounter] = 0; } } } //Print output Console.WriteLine("After"); Print2DMatrix(matrix); Console.WriteLine("------------------------------------------------------");
        }
Program For Creating the Matrix 
        public static int[,] CreateTestMatrix(int row, int column)
        {
            int[,] matrix = new int[row, column];
            int i, j;
            Random rnd = new Random();
            for (i = 0; i < row; i++)
            {
                //create a test matrix
                for (j = 0; j < column; j++)
                {
                    matrix[i, j] = rnd.Next(0, row * column);
                }
            }
            return matrix;
        }

Program for Printing the Matrix

        private static void Print2DMatrix(int[,] matrix)
        {
            for (int rowConter = 0; rowConter < matrix.GetLength(0); rowConter++)
            {
                {
                    for (int columnCounter = 0; columnCounter < matrix.GetLength(1); 
columnCounter++) { Console.Write(matrix[rowConter, columnCounter] + " "); } Console.WriteLine(Environment.NewLine); } } }
Test
Input Output

6 0 9 11

7 3 10 5

10 5 11 3

0 0 0 0

7 0 10 5

10 0 11 3

7 8 5 3

8 8 11 19

15 19 0 19

7 7 9 11

14 8 16 6

7 8 0 3

8 8 0 19

0 0 0 0

7 7 0 11

14 8 0 6

6 0 3

5 5 3

6 8 6

0 0 0

5 0 3

6 0 6

5 11 6 8

9 5 7 5

13 7 1 7

0 12 9 12

0 11 6 8

0 5 7 5

0 7 1 7

0 0 0 0

2 6 3

4 5 3

4 3 7

2 6 3

4 5 3

4 3 7

1 1

3 1

1 1

3 1