Binary Search Tree Construction and Various Traversals:
Code::
import java.util.*;
class node
{
node right,left;
int d;
node()
{
d=0;
right=left=null;
}
node(int a)
{
d=a;
right=left=null;
}
}
class tree
{
node root;
tree()
{
root=null;
}
void insert(int data)
{
root=insert(root,data);
}
node insert(node n,int data)
{
if(n==null)
{
n=new node(data);
}
else if(data<=n.d)
{
n.left=insert(n.left,data);
}
else
{
n.right=insert(n.right,data);
}
return n;
}
void inorder()
{
inorder(root);
System.out.println();
}
void inorder(node s)
{
if(s!=null)
{
inorder(s.left);
System.out.print(s.d+" ");
inorder(s.right);
}
}
void preorder()
{
preorder(root);
System.out.println();
}
void preorder(node s)
{
if(s!=null)
{
System.out.print(s.d+" ");
preorder(s.left);
preorder(s.right);
}
}
void postorder()
{
postorder(root);
System.out.println();
}
void postorder(node s)
{
if(s!=null)
{
postorder(s.left);
postorder(s.right);
System.out.print(s.d+" ");
}
}
}
public class BstTree
{
public static void main(String [] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Number of Elements");
int n=in.nextInt();
tree r=new tree();
System.out.println("Enter Elements:");
for(int i=0;i<n;i++)
{
r.insert(in.nextInt());
}
System.out.print("Inorder: ");
r.inorder();
System.out.print("Postorder: ");
r.postorder();
System.out.print("Preorder: ");
r.preorder();
}
}
Output::
Question:: A secret code encrypts a message by putting it in an array and reading down the columns (blanks are replaced by asterisks and full stops are added to fill up the array).
Write a program that encrypts an input string.
Example:
Input: “Lets go to the sandwich shop today”
Output: “Lohdsoe*ewhdtt*ioasoscpy**ah*.gtn*t.”
Code::
import java.util.*;
public class Encrypt
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
String a;
System.out.print("Enter Message: ");
a=in.nextLine();
if(a.length()%6!=0)
{
int x=a.length()%6;
for (int u=1;u<=6-x;u++)
{
a=a+'.'; //completing string
}
}
String x=a.replace(' ', '*'); //replacing space with *
int n=x.length()/6;
char[][] e=new char[n][6]; //array to get encrypted message
int i=0,k=0;
while(k!=x.length())
{
for(int j=0;j<6;j++)
{
e[i][j]=x.charAt(k);
k++;
}
i++;
}
System.out.println();
System.out.print("Encrypted Message: ");
for(int j=0;j<6;j++)
{
for(int s=0;s<i;s++)
{
System.out.print(e[s][j]);
}
}
System.out.println();
System.out.println();
System.out.println();
}
}
Output::
Following Code Will print the Rhombus Structure :
Code::
import java.util.*;
public class Rhombus
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Number of Rows: ");
int n=in.nextInt();
int count=1,c,space=0;
for(int i=1;i<2*n;i++)
{
if(i<=n)
{
c=i;
space++;
}
else
{
c=2*n-i;
space--;
}
for(int spc=n-space;spc>0;spc--)
{
System.out.print(" ");
}
for(int j=0;j<count;j++)
{
System.out.print(c);
if(j<count/2)
{
c--;
}
else
{
c++;
}
}
if(i<n)
{
count=count+2;
}
else
{
count=count-2;
}
System.out.println();
}
}
}
Output::
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients.
Pascal Triangle:
Code::
import java.util.*;
public class PascalTriangle
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Number of Rows: ");
int n=in.nextInt();
int [][] a=new int[n+1][];
for(int i=0;i<=n;i++)
{
a[i]=new int[i+1]; //jagged array
}
for(int i=0;i<=n;i++)
{ a[i][0]=1;
a[i][i]=1;
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for(int i=0;i<=n;i++)
{
for(int spc=2*n-i;spc>0;spc--)
{
System.out.print(" ");
}
for(int j=0;j<=i;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Output::
Question::
Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 … N Insert either a ‘+’ (for addition) or a ‘-‘ (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.
Example: Enter a number: 7
1+2-3+4-5-6+7=0
1+2-3-4+5+6-7=0
1-2+3+4-5+6-7=0
1-2-3-4-5+6+7=0
Code::
import java.util.*;
class sequencer
{
int n; //for no of terms
int b=1,c=1,d=1,e=1,f=1,g=1,h=1; //for different combinations
int[] a=new int[10]; //for saving all values
Scanner in=new Scanner(System.in);
public void combinations() {
System.out.print("Enter Number of Terms (N<=9):");
n=in.nextInt();
for(int i=0;i<n;i++)
{
a[i]=i+1; //assigning values
}
double s=Math.pow(2,n-1); //total combinations
int k=1;
System.out.println("Combinations are:");
while(k<=s)
{ int sum=a[0];
for(int i=1;i<n;i++)
{
sum=sum+a[i]; //checking sum
}
if(sum==0)
{
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println(" =0");
}
change(a);
k++;
}
}
public void change(int[] a)
{
a[n-1]=-a[n-1];
if(n==2) return;
if(b==2)
{
a[n-2]=-a[n-2];
b=1;
}
else
{
b++;
}
if(n==3) return;
if(c==4)
{
a[n-3]=-a[n-3];
c=1;
}
else
{
c++;
}
if(n==4) return;
if(d==8)
{
a[n-4]=-a[n-4];
d=1;
}
else
{
d++;
}
if(n==5) return;
if(e==16)
{
a[n-5]=-a[n-5];
e=1;
}
else
{
e++;
}
if(n==6) return;
if(f==32)
{
a[n-6]=-a[n-6];
f=1;
}
else
{
f++;
}
if(n==7) return;
if(g==64)
{
a[n-7]=-a[n-7];
g=1;
}
else
{
g++;
}
if(n==8) return;
if(h==128)
{
a[n-8]=-a[n-8];
h=1;
}
else
{
h++;
}
if(n==9) return;
}
}
public class AllIsNothing {
public static void main(String [] nt)
{
sequencer ob=new sequencer();
ob.combinations();
}
}
Output::
Special Thanks To Miss Hargun and Miss Aaishwarya for this program
Following Code Calculate The Square Root of a Number Without using sqrt function:
Code::
import java.util.*;
public class Squareroot
{
public static void main(String[] nt)
{ Scanner in=new Scanner(System.in);
double beg=0,end,n;
System.out.print("Enter Number:");
n=in.nextDouble();
end=n;
double prevmid=0,mid=(beg+end)/2;
double dif=Math.abs(mid-prevmid);
while(mid*mid!=n&&dif>0.000005)
{
if(mid*mid>n)
{
end=mid;
}
else
{
beg=mid;
}
prevmid=mid;
mid=(beg+end)/2;
dif=Math.abs(mid-prevmid);
}
System.out.println("Square Root is: "+mid);
}
}
Output::
Following Code will print a Diamond Pattern of User Specified Number of Rows:
Code::
import java.util.*;
public class Pattern
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter number of Rows: ");
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
for(int sp=n-i;sp>0;sp--)
{
System.out.print(" ");
}
for(int j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println();
}
for(int i=n;i>0;i--)
{
for(int sp=0;sp<n-i;sp++)
{
System.out.print(" ");
}
for(int j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Output::