Height of a binary tree

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    public class Node

    {

        private Node left;

        private Node right;

        private int value;

        public Node(Node left, Node right, int value)

        {

            this.left = left;

            this.right = right;

            this.value = value;

        }

        public Node getLeft()

        {

            return left;

        }

        public Node getRight()

        {

            return right;

        }

        public int getValue()

        {

            return value;

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            //Initializing the tree structure

            Node node1 = new Node(null, null, 18);

            Node node2 = new Node(null, null, 11);

            Node node3 = new Node(node1, null, 21);

            Node node4 = new Node(null, node3, 17);

            Node node5 = new Node(null, null, 13);

            Node node6 = new Node(node5, node4, 15);

            Node node7 = new Node(node2, null, 12);

            Node node8 = new Node(null, null, 1);

            Node node9 = new Node(node8, node7, 10);

            Node root = new Node(node9, node6, 13);

            int height = 0;

            if (root != null)

            {

                Stack fs = new Stack();

                Stack bs = new Stack();

                fs.Push(root);

                while (fs.Count != 0 || bs.Count != 0)

                {

                    if (fs.Count != 0)

                    {

                        height++;

                        while (fs.Count != 0)

                        {

                            Node temp = (Node)fs.Pop();

                            if (temp.getRight() != null)

                            {

                                bs.Push(temp.getRight());

                            }

                            if (temp.getLeft() != null)

                            {

                                bs.Push(temp.getLeft());

                            }

                        }

                    }

                    if (bs.Count != 0)

                    {

                        height++;

                        while (bs.Count != 0)

                        {

                            Node temp = (Node)bs.Pop();

                            if (temp.getLeft() != null)

                            {

                                fs.Push(temp.getLeft());

                            }

                            if (temp.getRight() != null)

                            {

                                fs.Push(temp.getRight());

                            }

                        }

                    }

                }

            }

            Console.WriteLine("Height of the binary tree: " + (height – 1));

        }

    }

}
 
Height of the binary tree: 4
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s