Skip to content

Commit 5754701

Browse files
More programs added
1 parent 4afff67 commit 5754701

File tree

7 files changed

+232
-0
lines changed

7 files changed

+232
-0
lines changed
-705 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package Trees.BinaryTree;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
import java.util.Scanner;
6+
//Given the binary Tree and two nodes say ‘p’ and ‘q’. Determine whether the two nodes are cousins
7+
// of each other or not. Two nodes are said to be cousins of each other if they are at same level of the Binary Tree and have different parents.
8+
//Do it in O(n).
9+
public class CheckCousins {
10+
public static BinaryTreeNode<Integer> takeInputLevelWise(){
11+
Scanner s = new Scanner(System.in);
12+
Queue<BinaryTreeNode<Integer>> pendingNodes = new LinkedList<>();
13+
System.out.println("Enter root data");
14+
int rootData = s.nextInt();
15+
if(rootData == -1){
16+
return null;
17+
}
18+
BinaryTreeNode<Integer> root = new BinaryTreeNode<>(rootData);
19+
pendingNodes.add(root);
20+
while(!pendingNodes.isEmpty()){
21+
BinaryTreeNode<Integer> front = pendingNodes.remove();
22+
System.out.println("Enter left child of "+front.data);
23+
int leftChild = s.nextInt();
24+
if(leftChild != -1){
25+
BinaryTreeNode<Integer> child = new BinaryTreeNode<>(leftChild);
26+
pendingNodes.add(child);
27+
front.left = child;
28+
}
29+
System.out.println("Enter right child of "+front.data);
30+
int rightChild = s.nextInt();
31+
if(rightChild != -1){
32+
BinaryTreeNode<Integer> child = new BinaryTreeNode<>(rightChild);
33+
pendingNodes.add(child);
34+
front.right = child;
35+
}
36+
}
37+
return root;
38+
}
39+
40+
public static boolean isCousin(BinaryTreeNode<Integer> root, int p, int q) {
41+
int xDepth = getDepth(root, p);
42+
int yDepth = getDepth(root, q);
43+
return xDepth == yDepth && getParent(root,p,0) != getParent(root,q,0);
44+
}
45+
public static int getDepth(BinaryTreeNode<Integer> root, int target) {
46+
if(root == null) {
47+
return 0;
48+
}
49+
if(root.data == target) {
50+
return 1;
51+
} else {
52+
int left = getDepth(root.left, target);
53+
if(left != 0) {
54+
return left+1;
55+
}
56+
int right = getDepth(root.right, target);
57+
if(right != 0) {
58+
return right+1;
59+
}
60+
}
61+
return 0;
62+
}
63+
64+
public static int getParent(BinaryTreeNode<Integer> root, int target, int parentVal) {
65+
if(root == null) {
66+
return 0;
67+
}
68+
if(root.data == target) {
69+
return parentVal;
70+
}
71+
int parent = getParent(root.left, target, root.data);
72+
int parent2 = getParent(root.right, target, root.data);
73+
return Math.max(parent, parent2);
74+
}
75+
76+
public static void main(String[] args) {
77+
BinaryTreeNode<Integer> root = takeInputLevelWise();
78+
System.out.println(isCousin(root,2, 4));
79+
}
80+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package Trees.BinaryTree;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.util.Scanner;
7+
//Given a binary tree, return the longest path from leaf to root. Longest means, a path which contain
8+
// maximum number of nodes from leaf to root.
9+
//Do it in O(n) time complexity
10+
public class LongestLeafToRootPath {
11+
12+
public static BinaryTreeNode<Integer> takeInputLevelWise(){
13+
Scanner s = new Scanner(System.in);
14+
Queue<BinaryTreeNode<Integer>> pendingNodes = new LinkedList<>();
15+
System.out.println("Enter root data");
16+
int rootData = s.nextInt();
17+
if(rootData == -1){
18+
return null;
19+
}
20+
BinaryTreeNode<Integer> root = new BinaryTreeNode<>(rootData);
21+
pendingNodes.add(root);
22+
while(!pendingNodes.isEmpty()){
23+
BinaryTreeNode<Integer> front = pendingNodes.remove();
24+
System.out.println("Enter left child of "+front.data);
25+
int leftChild = s.nextInt();
26+
if(leftChild != -1){
27+
BinaryTreeNode<Integer> child = new BinaryTreeNode<>(leftChild);
28+
pendingNodes.add(child);
29+
front.left = child;
30+
}
31+
System.out.println("Enter right child of "+front.data);
32+
int rightChild = s.nextInt();
33+
if(rightChild != -1){
34+
BinaryTreeNode<Integer> child = new BinaryTreeNode<>(rightChild);
35+
pendingNodes.add(child);
36+
front.right = child;
37+
}
38+
}
39+
return root;
40+
}
41+
public static ArrayList<Integer> longestRootToLeafPath(BinaryTreeNode<Integer> root){
42+
if(root == null){
43+
ArrayList<Integer> output = new ArrayList<>();
44+
return output;
45+
}
46+
// Recursive call on root.right
47+
ArrayList<Integer> right = longestRootToLeafPath(root.right);
48+
// Recursive call on root.left
49+
ArrayList<Integer> left = longestRootToLeafPath(root.left);
50+
// Compare the size of the two ArrayList
51+
// and insert current node accordingly
52+
if(right.size() < left.size()){
53+
left.add(root.data);
54+
}else{
55+
right.add(root.data);
56+
}
57+
// Return the appropriate ArrayList
58+
return (left.size() >
59+
right.size() ? left :right);
60+
}
61+
62+
public static void main(String[] args) {
63+
BinaryTreeNode<Integer> root = takeInputLevelWise();
64+
ArrayList<Integer> list = longestRootToLeafPath(root);
65+
for(int i=0; i< list.size(); i++){
66+
System.out.println(list.get(i));
67+
}
68+
}
69+
}

src/Trees/RemoveLeafNodesInTree.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package Trees;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
import java.util.Scanner;
6+
7+
//Remove all leaf nodes from a given generic Tree. Leaf nodes are those nodes, which don't have any children.
8+
//Note : Root will also be a leaf node if it doesn't have any child. You don't need to print the tree,
9+
// just remove all leaf nodes and return the updated root.
10+
public class RemoveLeafNodesInTree {
11+
public static void printTreeLevelWise(TreeNode<Integer> root){
12+
Queue<TreeNode<Integer>> pendingNodes = new LinkedList<>();
13+
pendingNodes.add(root);
14+
while(!pendingNodes.isEmpty()){
15+
int n = pendingNodes.size();
16+
while(n>0){
17+
TreeNode<Integer> frontNode = pendingNodes.peek();
18+
pendingNodes.poll();
19+
System.out.print(frontNode.data + " ");
20+
for(int i = 0;i < frontNode.children.size();i++){
21+
pendingNodes.add(frontNode.children.get(i));
22+
}
23+
n--;
24+
}
25+
System.out.println();
26+
}
27+
}
28+
public static TreeNode<Integer> takeInputLevelWise(){
29+
Scanner s = new Scanner(System.in);
30+
System.out.println("Enter root data");
31+
int rootData = s.nextInt();
32+
Queue<TreeNode<Integer>> pendingNodes = new LinkedList<>();
33+
TreeNode<Integer> root = new TreeNode<>(rootData);
34+
pendingNodes.add(root);
35+
while(!pendingNodes.isEmpty()){
36+
TreeNode<Integer> frontNode = pendingNodes.poll();
37+
System.out.println("Enter number of children of " + frontNode.data);
38+
int numChildren = s.nextInt();
39+
for(int i = 0; i<numChildren; i++){
40+
System.out.println("Enter "+ i + "th child of " + frontNode.data);
41+
int child = s.nextInt();
42+
TreeNode<Integer> childNode = new TreeNode<>(child);
43+
frontNode.children.add(childNode);
44+
pendingNodes.add(childNode);
45+
}
46+
}
47+
return root;
48+
}
49+
50+
public static TreeNode<Integer> removeLeafNodes(TreeNode<Integer> root) {
51+
if(root==null){ return null;}// if root is null return null
52+
if(root.children.size()==0){// if root itself is leaf return null
53+
return null;}
54+
// if root.children is a leaf node
55+
// then delete it from children vector
56+
for (int i = 0; i < root.children.size(); i++) {
57+
TreeNode child= root.children.get(i);
58+
// if it is a leaf
59+
if (child.children.size() == 0) {
60+
// shifting the vector to left
61+
// after the point i
62+
for (int j = i; j < root.children.size() - 1; j++)
63+
root.children.set(j, root.children.get(j + 1));
64+
// delete the last element
65+
root.children.remove(root.children.size()-1);
66+
i--;
67+
}
68+
}
69+
// Remove all leaf node
70+
// of children of root
71+
for (int i = 0;i < root.children.size();i++) {
72+
// call function for root.children
73+
root.children.set(i,removeLeafNodes(root.children.get(i)));
74+
}
75+
return root;
76+
}
77+
78+
public static void main(String[] args) {
79+
TreeNode<Integer> root = takeInputLevelWise();
80+
TreeNode<Integer> newRoot = removeLeafNodes(root);
81+
printTreeLevelWise(newRoot);
82+
}
83+
}

0 commit comments

Comments
 (0)