|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.