Simple Tree Implementation in C#
Simple Tree Implementation in C#
using System;
class Node
{
public int value;
public Node left;
public Node right;
public Node(int initial)
{
value =initial;
left= null;
right=null;
}
}
class tree
{
int count;
Node top;
public tree()
{
top=null;
count=0;
}
public tree(int initial)
{
top=new Node(initial);
}
public void Add(int value)
{
if(top==null)
{
Node newnode=new Node(value);
top=newnode;
return;
}
Node currentnode =top;
bool added=false;
do
{
if(value<currentnode.value)
{
if (currentnode.left==null)
{
Node newnode =new Node(value);
currentnode.left=newnode;
added=true;
}
else
{
currentnode=currentnode.left;
}
}
if(value>=currentnode.value)
{
if(currentnode.right == null)
{
Node newnode = new Node(value);
currentnode.right=newnode;
added=true;
}
else
{
currentnode=currentnode.right;
}
}
}while(!added);
}
public void AddRc(int value)
{
AddR(ref top,value);
}
private void AddR(ref Node N,int value)
{
if(N==null)
{
Node newnode=new Node(value);
N = newnode;
return;
}
if(value<N.value)
{
AddR(ref N.left,value);
return;
}
if(value >= N.value)
{
AddR(ref N.right,value);
return;
}
}
public void inorder(Node N,ref string s)
{
if(N==null)
{
N=top;
count ++;
Console.WriteLine("From top "+ N.value);
}
if(N.left !=null)
{
inorder(N.left,ref s);
s=s+N.value.ToString().PadLeft(3);
count++;
Console.WriteLine("From left "+ N.value);
}
else
{
count++;
s=s+N.value.ToString().PadLeft(3);
Console.WriteLine("From right-left"+ N.value);
}
if(N.right != null)
{
inorder(N.right,ref s);
count++;
Console.WriteLine("From Right"+ N.value);
}
}
}
class program
{
public static void Main()
{
int num=10;
tree mytree=new tree();
mytree.AddRc(11);
mytree.AddRc(7);
mytree.AddRc(13);
mytree.AddRc(5);
mytree.AddRc(9);
mytree.AddRc(12);
mytree.AddRc(27);
mytree.AddRc(28);
string teestring="";
Console.WriteLine("Inorder traversal");
mytree.inorder(null,ref teestring);
Console.WriteLine(teestring);
}
}
class Node
{
public int value;
public Node left;
public Node right;
public Node(int initial)
{
value =initial;
left= null;
right=null;
}
}
class tree
{
int count;
Node top;
public tree()
{
top=null;
count=0;
}
public tree(int initial)
{
top=new Node(initial);
}
public void Add(int value)
{
if(top==null)
{
Node newnode=new Node(value);
top=newnode;
return;
}
Node currentnode =top;
bool added=false;
do
{
if(value<currentnode.value)
{
if (currentnode.left==null)
{
Node newnode =new Node(value);
currentnode.left=newnode;
added=true;
}
else
{
currentnode=currentnode.left;
}
}
if(value>=currentnode.value)
{
if(currentnode.right == null)
{
Node newnode = new Node(value);
currentnode.right=newnode;
added=true;
}
else
{
currentnode=currentnode.right;
}
}
}while(!added);
}
public void AddRc(int value)
{
AddR(ref top,value);
}
private void AddR(ref Node N,int value)
{
if(N==null)
{
Node newnode=new Node(value);
N = newnode;
return;
}
if(value<N.value)
{
AddR(ref N.left,value);
return;
}
if(value >= N.value)
{
AddR(ref N.right,value);
return;
}
}
public void inorder(Node N,ref string s)
{
if(N==null)
{
N=top;
count ++;
Console.WriteLine("From top "+ N.value);
}
if(N.left !=null)
{
inorder(N.left,ref s);
s=s+N.value.ToString().PadLeft(3);
count++;
Console.WriteLine("From left "+ N.value);
}
else
{
count++;
s=s+N.value.ToString().PadLeft(3);
Console.WriteLine("From right-left"+ N.value);
}
if(N.right != null)
{
inorder(N.right,ref s);
count++;
Console.WriteLine("From Right"+ N.value);
}
}
}
class program
{
public static void Main()
{
int num=10;
tree mytree=new tree();
mytree.AddRc(11);
mytree.AddRc(7);
mytree.AddRc(13);
mytree.AddRc(5);
mytree.AddRc(9);
mytree.AddRc(12);
mytree.AddRc(27);
mytree.AddRc(28);
string teestring="";
Console.WriteLine("Inorder traversal");
mytree.inorder(null,ref teestring);
Console.WriteLine(teestring);
}
}
Comments
Post a Comment