package pep.Day31;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Stack;
public class Iterator_and_Iterable {
public static class GenericTree implements Iterable<Integer> {
Node root;
GenericTree(Node root) {
this.root = root;
}
@Override
public Iterator<Integer> iterator() {
Iterator<Integer> obj = new GenericTree_PreOrderIterator(root);
return obj;
}
}
private static class Pair {
Node node;
int state;
Pair(Node node, int state) {
this.node = node;
this.state = state;
}
}
public static class GenericTree_PreOrderIterator implements Iterator<Integer> {
Integer next_val;
Stack<Pair> stack;
GenericTree_PreOrderIterator(Node root) {
stack = new Stack<>();
stack.push(new Pair(root, -1));
// ek baar to chalana padega, becoz, initially next_val me initially value null hai
// so that next_val, pehle pre-order pr jaakr set ho jae
next();
}
@Override
public boolean hasNext() {
if (next_val == null)
return false;
else
return true;
}
@Override
public Integer next() {
Integer val_to_return = next_val;
// if in case stack empty tha, to return null kr de,
// which will be used by hasNext() function
next_val = null;
// move next_val to forward, if not possible set it to null
while (!stack.isEmpty()) {
Pair top = stack.peek();
if (top.state == -1) {
next_val = top.node.data;
top.state++;
break; // jab koi next call krega to, pre-order return kr kr return kr dega
} else if (top.state >= 0 && top.state <= top.node.children.size() - 1) {
Pair child = new Pair(top.node.children.get(top.state++), -1);
stack.push(child);
} else if (top.state == top.node.children.size()) {
stack.pop();
}
}
return val_to_return;
}
}
private static class Node {
int data;
ArrayList<Node> children = new ArrayList<>();
}
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 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);
GenericTree gt = new GenericTree(root);
// jab hum yeh collan( : ) wala syntax use krte hain to humein
// Iterable ko implement krna hoga
// for (int val : gt)
// System.out.println(val);
Iterator<Integer> gti = gt.iterator();
while (gti.hasNext()) {
System.out.println(gti.next());
}
}
}
Saturday, March 5, 2022
Iterable and Iterator in Generic Tree
Subscribe to:
Post Comments (Atom)
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:...
-
https://leetcode.com/problems/reverse-integer/description/ Integer.MAX_VALUE = 2,147,483,647 = 2 31 - 1 Integer.MIN_VALUE = -2,147,483,64...
-
https://leetcode.com/problems/two-sum/description/ Given the constraints: 2 <= nums.length <= 10⁴ -10⁹ <= nums[i] <= 10⁹ -10...
-
For this particular question, we only have the In - region. So, the code looks like : package pep.Day7 ; public class TowerOfHanoi { p...
No comments:
Post a Comment