Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / C# 4.0

A Link to LINQ and a Look at the Binary Search Tree

Rate me:
Please Sign up or sign in to vote.
4.43/5 (7 votes)
19 Apr 2010CPOL6 min read 33.7K   200   18  
A Comprehensive Look at LINQ and the Binary Search Tree Data Structure
using System;



using System;

namespace BinaryTreeLibrary
{
    class TreeNode
    {
        public TreeNode LeftNode { get; set; }

        public int Data { get; set; }
        public TreeNode RightNode { get; set; }
        public TreeNode(int nodeData)
        {
            Data = nodeData;
            LeftNode = RightNode = null;
        }

        public void Insert(int insertValue)
        {
            if (insertValue < Data)
            {
                if (LeftNode == null)

                    LeftNode = new TreeNode(insertValue);
                else
                    LeftNode.Insert(insertValue);
            }

            else if (insertValue > Data)
            {

                if (RightNode == null)
                    RightNode = new TreeNode(insertValue);

                else
                    RightNode.Insert(insertValue);
            }
        }
    }

    public class Tree
    {
        private TreeNode root;
        public Tree()
        {
            root = null;
        }
        public void insertNode(int insertValue)
        {
            if (root == null)
                root = new TreeNode(insertValue);
            else
                root.Insert(insertValue);
        }

        public void PreorderTraversal()
        {
            PreorderHelper(root);
        }

        private void PreorderHelper(TreeNode node)
        {
            if (node != null)
            {
                Console.Write(node.Data + " ");
                PreorderHelper(node.LeftNode);
                PreorderHelper(node.RightNode);

            }
        }

        public void InorderTraversal()
        {
            InorderHelper(root);
        }

        private void InorderHelper(TreeNode node)
        {
            if (node != null)
            {
                InorderHelper(node.LeftNode);
                Console.Write(node.Data + " ");
                InorderHelper(node.RightNode);
            }
        }

        public void PostorderTraversal()
        {
            PostorderHelper(root);
        }

        private void PostorderHelper(TreeNode node)
        {
            if (node != null)
            {
                PostorderHelper(node.LeftNode);
                PostorderHelper(node.RightNode);
                Console.Write(node.Data + " ");
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Monroe Community
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions