Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two DP problems #191

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions BinaryTrees/CheckIfTwoBinaryTreesAreIdentical.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

package javaapplication34;
import java.util.*;

class Node
{
public int data;
public Node lchild;
public Node rchild;

public Node()
{
this.lchild=this.rchild=null;
}

@Override
public String toString()
{
return this.data +" ";
}
}

class BinaryTree
{
Scanner sc = new Scanner(System.in);
public Node root = new Node();
Queue<Node> q= new LinkedList<>();

public BinaryTree()
{
this.root.lchild=this.root.rchild=null;
}
public void Insertion()
{
q.add(root);
int num;
while(!q.isEmpty())
{
Node n = q.poll();
System.out.println("Enter the left child of "+n.data);
num=sc.nextInt();
if(num!=-1)
{
Node temp=new Node();
temp.data=num;
temp.lchild=temp.rchild=null;
q.add(temp);
n.lchild=temp;

}
System.out.println("Enter the right child of "+n.data);
num=sc.nextInt();
if(num!=-1)
{
Node temp=new Node();
temp.data=num;
temp.lchild=temp.rchild=null;
q.add(temp);
n.rchild=temp;

}
}
}
public void PreOrder(Node root)
{
if(root!=null)
{
System.out.print(root.data+" ");
PreOrder(root.lchild);
PreOrder(root.rchild);
}
}

public boolean isIdentical(Node r1,Node r2)
{
if(r1==null && r2==null)
{
return true;
}
if(r1==null || r2==null)
{
return false;
}

return r1.data==r2.data && isIdentical(r1.lchild,r2.lchild) && isIdentical(r1.rchild,r2.rchild);
}

}


public class JavaApplication34 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

BinaryTree bt1 = new BinaryTree();
System.out.println("Enter the root node of hte first tree");
bt1.root.data=sc.nextInt();
bt1.Insertion();
bt1.PreOrder(bt1.root);
System.out.println();
System.out.println("Enter the root node of the second tree");
BinaryTree bt2 = new BinaryTree();
bt2.root.data=sc.nextInt();
bt2.Insertion();
bt2.PreOrder(bt2.root);
BinaryTree bt = new BinaryTree();
System.out.println();
System.out.println(bt.isIdentical(bt1.root, bt2.root));


}

}
100 changes: 100 additions & 0 deletions BinaryTrees/DifferenceBetweenEvenlevelAndOddLevelElements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

package differencebetweenevenlevelandoddlevelelements;
import java.util.*;

class Node
{
int data;
Node lchild;
Node rchild;

public Node(int value)
{
this.data=value;
this.lchild=this.rchild=null;
}
public Node()
{
this.lchild=this.rchild=null;
}
}

class BinaryTree
{
Scanner sc = new Scanner(System.in);
Node p = new Node();
Node root = new Node();
Queue<Node> q;

public BinaryTree()
{
q = new LinkedList<>();
q.add(root);
}
public void Insert()
{
while(!q.isEmpty())
{
p=q.poll();
System.out.println("Enter the left chlid of "+p.data);
int num;
num = sc.nextInt();
if(num !=-1)
{
Node r = new Node(num);
r.lchild=r.rchild=null;
q.add(r);
p.lchild=r;
}
System.out.println("Enter the right chlid of "+p.data);

num = sc.nextInt();
if(num !=-1)
{
Node r = new Node(num);
r.lchild=r.rchild=null;
q.add(r);
p.rchild=r;
}
}
}
}

public class DifferenceBetweenEvenlevelAndOddLevelElements {
public static void Preorder(Node P)
{
if(P!=null)
{
System.out.print(P.data+" ");
Preorder(P.lchild);
Preorder(P.rchild);
}
}
public static int EvenOddDifference(Node P)
{
if(P==null)
{
return 0;
}
else
{
return P.data - EvenOddDifference(P.lchild)-EvenOddDifference(P.rchild);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the root node");
int num =sc.nextInt();
BinaryTree bt = new BinaryTree();
bt.root.data=num;
bt.Insert();
System.out.println();
System.out.println("Preorder");
Preorder(bt.root);
System.out.println();
System.out.println(EvenOddDifference(bt.root));


}

}
117 changes: 117 additions & 0 deletions BinaryTrees/GetLevelOfAnyNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

package javaapplication35;
import java.util.*;

class Node
{
public int data;
public Node lchild;
public Node rchild;

public Node()
{
this.lchild=this.rchild=null;
}

@Override
public String toString()
{
return this.data+" ";
}
}

class BinaryTree
{
Scanner sc = new Scanner(System.in);
public Node root = new Node();
Queue<Node> q= new LinkedList<>();

public BinaryTree()
{
this.root.lchild=this.root.rchild=null;
}
public void Insertion()
{
q.add(root);
int num;
while(!q.isEmpty())
{
Node n = q.poll();
System.out.println("Enter the left child of "+n.data);
num=sc.nextInt();
if(num!=-1)
{
Node temp=new Node();
temp.data=num;
temp.lchild=temp.rchild=null;
q.add(temp);
n.lchild=temp;

}
System.out.println("Enter the right child of "+n.data);
num=sc.nextInt();
if(num!=-1)
{
Node temp=new Node();
temp.data=num;
temp.lchild=temp.rchild=null;
q.add(temp);
n.rchild=temp;

}
}
}
public void PreOrder(Node root)
{
if(root!=null)
{
System.out.print(root.data+" ");
PreOrder(root.lchild);
PreOrder(root.rchild);
}
}
public int FindLevel(Node root,int num,int level)
{
//System.out.println(root);

if(root==null)
{
return 0;
}
if(root.data==num)
{
return level;
}

int res = FindLevel(root.lchild,num,level+1);
if(res!=0)
{
return res;
}


res = FindLevel(root.rchild,num,level+1);
return res;
}
}

public class JavaApplication35 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

BinaryTree bt = new BinaryTree();
System.out.println("Enter the root node");
bt.root.data=sc.nextInt();
bt.Insertion();
bt.PreOrder(bt.root);
System.out.println();
int num;
System.out.println("Enter the number whose level you want to know");
num=sc.nextInt();
System.out.println(bt.FindLevel(bt.root,num,0));

}

}
Loading