Print binary tree in spiral order

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 bottomFirst = new Node(null, null, 1);

            Node bottomSecond = new Node(null, null, 4);

            Node bottomThird = new Node(null, null, 7);

            Node bottomFourth = new Node(null, null, 12);

            Node middleFirst = new Node(bottomFirst, bottomSecond, 3);

            Node middleSecond = new Node(bottomThird, bottomFourth, 10);

            Node root = new Node(middleFirst, middleSecond, 5);

            Console.WriteLine("Printing the tree in spiral order");

            Console.WriteLine("=================================");

            if (root != null)

            {

                Stack fs = new Stack();

                Stack bs = new Stack();

                fs.Push(root);

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

                {

                    while (fs.Count != 0)

                    {

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

                        Console.Write(temp.getValue() + " ");

                        if (temp.getRight() != null)

                        {

                            bs.Push(temp.getRight());

                        }

                        if (temp.getLeft() != null)

                        {

                            bs.Push(temp.getLeft());

                        }

                    }

                    while (bs.Count != 0)

                    {

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

                        Console.Write(temp.getValue() + " ");

                        if (temp.getLeft() != null)

                        {

                            fs.Push(temp.getLeft());

                        }

                        if (temp.getRight() != null)

                        {

                            fs.Push(temp.getRight());

                        }

                    }

                }

                Console.WriteLine();

            }

        }

    }

}
 
Printing the tree in spiral order
=================================
5 3 10 12 7 4 1
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