arachnode.net
An Open Source C# web crawler with Lucene.NET search using SQL Server 2005/2008/CE An Open Source C# web crawler with Lucene.NET search using MongoDB/RavenDB/Hadoop
Mongo/Raven/MySQL/Hadoop Does arachnode.net scale? | VS2008/2010/2012 & SQL2008/2012 | Download the latest release
Recursive tree traversal orders...

using System;
using System.Collections.Generic;

namespace VMTree
{
    internal class Program
    {
        private static readonly Queue<Node> Queue = new Queue<Node>();

        private static void Main()
        {
            var node1 = new Node();
            var node2 = new Node();
            var node3 = new Node();
            var node4 = new Node();
            var node5 = new Node();
            var node6 = new Node();
            var node7 = new Node();

            node1.Value = 1;
            node2.Value = 2;
            node3.Value = 3;
            node4.Value = 4;
            node5.Value = 5;
            node6.Value = 6;
            node7.Value = 7;

            node1.Left = node2;
            node1.Right = node3;

            node2.Left = node4;
            node2.Right = node5;

            node3.Left = node6;
            node3.Right = node7;

            Console.WriteLine("PreorderTraversal");

            PreorderTraversal(node1);

            Console.WriteLine("InorderTraversal");

            InorderTraversal(node1);

            Console.WriteLine("PostorderTraversal");

            PostorderTraversal(node1);

            Console.WriteLine("BreathFirstTraversal");

            BreadthFirstTraversal(node1);

            Console.ReadLine();
        }

        private static void PreorderTraversal(Node node)
        {
            if (node == null)
            {
                return;
            }

            Console.WriteLine(node.Value.ToString());

            PreorderTraversal(node.Left);
            PreorderTraversal(node.Right);
        }

        private static void InorderTraversal(Node node)
        {
            if (node == null)
            {
                return;
            }

            InorderTraversal(node.Left);

            Console.WriteLine(node.Value);

            InorderTraversal(node.Right);
        }

        private static void PostorderTraversal(Node node)
        {
            if (node == null)
            {
                return;
            }

            PostorderTraversal(node.Left);

            PostorderTraversal(node.Right);

            Console.WriteLine(node.Value);
        }

        private static void BreadthFirstTraversal(Node node)
        {
            if (node == null)
            {
                return;
            }

            Queue.Enqueue(node.Left);

            Queue.Enqueue(node.Right);

            Console.WriteLine(node.Value);

            if (Queue.Count != 0)
            {
                BreadthFirstTraversal(Queue.Dequeue());
            }
        }
    }

    internal class Node
    {
        public int Value { get; set; }
        public Node Left { get; set; }
        public Node Right { get; set; }
        public override string ToString()
        {
            return Value.ToString();
        }
    }
}

Posted Fri, Sep 25 2009 9:05 AM by arachnode.net
An Open Source C# web crawler with Lucene.NET search using SQL 2005/2008/CE

copyright 2004-2014, arachnode.net LLC