General Tree Data Structure.
Each node at at right including root node is a parent node
& node that will be added at left will be the child node.
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Genreal_tree_node
{
class bst_node
{
public int id;
public
bst_node left, right;
public
bst_node()
{
id = 0;
left = right = null;
}
public
bst_node(int id1)
{
id = id1;
left = right = null;
}
}
class binarytree
{
bst_node root;
bst_node parent;
bst_node temp;
public
binarytree()
{ parent = temp = root = null; }
public
void inserhelper(int
id)
{
insertnode(id, ref root);
}
private
void insertnode(int
id, ref bst_node r)
{
if
(r == null)
r = new
bst_node(id);
else
if (id < r.id)
insertnode(id, ref r.left);
else
if (id > r.id)
insertnode(id, ref r.right);
else
Console.WriteLine("Same
data not allowed");
}
public
void inordertraversehelper()
{
inordertravers(root);
}
private
void inordertravers(bst_node r)
{
if
(r != null)
{
inordertravers(r.left);
Console.WriteLine(r.id);
inordertravers(r.right);
}
}
public
void maximno()
{
bst_node temp = root;
while
(temp != null)
{
temp = temp.right;
if
(temp == null)
{
break;
}
}
Console.WriteLine(temp.id);
}
public
void minimum()
{
bst_node temp = root;
while
(temp != null)
{
temp = temp.left;
if
(temp == null)
{
break;
}
}
Console.WriteLine(temp.id);
}
public
void searchhelper(int
a)
{
sesarch(a,ref root);
}
private void sesarch(int
a, ref bst_node r)
{
if
(r != null)
{
if
(a == r.id)
{
parent = r;
}
sesarch(a, ref r.left);
sesarch(a, ref r.right);
}
}
public
void genrealtreeinsert()
{
int
a,b;
if
(root == null)
{
Console.WriteLine("Enter the value of parent\n");
a = Convert.ToInt32(Console.ReadLine());
root = new
bst_node(a);
Console.Write("How much child root should have\n");
int
c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the value of Child");
b = Convert.ToInt32(Console.ReadLine());
temp = root.left = new bst_node(b);
while
(c != 1)
{
Console.WriteLine("Enter the value of Child\n");
b = Convert.ToInt32(Console.ReadLine());
temp.right = new bst_node(b);
temp = temp.right;
c--;
}
}
else
{
childadder();
}
}
private
void childadder()
{
int
a, b;
Console.WriteLine("*****At which node do you want add
child******");
a = Convert.ToInt32(Console.ReadLine());
sesarch(a, ref root);
if
(parent.left == null)
{
Console.Write("How much child root should have");
int
c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the value of Child");
b = Convert.ToInt32(Console.ReadLine());
temp = parent.left = new bst_node(b);
while
(c != 1)
{
Console.WriteLine("Enter the value of Child");
b = Convert.ToInt32(Console.ReadLine());
temp.right = new bst_node(b);
temp = temp.right;
c--;
}
}
else
{
Console.WriteLine("Node alredy contain child");
Console.WriteLine("DO YOU WANT OVERWRITE\nPress y to overwrite");
string
check = Console.ReadLine();
if
(check == "y" || check == "y")
{
parent.left = null;
childadder();
}
}
}
}
class Program
{
static void Main(string[]
args)
{
binarytree tree = new binarytree();
Console.WriteLine("-----------------");
tree.genrealtreeinsert();
tree.genrealtreeinsert();
tree.genrealtreeinsert();
Console.WriteLine("-----------------");
//tree.searchhelper(4);
Console.WriteLine("-----------------");
tree.inordertraversehelper();
}
}
}
No comments:
Post a Comment