/*
* 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 for binary tree insertion without recursion
*/
package non_recursion_bin_tree_insertion;
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;
}
public boolean isEmpty()
{
return root==null;
}
public void insert(int data)
{
binary_node temp;
binary_node nptr = new binary_node(data);
ll_queue lq = new ll_queue();
if(root==null)
{
root = nptr;
}
else
{
lq.enqueue(root);
while(!lq.isEmpty())
{
temp = lq.dequeue();
// System.out.println(temp.getData());
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;
}
}
lq.delete_queue();
}
}
public void preorder()
{
preorder_traversal(root);
}
public void preorder_traversal(binary_node preO)
{
if(preO!=null)
{
System.out.print(preO.getData() + " ");
preorder_traversal(preO.getLeft());
preorder_traversal(preO.getRight());
}
}
public void Postorder()
{
Postordertraversal(root);
}
public void Postordertraversal(binary_node node)
{
if(node!=null)
{
Postordertraversal(node.getLeft());
Postordertraversal(node.getRight());
System.out.print(node.getData()+ " ");
}
}
public void Inorder()
{
Inordertraversal(root);
}
public void Inordertraversal(binary_node node)
{
if(node!=null)
{
Inordertraversal(node.getLeft());
System.out.print(node.getData()+ " ");
Inordertraversal(node.getRight());
}
}
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());
}
lq.delete_queue();
}
}
public void display()
{
char c;
Scanner sc = new Scanner(System.in);
int data;
do
{
data = sc.nextInt();
insert(data);
System.out.println("do you want to insert more");
c = sc.next().charAt(0);
}while(c=='Y'||c=='y');
System.out.println("Preorder");
preorder();
System.out.println("Inorder");
Inorder();
System.out.println("Postorder");
Postorder();
System.out.println("Levelorder");
Level_order_traversal();
}
}
class ll_node
{
ll_node link;
binary_node data;
public ll_node()
{
link = null;
data = null;
}
public ll_node(binary_node data)
{
this.data = data;
link = null;
}
public ll_node getLink() {
return link;
}
public void setLink(ll_node link) {
this.link = link;
}
public binary_node getData() {
return data;
}
public void setData(binary_node data) {
this.data = data;
}
}
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 data)
{
ll_node nptr = new ll_node(data);
if(front==null)
{
front = rear = nptr;
}
else
{
rear.setLink(nptr);
rear = nptr;
}
size++;
}
public binary_node dequeue()
{
binary_node data;
if(isEmpty())
{
return null;
}
else
{
data = front.getData();
front = front.getLink();
}
size--;
return data;
}
public void delete_queue()
{
while(!isEmpty())
{
front = front.getLink();
size--;
}
}
}
public class Non_recursion_bin_tree_insertion {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
binary_tree bt = new binary_tree();
bt.display();
// TODO code application logic here
}
}
* 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 for binary tree insertion without recursion
*/
package non_recursion_bin_tree_insertion;
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;
}
public boolean isEmpty()
{
return root==null;
}
public void insert(int data)
{
binary_node temp;
binary_node nptr = new binary_node(data);
ll_queue lq = new ll_queue();
if(root==null)
{
root = nptr;
}
else
{
lq.enqueue(root);
while(!lq.isEmpty())
{
temp = lq.dequeue();
// System.out.println(temp.getData());
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;
}
}
lq.delete_queue();
}
}
public void preorder()
{
preorder_traversal(root);
}
public void preorder_traversal(binary_node preO)
{
if(preO!=null)
{
System.out.print(preO.getData() + " ");
preorder_traversal(preO.getLeft());
preorder_traversal(preO.getRight());
}
}
public void Postorder()
{
Postordertraversal(root);
}
public void Postordertraversal(binary_node node)
{
if(node!=null)
{
Postordertraversal(node.getLeft());
Postordertraversal(node.getRight());
System.out.print(node.getData()+ " ");
}
}
public void Inorder()
{
Inordertraversal(root);
}
public void Inordertraversal(binary_node node)
{
if(node!=null)
{
Inordertraversal(node.getLeft());
System.out.print(node.getData()+ " ");
Inordertraversal(node.getRight());
}
}
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());
}
lq.delete_queue();
}
}
public void display()
{
char c;
Scanner sc = new Scanner(System.in);
int data;
do
{
data = sc.nextInt();
insert(data);
System.out.println("do you want to insert more");
c = sc.next().charAt(0);
}while(c=='Y'||c=='y');
System.out.println("Preorder");
preorder();
System.out.println("Inorder");
Inorder();
System.out.println("Postorder");
Postorder();
System.out.println("Levelorder");
Level_order_traversal();
}
}
class ll_node
{
ll_node link;
binary_node data;
public ll_node()
{
link = null;
data = null;
}
public ll_node(binary_node data)
{
this.data = data;
link = null;
}
public ll_node getLink() {
return link;
}
public void setLink(ll_node link) {
this.link = link;
}
public binary_node getData() {
return data;
}
public void setData(binary_node data) {
this.data = data;
}
}
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 data)
{
ll_node nptr = new ll_node(data);
if(front==null)
{
front = rear = nptr;
}
else
{
rear.setLink(nptr);
rear = nptr;
}
size++;
}
public binary_node dequeue()
{
binary_node data;
if(isEmpty())
{
return null;
}
else
{
data = front.getData();
front = front.getLink();
}
size--;
return data;
}
public void delete_queue()
{
while(!isEmpty())
{
front = front.getLink();
size--;
}
}
}
public class Non_recursion_bin_tree_insertion {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
binary_tree bt = new binary_tree();
bt.display();
// TODO code application logic here
}
}
No comments:
Post a Comment