2379
2379
2
class LinkedList
{
class Node
{
Object data; //data item
Node next; //refers to next node in the list
Node(Object d) //constructor
{
data=d;
} // ,,next is automatically set to null
}
class LinkedlistDemo
{
public static void main(String[] args)
{
LinkedList list=new LinkedList(); //create list
class Node
{
Object data;
Node left;
Node right;
Node( Object d ) // constructor
{ data = d; }
}
class BinaryTree
{ Object tree[];
int maxSize;
java.util.LinkedList<Node> que =
new java.util.LinkedList<Node>();
BinaryTree( Object a[], int n ) // constructor
{ maxSize = n;
tree = new Object[maxSize];
for( int i=0; i<maxSize; i++ )
tree[i] = a[i];
}
public Node buildTree( int index )
{ Node p = null;
if( tree[index] != null )
{ p = new Node(tree[index]);
p.left = buildTree(2*index+1);
p.right = buildTree(2*index+2);
}
return p;
}
public void levelorder(Node p)
{
que.addLast(p);
while( !que.isEmpty() )
{
p = que.removeFirst();
System.out.print(p.data + " ");
if(p.left != null)
que.addLast(p.left);
if(p.right != null)
que.addLast(p.right);
}
}
} // end of BinaryTree class
////////////////////////
LevelOrderTraversal.java //////////////////////
class LevelOrderTraversal
{
public static void main(String args[])
{
Object arr[] = {'E', 'C', 'G', 'A', 'D', 'F', 'H',
null,'B', null, null, null, null,
null, null, null, null, null, null};
BinaryTree t = new BinaryTree( arr, arr.length );
Node root = t.buildTree(0); // buildTree() returns reference to root
System.out.print("\n Level Order Tree Traversal: ");
t.levelorder(root);
}
}
12
public class BubbleSort{
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
}
}