Skip to content

Commit 986414b

Browse files
committed
Cleanup and adding some iterators
1 parent e7de2e5 commit 986414b

28 files changed

+758
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Below topics/problems are covered as of now.
133133
- [ ] Introduction
134134
- [ ] Array based Implementation
135135
- [ ] Stack based Implementation
136-
- [X] [Dequeue Implementation](../master/src/com/deepak/data/structures/Queue/Dequeue.java)
136+
- [X] [Dequeue Implementation](../master/src/com/deepak/data/structures/PriorityQueue/Dequeue.java)
137137
- [ ] Skip List
138138

139139
**14. Trie**

src/com/deepak/data/structures/Cache/LRUCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
33
* LRUCache.java
44
*/
55
package com.deepak.data.structures.Cache;

src/com/deepak/data/structures/Cache/MRUCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
33
* MRUCache.java
44
*/
55
package com.deepak.data.structures.Cache;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* HoppingIterator.java
4+
*/
5+
package com.deepak.data.structures.Iterators;
6+
7+
import java.util.Iterator;
8+
import java.util.NoSuchElementException;
9+
10+
/**
11+
* <br> Problem Statement :
12+
*
13+
* Implement an iterator that hops specified number of times and then returns the next
14+
* element after the hop. Note: the iterator always returns the first element as
15+
* it is, and starts hopping only after the first element.
16+
*
17+
* Examples:
18+
*
19+
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
20+
* iterator will return [1, 3, 5] in order when the hop value is 1.
21+
*
22+
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
23+
* iterator will return [1, 4] in order when the hop value is 2.
24+
*
25+
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
26+
* iterator will return [1, 5] in order when the hop value is 3.
27+
*
28+
* </br>
29+
*
30+
* @author Deepak
31+
*/
32+
public class HoppingIterator<T> implements Iterator<T> {
33+
34+
/* Iterator */
35+
private final Iterator<T> iterator;
36+
/* Variable to keep track of next item */
37+
private T nextItem;
38+
/* Number of hops needed */
39+
private final int numOfHops;
40+
/* Flag to check if it is first element */
41+
private boolean first;
42+
43+
/**
44+
* Constructor
45+
*
46+
* @param iterator
47+
* @param numOfHops
48+
*/
49+
public HoppingIterator(Iterator<T> iterator, int numOfHops) {
50+
if (numOfHops < 0) {
51+
throw new IllegalArgumentException("Invalid value for number of hops!!");
52+
}
53+
this.iterator = iterator;
54+
this.numOfHops = numOfHops;
55+
nextItem = null;
56+
first = true;
57+
}
58+
59+
/**
60+
* Method to check of next element exists
61+
*/
62+
@Override
63+
public boolean hasNext() {
64+
/* If we already have next item, return true */
65+
if (nextItem != null) {
66+
return true;
67+
}
68+
/* If it is not first element, move till the next hop */
69+
if (!first) {
70+
for (int hop = 0; hop < numOfHops && iterator.hasNext(); hop++) {
71+
iterator.next();
72+
}
73+
}
74+
/* Now, if next element exits. move there and update first flag */
75+
if (iterator.hasNext()) {
76+
nextItem = iterator.next();
77+
first = false;
78+
}
79+
return nextItem != null;
80+
}
81+
82+
/**
83+
* Method to find next element in collection
84+
*/
85+
@Override
86+
public T next() {
87+
/* If no next element exits, collection has finished */
88+
if (!hasNext()) {
89+
throw new NoSuchElementException("No Element left in collection!!");
90+
}
91+
/* Else, find the next item, return it and set next item to null */
92+
T itemToReturn = nextItem;
93+
nextItem = null;
94+
return itemToReturn;
95+
}
96+
97+
/**
98+
* Method to remove the element
99+
*/
100+
@Override
101+
public void remove() {
102+
throw new UnsupportedOperationException("Remove not supported!!");
103+
}
104+
105+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.deepak.data.structures.Iterators;
2+
3+
import java.util.Iterator;
4+
5+
public class InOrderIterator<T> implements Iterator<T> {
6+
7+
8+
@Override
9+
public boolean hasNext() {
10+
return false;
11+
}
12+
13+
@Override
14+
public T next() {
15+
return null;
16+
}
17+
18+
@Override
19+
public void remove() {
20+
throw new UnsupportedOperationException("Remove not supported!!");
21+
}
22+
23+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* LevelOrderIterator.java
4+
*/
5+
package com.deepak.data.structures.Iterators;
6+
7+
import java.util.Iterator;
8+
import java.util.LinkedList;
9+
import java.util.NoSuchElementException;
10+
import java.util.Queue;
11+
12+
/**
13+
* <br> Problem Statement :
14+
*
15+
* Implement an iterator that iterates through a binary tree in level order.
16+
*
17+
* </br>
18+
*
19+
* @author Deepak
20+
*/
21+
public class LevelOrderIterator<T> implements Iterator<T> {
22+
23+
/* Variable for next item to return */
24+
private T nextItem;
25+
/* Queue to hold items level by level */
26+
private final Queue<TreeNode<T>> queue;
27+
28+
/**
29+
* Constructor
30+
*
31+
* @param root
32+
*/
33+
public LevelOrderIterator(TreeNode<T> root) {
34+
queue = new LinkedList<>();
35+
queue.add(root);
36+
}
37+
38+
/**
39+
* Method to check if next element exists
40+
*/
41+
@Override
42+
public boolean hasNext() {
43+
/* If next item exists, return true */
44+
if (nextItem != null) {
45+
return true;
46+
}
47+
/* If queue is empty, return false */
48+
if (queue.isEmpty()) {
49+
return false;
50+
}
51+
/* Remove the node from the queue */
52+
TreeNode<T> node = queue.remove();
53+
/* If left or right child exists, add to the queue */
54+
if (node.hasLeft()) {
55+
queue.add(node.left);
56+
}
57+
if (node.hasRight()) {
58+
queue.add(node.right);
59+
}
60+
/* Update next item and return true */
61+
nextItem = node.item;
62+
return true;
63+
}
64+
65+
/**
66+
* Method to find next element
67+
*/
68+
@Override
69+
public T next() {
70+
/* If there is no next element, throw exception */
71+
if (!hasNext()) {
72+
throw new NoSuchElementException("No Element left in collection!!");
73+
}
74+
/* Update item to return and next value */
75+
T itemToReturn = nextItem;
76+
nextItem = null;
77+
return itemToReturn;
78+
}
79+
80+
/**
81+
* Method to remove the item
82+
*/
83+
@Override
84+
public void remove() {
85+
throw new UnsupportedOperationException("Remove not supported!");
86+
}
87+
88+
/**
89+
* TreeNode class
90+
*
91+
* @author Deepak
92+
*
93+
* @param <E>
94+
*/
95+
public static class TreeNode<E> {
96+
97+
/* Data in the node, left child and right child */
98+
public E item;
99+
public TreeNode<E> left;
100+
public TreeNode<E> right;
101+
102+
/**
103+
* Constructor
104+
*
105+
* @param item
106+
*/
107+
public TreeNode(E item) {
108+
this.item = item;
109+
}
110+
111+
public boolean hasLeft() {
112+
return left != null;
113+
}
114+
115+
public boolean hasRight() {
116+
return right != null;
117+
}
118+
119+
}
120+
121+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* MaxIterator.java
4+
*/
5+
package com.deepak.data.structures.Iterators;
6+
7+
import java.util.Comparator;
8+
import java.util.Iterator;
9+
import java.util.NoSuchElementException;
10+
11+
/**
12+
* <br> Problem Statement :
13+
*
14+
* Implement a max iterator i.e. it always returns the next item in the list
15+
* bigger than the last item returned.
16+
*
17+
* </br>
18+
*
19+
* @author Deepak
20+
*/
21+
public class MaxIterator<T> implements Iterator<T> {
22+
23+
/* Iterator and Comparator */
24+
private final Iterator<T> iterator;
25+
private final Comparator<T> comparator;
26+
/* Variables to store last item and next item */
27+
private T lastItem;
28+
private T nextItem;
29+
30+
/**
31+
* Constructor
32+
*
33+
* @param iterator
34+
* @param comparator
35+
*/
36+
public MaxIterator(Iterator<T> iterator, Comparator<T> comparator) {
37+
this.iterator = iterator;
38+
this.comparator = comparator;
39+
}
40+
41+
/**
42+
* Method to check if next element exists
43+
*/
44+
@Override
45+
public boolean hasNext() {
46+
/* If next item is not null, return true */
47+
if (nextItem != null) {
48+
return true;
49+
}
50+
/* Else go through the collection and find the next bigger element */
51+
while (nextItem == null && iterator.hasNext()) {
52+
T item = iterator.next();
53+
if (lastItem == null || comparator.compare(item, lastItem) > 0) {
54+
nextItem = item;
55+
}
56+
}
57+
/* Return true if exists */
58+
return nextItem != null;
59+
}
60+
61+
/**
62+
* Method to find next element
63+
*/
64+
@Override
65+
public T next() {
66+
/* If next element doesn't exists, throw exception */
67+
if (!hasNext()) {
68+
throw new NoSuchElementException("No element found in collection!!");
69+
}
70+
/* Else update the last item found */
71+
lastItem = nextItem;
72+
nextItem = null;
73+
return lastItem;
74+
}
75+
76+
/**
77+
* Method to remove the element
78+
*/
79+
@Override
80+
public void remove() {
81+
throw new UnsupportedOperationException("Remove not supported!!");
82+
}
83+
84+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.deepak.data.structures.Iterators;
2+
3+
public class PostOrderIterator {
4+
5+
6+
7+
}

0 commit comments

Comments
 (0)