Friday, February 25, 2022

Remove Leaves In Generic Tree

I:P:

24 10 20 50 -1 60 -1 -1 30 70 -1 80 110 -1 120 -1 -1 90 -1 -1 40 100 -1 -1 -1

O/P:

10 -> 20, 30, 40, . 20 -> . 30 -> 80, . 80 -> . 40 -> .



package pep.Day29;

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

public class Remove_Leaves_In_GenericTree {
private static class Node {
int data;
ArrayList<Node> children = new ArrayList<>();
}

public static void display(Node node) {
String str = node.data + " -> ";
for (Node child : node.children) {
str += child.data + ", ";
}
str += ".";
System.out.println(str);

for (Node child : node.children) {
display(child);
}
}

public static Node construct(int[] arr) {
Node root = null;

Stack<Node> st = new Stack<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == -1) {
st.pop();
} else {
Node t = new Node();
t.data = arr[i];

if (st.size() > 0) {
st.peek().children.add(t);
} else {
root = t;
}

st.push(t);
}
}

return root;
}

public static int size(Node node) {
int s = 0;

for (Node child : node.children) {
s += size(child);
}
s += 1;

return s;
}

public static int max(Node node) {
int m = Integer.MIN_VALUE;

for (Node child : node.children) {
int cm = max(child);
m = Math.max(m, cm);
}
m = Math.max(m, node.data);

return m;
}

public static int height(Node node) {
int h = -1;

for (Node child : node.children) {
int ch = height(child);
h = Math.max(h, ch);
}
h += 1;

return h;
}

public static void traversals(Node node) {
System.out.println("Node Pre " + node.data);

for (Node child : node.children) {
System.out.println("Edge Pre " + node.data + "--" + child.data);
traversals(child);
System.out.println("Edge Post " + node.data + "--" + child.data);
}

System.out.println("Node Post " + node.data);
}

public static void levelOrderLinewiseZZ(Node node) {
Stack<Node> stack = new Stack<>();
stack.add(node);

Stack<Node> cstack = new Stack<>();
int level = 0;

while (stack.size() > 0) {
node = stack.pop();
System.out.print(node.data + " ");

if (level % 2 == 0) {
for (int i = 0; i < node.children.size(); i++) {
Node child = node.children.get(i);
cstack.push(child);
}
} else {
for (int i = node.children.size() - 1; i >= 0; i--) {
Node child = node.children.get(i);
cstack.push(child);
}
}

if (stack.size() == 0) {
stack = cstack;
cstack = new Stack<>();
level++;
System.out.println();
}
}
}

public static void mirror(Node node) {
for (Node child : node.children) {
mirror(child);
}
Collections.reverse(node.children);
}

public static void removeLeaves(Node node) {
// jine child ke children nhi hai
// remove them
for (int i = node.children.size() - 1; i >= 0; i--) {
Node child = node.children.get(i);
if (child.children.isEmpty())
node.children.remove(i);
}

// make recursive call for all child
for (Node child : node.children) {
removeLeaves(child);
}
}

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

Node root = construct(arr);
removeLeaves(root);
display(root);
}

}

Time Complexity: We are traversing all the nodes once, hence the time complexity will be O(n) where n = number of nodes in the generic tree. 


Space Complexity: We are not using any extra space in the form of any auxiliary data structure. Hence the space complexity is O(1). Note: We are using recursion which does take stack space of O(d) where d = maximum depth of the generic tree.


No comments:

Post a Comment

Diagonal Traversal

 eg.  1       2       3       4 5      6       7       8 9    10    11     12 13  14   15    16 Output: 1 6 11 16 2 7 12 3 8 4  Approach:...