Insert into complete binary tree :
public void insertintobinarytree(int dt)
{
Queue<Node> queue = new Queue<Node>();
Node temp;
Node newnode = new Node();
newnode.data = dt;
newnode.left = null;
newnode.right = null;
if (root == null)
root = newnode;
else
{
queue.Enqueue(root);
while (!(queue.Count == 0))
{
temp = queue.Dequeue();
if (temp.left != null)
queue.Enqueue(temp.left);
else
{
temp.left = newnode;
return;
}
if (temp.right != null)
queue.Enqueue(temp.right);
else
{
temp.right = newnode;
return;
}
}
}
}
Insert into Binary Search Tree :
public void insert(int n)
{
Queue<int> queue= new Queue<int>();
node temp= new node();
temp.left=null;
temp.right=null;
temp.item=n;
if(root==null)
{
root= temp;
}
else
{
queue.Enqueue(root);
while(queue.Count!=0)
{
node t = queue.DeQueue();
if(t.data > n)
{
if(t.left==null)
{
t.left=temp;
return;
}
else
queue.Enqueue(t.left);
}
else
{
if(t.right==null)
{
t.right=temp;
return;
}
else
queue.Enqueue(t.right);
}
}
}
public void insertintobinarytree(int dt)
{
Queue<Node> queue = new Queue<Node>();
Node temp;
Node newnode = new Node();
newnode.data = dt;
newnode.left = null;
newnode.right = null;
if (root == null)
root = newnode;
else
{
queue.Enqueue(root);
while (!(queue.Count == 0))
{
temp = queue.Dequeue();
if (temp.left != null)
queue.Enqueue(temp.left);
else
{
temp.left = newnode;
return;
}
if (temp.right != null)
queue.Enqueue(temp.right);
else
{
temp.right = newnode;
return;
}
}
}
}
Insert into Binary Search Tree :
public void insert(int n)
{
Queue<int> queue= new Queue<int>();
node temp= new node();
temp.left=null;
temp.right=null;
temp.item=n;
if(root==null)
{
root= temp;
}
else
{
queue.Enqueue(root);
while(queue.Count!=0)
{
node t = queue.DeQueue();
if(t.data > n)
{
if(t.left==null)
{
t.left=temp;
return;
}
else
queue.Enqueue(t.left);
}
else
{
if(t.right==null)
{
t.right=temp;
return;
}
else
queue.Enqueue(t.right);
}
}
}