/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
Write a program to find the size of a binary tree using recursion and non recursion
*/
package bin_tree_size_height_depth;
import java.util.Scanner;
/**
*
* @author shabhatn
*/
class binary_node
{
binary_node left , right;
int data;
public binary_node()
{
left = right = null;
}
public binary_node(int data)
{
left = right = null;
this.data = data;
}
public binary_node getLeft() {
return left;
}
public void setLeft(binary_node left) {
this.left = left;
}
public binary_node getRight() {
return right;
}
public void setRight(binary_node right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
class binary_tree
{
binary_node root;
int size;
public binary_tree()
{
root = null;
size = 0;
}
public boolean isEmpty()
{
return root == null;
}
public void insert(int data)
{
root = insert_recursion(root,data);
}
public binary_node insert_recursion(binary_node node,int data)
{
if(node==null)
{
node = new binary_node(data);
}
else
{
if(node.getLeft()==null)
{
node.left = insert_recursion(node.left,data);
}
else
{
node.right = insert_recursion(node.right,data);
}
}
return node;
}
public void insert_nonrecursion(int data)
{
binary_node nptr = new binary_node(data);
binary_node temp;
ll_queue lq = new ll_queue();
if(root==null)
{
root = nptr;
}
else
{
lq.enqueue(root);
while(!lq.isEmpty())
{
temp = lq.dequeue();
if(temp.getLeft()!=null)
{
lq.enqueue(temp.getLeft());
}
else
{
temp.setLeft(nptr);
break;
}
if(temp.getRight()!=null)
{
lq.enqueue(temp.getRight());
}
else
{
temp.setRight(nptr);
break;
}
}
}
}
public void Level_order_traversal()
{
LevelOrderTraversal(root);
}
public void LevelOrderTraversal(binary_node node)
{
ll_queue lq = new ll_queue();
binary_node temp;
if(node!=null)
{
lq.enqueue(node);
while(!lq.isEmpty())
{
temp = lq.dequeue();
System.out.print(temp.getData()+ " ");
if(temp.getLeft()!=null)
lq.enqueue(temp.getLeft());
if(temp.getRight()!=null)
lq.enqueue(temp.getRight());
}
}
}
public void size()
{
int x = size_recursion(root);
System.out.print("size recursion-->" + x);
}
public int size_recursion(binary_node node)
{
int left , right , count=0;
if(node==null)
return 0;
else
{
count++;
left = size_recursion(node.left);
right = size_recursion(node.right);
count = left+right+count;
}
return count;
}
public int size_nonrecursion()
{
int count = 0;
if(root==null)
return 0;
else
{
ll_queue lq = new ll_queue();
binary_node temp;
lq.enqueue(root);
while(!lq.isEmpty())
{
temp = lq.dequeue();
count++;
if(temp.getLeft()!=null)
lq.enqueue(temp.getLeft());
if(temp.getRight()!=null)
lq.enqueue(temp.getRight());
}
return count;
}
}
public void display()
{
char c;
Scanner sc = new Scanner(System.in);
int data;
do
{
data = sc.nextInt();
insert(data);
// insert_nonrecursion(data);
System.out.println("do you want to insert more");
c = sc.next().charAt(0);
}while(c=='Y'||c=='y');
System.out.println("Levelorder");
Level_order_traversal();
size();
int x = size_nonrecursion();
System.out.println("non recursion size-->"+x);
}
}
class ll_queue
{
ll_node front,rear;
int size;
public ll_queue()
{
front = rear = null;
size = 0;
}
public boolean isEmpty()
{
return front==null;
}
public void enqueue(binary_node node)
{
ll_node nptr = new ll_node(node);
if(front==null)
{
front = rear = nptr;
}
else
{
rear.setLink(nptr);
rear = nptr;
}
size++;
}
public binary_node dequeue()
{
binary_node temp;
if(front==null)
return null;
else
{
temp = front.getNode();
front = front.getLink();
}
return temp;
}
}
class ll_node
{
ll_node link;
binary_node node;
public ll_node()
{
link = null;
node = null;
}
public ll_node(binary_node node)
{
link = null;
this.node = node;
}
public ll_node getLink() {
return link;
}
public void setLink(ll_node link) {
this.link = link;
}
public binary_node getNode() {
return node;
}
public void setNode(binary_node node) {
this.node = node;
}
}
public class Bin_tree_size_height_depth {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
binary_tree bt = new binary_tree();
bt.display();
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
Write a program to find the size of a binary tree using recursion and non recursion
*/
package bin_tree_size_height_depth;
import java.util.Scanner;
/**
*
* @author shabhatn
*/
class binary_node
{
binary_node left , right;
int data;
public binary_node()
{
left = right = null;
}
public binary_node(int data)
{
left = right = null;
this.data = data;
}
public binary_node getLeft() {
return left;
}
public void setLeft(binary_node left) {
this.left = left;
}
public binary_node getRight() {
return right;
}
public void setRight(binary_node right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
class binary_tree
{
binary_node root;
int size;
public binary_tree()
{
root = null;
size = 0;
}
public boolean isEmpty()
{
return root == null;
}
public void insert(int data)
{
root = insert_recursion(root,data);
}
public binary_node insert_recursion(binary_node node,int data)
{
if(node==null)
{
node = new binary_node(data);
}
else
{
if(node.getLeft()==null)
{
node.left = insert_recursion(node.left,data);
}
else
{
node.right = insert_recursion(node.right,data);
}
}
return node;
}
public void insert_nonrecursion(int data)
{
binary_node nptr = new binary_node(data);
binary_node temp;
ll_queue lq = new ll_queue();
if(root==null)
{
root = nptr;
}
else
{
lq.enqueue(root);
while(!lq.isEmpty())
{
temp = lq.dequeue();
if(temp.getLeft()!=null)
{
lq.enqueue(temp.getLeft());
}
else
{
temp.setLeft(nptr);
break;
}
if(temp.getRight()!=null)
{
lq.enqueue(temp.getRight());
}
else
{
temp.setRight(nptr);
break;
}
}
}
}
public void Level_order_traversal()
{
LevelOrderTraversal(root);
}
public void LevelOrderTraversal(binary_node node)
{
ll_queue lq = new ll_queue();
binary_node temp;
if(node!=null)
{
lq.enqueue(node);
while(!lq.isEmpty())
{
temp = lq.dequeue();
System.out.print(temp.getData()+ " ");
if(temp.getLeft()!=null)
lq.enqueue(temp.getLeft());
if(temp.getRight()!=null)
lq.enqueue(temp.getRight());
}
}
}
public void size()
{
int x = size_recursion(root);
System.out.print("size recursion-->" + x);
}
public int size_recursion(binary_node node)
{
int left , right , count=0;
if(node==null)
return 0;
else
{
count++;
left = size_recursion(node.left);
right = size_recursion(node.right);
count = left+right+count;
}
return count;
}
public int size_nonrecursion()
{
int count = 0;
if(root==null)
return 0;
else
{
ll_queue lq = new ll_queue();
binary_node temp;
lq.enqueue(root);
while(!lq.isEmpty())
{
temp = lq.dequeue();
count++;
if(temp.getLeft()!=null)
lq.enqueue(temp.getLeft());
if(temp.getRight()!=null)
lq.enqueue(temp.getRight());
}
return count;
}
}
public void display()
{
char c;
Scanner sc = new Scanner(System.in);
int data;
do
{
data = sc.nextInt();
insert(data);
// insert_nonrecursion(data);
System.out.println("do you want to insert more");
c = sc.next().charAt(0);
}while(c=='Y'||c=='y');
System.out.println("Levelorder");
Level_order_traversal();
size();
int x = size_nonrecursion();
System.out.println("non recursion size-->"+x);
}
}
class ll_queue
{
ll_node front,rear;
int size;
public ll_queue()
{
front = rear = null;
size = 0;
}
public boolean isEmpty()
{
return front==null;
}
public void enqueue(binary_node node)
{
ll_node nptr = new ll_node(node);
if(front==null)
{
front = rear = nptr;
}
else
{
rear.setLink(nptr);
rear = nptr;
}
size++;
}
public binary_node dequeue()
{
binary_node temp;
if(front==null)
return null;
else
{
temp = front.getNode();
front = front.getLink();
}
return temp;
}
}
class ll_node
{
ll_node link;
binary_node node;
public ll_node()
{
link = null;
node = null;
}
public ll_node(binary_node node)
{
link = null;
this.node = node;
}
public ll_node getLink() {
return link;
}
public void setLink(ll_node link) {
this.link = link;
}
public binary_node getNode() {
return node;
}
public void setNode(binary_node node) {
this.node = node;
}
}
public class Bin_tree_size_height_depth {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
binary_tree bt = new binary_tree();
bt.display();
}
}