arachnode.net
An Open Source C# web crawler with Lucene.NET search using SQL Server 2008/2012/2014/2016/CE An Open Source C# web crawler with Lucene.NET search using MongoDB/RavenDB/Hadoop

Completely Open Source @ GitHub

Does arachnode.net scale? | Download the latest release
Binary Search Tree Challenge

#region

using System;
using System.Collections.Generic;

#endregion

namespace BinaryTreeSearch
{
    public class TreeNode<T>
    {
        public TreeNode<T> Parent { get; set; }
        public TreeNode<T> Left { get; set; }
        public TreeNode<T> Right { get; set; }
        public T Value { get; set; }

        public override string ToString()
        {
            return "Parent: " + (Parent == null ? default(T) : Parent.Value) + " Number: " + Value + " Left: " + (Left == null ? default(T) : Left.Value) + " Right: " + (Right == null ? default(T) : Right.Value);
        }
    }

    internal class Program
    {
        public static void Main(string[] args)
        {
            TreeNode<int?> treeNode = new TreeNode<int?> {Value = 100};

            AddTreeNode(treeNode, new TreeNode<int?> {Value = 95});
            AddTreeNode(treeNode, new TreeNode<int?> {Value = 105});

            /**/

            AddTreeNode(treeNode, new TreeNode<int?> {Value = 93});
            AddTreeNode(treeNode, new TreeNode<int?> {Value = 97});

            AddTreeNode(treeNode, new TreeNode<int?> {Value = 103});
            AddTreeNode(treeNode, new TreeNode<int?> {Value = 107});

            /**/

            AddTreeNode(treeNode, new TreeNode<int?> { Value = 92 });
            AddTreeNode(treeNode, new TreeNode<int?> { Value = 94 });
            AddTreeNode(treeNode, new TreeNode<int?> { Value = 96 });
            AddTreeNode(treeNode, new TreeNode<int?> { Value = 98 });

            AddTreeNode(treeNode, new TreeNode<int?> { Value = 102 });
            AddTreeNode(treeNode, new TreeNode<int?> { Value = 104 });
            AddTreeNode(treeNode, new TreeNode<int?> { Value = 106 });
            AddTreeNode(treeNode, new TreeNode<int?> { Value = 108 });

            /**/

            Console.WriteLine("DepthWiseIterationFromTheLeft");
            DepthWiseIterationFromTheLeft(treeNode);

            /**/

            Console.WriteLine("\n\nDepthWiseIterationFromTheRight");
            DepthWiseIterationFromTheRight(treeNode);

            /**/

            Console.WriteLine("\n\nBreadthWiseIterationFromTheLeft");
            BreadthWiseIterationFromTheLeft(treeNode);

            /**/

            Console.WriteLine("\n\nBreadthWiseIterationFromTheRight");
            BreadthWiseIterationFromTheRight(treeNode);

            /**/

            Console.ReadLine();
        }

        public static void AddTreeNode(TreeNode<int?> treeNode, TreeNode<int?> treeNodeToAdd)
        {
            if (treeNode == null || treeNodeToAdd == null)
            {
                return;
            }

            if (treeNodeToAdd.Value <= treeNode.Value)
            {
                if (treeNode.Left == null)
                {
                    treeNodeToAdd.Parent = treeNode;
                    treeNode.Left = treeNodeToAdd;
                }
                else
                {
                    AddTreeNode(treeNode.Left, treeNodeToAdd);
                }
            }

            if (treeNodeToAdd.Value > treeNode.Value)
            {
                if (treeNode.Right == null)
                {
                    treeNodeToAdd.Parent = treeNode;
                    treeNode.Right = treeNodeToAdd;
                }
                else
                {
                    AddTreeNode(treeNode.Right, treeNodeToAdd);
                }
            }
        }

        public static void DepthWiseIterationFromTheLeft(TreeNode<int?> treeNode)
        {
            if (treeNode == null)
            {
                return;
            }

            Console.Write(treeNode.Value + " ");

            DepthWiseIterationFromTheLeft(treeNode.Left);
            DepthWiseIterationFromTheLeft(treeNode.Right);
        }

        public static void DepthWiseIterationFromTheRight(TreeNode<int?> treeNode)
        {
            if (treeNode == null)
            {
                return;
            }

            Console.Write(treeNode.Value + " ");

            DepthWiseIterationFromTheRight(treeNode.Right);
            DepthWiseIterationFromTheRight(treeNode.Left);
        }

        public static void BreadthWiseIterationFromTheLeft(TreeNode<int?> treeNode)
        {
            if (treeNode == null)
            {
                return;
            }

            var queue = new Queue<TreeNode<int?>>();

            queue.Enqueue(treeNode);

            while(queue.Count != 0)
            {
                var treeNode2 = queue.Dequeue();

                Console.Write(treeNode2.Value + " ");

                if (treeNode2.Left != null)
                {
                    queue.Enqueue(treeNode2.Left);
                }
                if (treeNode2.Right != null)
                {
                    queue.Enqueue(treeNode2.Right);
                }
            }
        }

        public static void BreadthWiseIterationFromTheRight(TreeNode<int?> treeNode)
        {
            if (treeNode == null)
            {
                return;
            }

            var queue = new Queue<TreeNode<int?>>();

            queue.Enqueue(treeNode);

            while (queue.Count != 0)
            {
                var treeNode2 = queue.Dequeue();

                Console.Write(treeNode2.Value + " ");

                if (treeNode2.Right != null)
                {
                    queue.Enqueue(treeNode2.Right);
                }
                if (treeNode2.Left != null)
                {
                    queue.Enqueue(treeNode2.Left);
                }
            }
        }
    }
}

Posted Mon, Feb 18 2013 6:34 PM by arachnode.net
An Open Source C# web crawler with Lucene.NET search using SQL 2008/2012/CE

copyright 2004-2017, arachnode.net LLC