Sunday, February 27, 2022

Linearize A Generic Tree - O (n ^ 2)

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, .

20 -> 50, .

50 -> 60, .

60 -> 30, .

30 -> 70, .

70 -> 80, .

80 -> 110, .

110 -> 120, .

120 -> 90, .

90 -> 40, .

40 -> 100, .

100 -> .



1. Sab apne apne child pehle linearize kr kr le aaenge

2. ag kise ke paas bhi e se jyada child hai to, usko linear bna denge

package pep.Day29;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;

public class Linearize_A_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 void linearize(Node node) {
for (Node child : node.children) {
linearize(child);
}
while (node.children.size() > 1) {
// last node ko remove kr denge using (node.children.size() - 1)
Node lastNode = node.children.remove(node.children.size() - 1);

// last node ko get kr denge using (node.children.size() - 1)
Node secondLastNode = node.children.get(node.children.size() - 1);

// jo last node bache hai after deletion, uski lat node laaenge
getTail(secondLastNode).children.add(lastNode);
}
}

private static Node getTail(Node node) {
while (!node.children.isEmpty())
node = node.children.get(0);
return node;
}

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);
linearize(root);
display(root);
}

}


1. Time Complexity: O(n^2)

We have visited every node to linearize it. Although the leaf nodes do not get linearized, still we have visited them and so, we have visited n nodes. Also, when we visit them and try to linearize them, we visit all the nodes after linearizing in order to find the tail and add the next node in the pre-order to its children's ArrayList. This happens inside the first loop of traversal. So, in a way- we have a nested loop where we are visiting almost n elements every time. So, the time complexity is O(n^2).


after recursion, in post order we have another another loop to getTain by traversing n elements.

2. Space Complexity: O(1)

The space complexity remains O(1) since we haven't used any extra space.



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:...