Skip to content

Commit

Permalink
Merge pull request tarunsinghofficial#1928 from Sumant03/TreesImporta…
Browse files Browse the repository at this point in the history
…ntQuestions

TreesImportantQuestions
  • Loading branch information
tarunsinghofficial authored Oct 25, 2021
2 parents 4aafc81 + 1b8592f commit 15bd720
Show file tree
Hide file tree
Showing 5 changed files with 672 additions and 0 deletions.
177 changes: 177 additions & 0 deletions Java_Programs_for_beginners/Trees/DiameterOfBTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@

import java.io.*;
import java.util.*;

public class DiameterOfBTree {
public static class Node {
int data;
Node left;
Node right;

Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}

public static class Pair {
Node node;
int state;

Pair(Node node, int state) {
this.node = node;
this.state = state;
}
}

public static Node construct(Integer[] arr) {
Node root = new Node(arr[0], null, null);
Pair rtp = new Pair(root, 1);

Stack<Pair> st = new Stack<>();
st.push(rtp);

int idx = 0;
while (st.size() > 0) {
Pair top = st.peek();
if (top.state == 1) {
idx++;
if (arr[idx] != null) {
top.node.left = new Node(arr[idx], null, null);
Pair lp = new Pair(top.node.left, 1);
st.push(lp);
} else {
top.node.left = null;
}

top.state++;
} else if (top.state == 2) {
idx++;
if (arr[idx] != null) {
top.node.right = new Node(arr[idx], null, null);
Pair rp = new Pair(top.node.right, 1);
st.push(rp);
} else {
top.node.right = null;
}

top.state++;
} else {
st.pop();
}
}

return root;
}

public static void display(Node node) {
if (node == null) {
return;
}

String str = "";
str += node.left == null ? "." : node.left.data + "";
str += " <- " + node.data + " -> ";
str += node.right == null ? "." : node.right.data + "";
System.out.println(str);

display(node.left);
display(node.right);
}

public static int height(Node node) {
if (node == null) {
return -1;
}

int lh = height(node.left);
int rh = height(node.right);

int th = Math.max(lh, rh) + 1;
return th;
}
static int diaOfTree;
public static int diameter1(Node node) {
// write your code here
if(node==null){
return -1;
}

int lh=diameter1(node.left);
int rh=diameter1(node.right);

int diaOfNode=lh+rh+2;
if(diaOfNode>diaOfTree){
diaOfTree=diaOfNode;
}

return Math.max(lh,rh)+1;

}
public static int diameter2(Node node){
if(node==null){
return 0;
}
int ldia=diameter2(node.left);
int rdia=diameter2(node.right);


int lh=height(node.left);
int rh=height(node.right);
int diaofNode=lh+rh+2;

return Math.max(diaofNode,Math.max(ldia,rdia));

}
public static class DiaPair{
int ht,dia;
DiaPair(int ht,int dia){
this.ht=ht;
this.dia=dia;
}
}
public static DiaPair diameter3(Node node){
if(node==null){
return new DiaPair(-1,0);
}
DiaPair lpair=diameter3(node.left);
DiaPair rpair=diameter3(node.right);

int ht=Math.max(lpair.ht,rpair.ht)+1;
int diaOfNode=lpair.ht+rpair.ht+2;
int diaofTree=Math.max(Math.max(lpair.dia,rpair.dia),diaOfNode);

return new DiaPair(ht,diaofTree);


}



public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Integer[] arr = new Integer[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
if (values[i].equals("n") == false) {
arr[i] = Integer.parseInt(values[i]);
} else {
arr[i] = null;
}
}

Node root = construct(arr);

// diaOfTree = 0;
// diameter1(root);
// System.out.println(diaOfTree);

// System.out.println(diameter2(root));
DiaPair res=diameter3(root);
System.out.println(res.dia);

}

}
85 changes: 85 additions & 0 deletions Java_Programs_for_beginners/Trees/InOrderMorrisTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.util.*;

public class Main {
public static Scanner scn = new Scanner(System.in);

public static class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

TreeNode(int val) {
this.val = val;
}
}

public static TreeNode rightMostNode(TreeNode lc,TreeNode curr){
TreeNode rmn=lc;
while(rmn.right!=null&&rmn.right!=curr){
rmn=rmn.right;
}

return rmn;
}

public static ArrayList<Integer> morrisInTraversal(TreeNode node) {
ArrayList<Integer> ans=new ArrayList<>();

TreeNode curr=node;

while(curr!=null){

TreeNode lc=curr.left;
if(lc==null){
ans.add(curr.val);
curr=curr.right;
}else{
TreeNode rmn=rightMostNode(lc,curr);
if(rmn.right==null){
rmn.right=curr;
curr=curr.left;
}else if(rmn.right==curr){

ans.add(curr.val);
rmn.right=null;
curr=curr.right;
}
}

}
return ans;
}

// input_section=================================================

public static TreeNode createTree(int[] arr, int[] IDX) {
if (IDX[0] > arr.length || arr[IDX[0]] == -1) {
IDX[0]++;
return null;
}
TreeNode Treenode = new TreeNode(arr[IDX[0]++]);
Treenode.left = createTree(arr, IDX);
Treenode.right = createTree(arr, IDX);

return Treenode;
}

public static void solve() {
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = scn.nextInt();

int[] IDX = new int[1];
TreeNode root = createTree(arr, IDX);

ArrayList<Integer> ans = morrisInTraversal(root);
for (Integer i : ans)
System.out.print(i + " ");

}

public static void main(String[] args) {
solve();
}
}
Loading

0 comments on commit 15bd720

Please sign in to comment.