Skip to content

Commit 40d328f

Browse files
committed
some bugs and some minor tweak
1 parent 3ab3ef7 commit 40d328f

File tree

4 files changed

+55
-63
lines changed

4 files changed

+55
-63
lines changed

src/Graphs/GraphGeneric.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
public class GraphGeneric<T> {
66

7-
// example for generic graph is present in dir -> ./GenericGraphExample.java
87
private Map<Object, ArrayList<Object>> map;
98
private boolean bidirectional;
109

@@ -44,20 +43,28 @@ public void addEdge(T start, T end) {
4443
}
4544
}
4645

47-
HashSet<T> vis;
4846

49-
public ArrayList<ArrayList<T>> bfs() {
47+
public List<List<T>> bfs() {
48+
HashSet<T> vis;
5049
vis = new HashSet<>();
51-
ArrayList<ArrayList<T>> ans = new ArrayList<>();
50+
List<List<T>> ans = new ArrayList<>();
5251
map.forEach((k, v) -> {
5352
if (!vis.contains(k)) {
54-
ans.add(bfs((T) k));
53+
ans.add(bfs((T) k, vis));
5554
}
5655
});
5756
return ans;
5857
}
5958

60-
public ArrayList<T> bfs(T source) {
59+
public List<T> bfs(T source) {
60+
if (!map.containsKey(source)) {
61+
return new ArrayList<>();
62+
}
63+
HashSet<T> vis = new HashSet<>();
64+
return this.bfs(source, vis);
65+
}
66+
67+
private List<T> bfs(T source, HashSet<T> vis) {
6168
ArrayList<T> ans = new ArrayList<>();
6269
Queue<T> queue = new LinkedList<>();
6370
queue.add(source);
@@ -76,26 +83,27 @@ public ArrayList<T> bfs(T source) {
7683
return ans;
7784
}
7885

79-
public ArrayList<ArrayList<T>> dfs() {
80-
ArrayList<ArrayList<T>> ans = new ArrayList<>();
86+
public List<List<T>> dfs() {
87+
List<List<T>> ans = new ArrayList<>();
8188
HashSet<T> visited = new HashSet<>();
8289
map.forEach((k, v) -> {
8390
if (!visited.contains(k)) {
8491
ans.add(this.dfs((T) k, visited));
8592
}
8693
});
87-
8894
return ans;
8995
}
9096

91-
public ArrayList<T> dfs(T source) { // return only a component of graph containing that source in it
97+
// return only a component of graph containing that source in it
98+
public List<T> dfs(T source) {
9299
if (!map.containsKey(source)) {
93100
return new ArrayList<>();
94101
}
102+
HashSet<T> vis = new HashSet<>();
95103
return this.dfs(source, new HashSet<>());
96104
}
97105

98-
private ArrayList<T> dfs(T s, HashSet<T> visited) {
106+
private List<T> dfs(T s, HashSet<T> visited) {
99107
ArrayList<T> ret = new ArrayList<>();
100108
ret.add(s);
101109
visited.add(s);
@@ -107,9 +115,9 @@ private ArrayList<T> dfs(T s, HashSet<T> visited) {
107115
return ret;
108116
}
109117

110-
@Override // returns adjacency map
118+
@Override // returns adjacency map
111119
public String toString() {
112120
return map.toString();
113121
}
114122

115-
}
123+
}

src/Heaps/Heap.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class Heap<T extends Comparable<T>> {
1010

1111
private int size = 0;
1212

13+
// Default as maxheap
1314
public Heap() {
1415
list.add(null);
1516
index = 0;
1617
}
1718

18-
// Default as maxheap
1919
// pass Comparator for min Heap
2020
// pass Comparator.reverseOrder() for min heap
2121
public Heap(Comparator<T> comparator) {
@@ -59,25 +59,21 @@ public T poll() {
5959
T t = list.get(index);
6060
list.set(1, t);
6161
int i = 1;
62-
while (i < index) {
62+
while (i < index - 1) {
6363
int maxIndex = i, left = 2 * i, right = (2 * i) + 1;
64-
if (left < index && comparator.compare(list.get(i), list.get(left)) > 0) {
64+
if (left < index && comparator.compare(list.get(maxIndex), list.get(left)) < 0)
6565
maxIndex = left;
66-
}
67-
if (right < index && comparator.compare(list.get(i), list.get(right)) > 0) {
66+
if (right < index && comparator.compare(list.get(maxIndex), list.get(right)) < 0)
6867
maxIndex = right;
69-
}
7068
if (maxIndex != i) {
7169
t = list.get(i);
7270
list.set(i, list.get(maxIndex));
7371
list.set(maxIndex, t);
7472
i = maxIndex;
75-
} else {
76-
break;
77-
}
78-
73+
} else break;
7974
}
8075
index--;
76+
size--;
8177
return deleted;
8278
}
8379

@@ -97,6 +93,10 @@ public void clear() {
9793
index = 0;
9894
}
9995

96+
public boolean isEmpty() {
97+
return index == 0;
98+
}
99+
100100
@Override
101101
public String toString() {
102102
StringBuilder ans = new StringBuilder("[");
@@ -108,4 +108,4 @@ public String toString() {
108108
ans.append(list.get(i));
109109
return ans.append("]").toString();
110110
}
111-
}
111+
}
Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
11
package Lists;
22

3-
public class StackCustom<T> {
3+
public class Stacks<T> {
44

55
private Object[] arr;
6-
private int top = -1, size = 0;
6+
private int top = -1;
77
private final int DEFAULT_CAPACITY = 5;
88
private boolean expand = false;
99

10-
public StackCustom() {
10+
public Stacks() {
11+
// this creates a dynamic stack with no length bound
1112
arr = new Object[DEFAULT_CAPACITY];
1213
expand = true;
1314
}
1415

15-
public StackCustom(int initailCapacity) {
16-
arr = new Object[initailCapacity];
16+
public Stacks(int capacity) {
17+
// this creates capacity bounded stack
18+
arr = new Object[capacity];
1719
}
1820

1921
private void doubleArr() {
22+
// this method used internally for dynamic length of stack
2023
Object[] temp = new Object[2 * arr.length];
21-
for (int i = 0; i < arr.length; i++) {
24+
for (int i = 0; i < arr.length; i++)
2225
temp[i] = arr[i];
23-
}
2426
arr = temp;
2527
}
2628

27-
public T push(T val) throws Exception {
29+
public T push(T val) throws RuntimeException {
2830
if (top == arr.length - 1) {
29-
if (expand) {
30-
doubleArr();
31-
} else {
32-
throw new Exception("Stack Overflow");
33-
}
31+
if (expand) doubleArr();
32+
else throw new RuntimeException("Stack Overflow");
3433
}
3534
arr[++top] = val;
3635
return (T) arr[top];
37-
3836
}
3937

40-
public boolean empty() {
38+
public boolean isEmpty() {
4139
return top == -1;
4240
}
4341

44-
public T peek() throws Exception {
45-
if (top == -1) {
46-
throw new Exception("Empty stack exception");
47-
}
42+
public int size() {
43+
return this.top + 1;
44+
}
45+
46+
public T peek() throws RuntimeException {
47+
if (top == -1) throw new RuntimeException("Empty stack exception");
4848
return (T) arr[top];
4949
}
5050

51-
public T pop() throws Exception {
52-
if (top == -1) {
53-
throw new Exception("Empty stack exception");
54-
}
55-
T temp = (T) arr[top];
56-
top--;
51+
public T pop() throws RuntimeException {
52+
if (top == -1) throw new RuntimeException("Empty stack exception");
53+
T temp = (T) arr[top--];
5754
return temp;
5855
}
5956

@@ -71,5 +68,4 @@ public String toString() {
7168
}
7269
return ret.toString();
7370
}
74-
75-
}
71+
}

src/Trie/Trie.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,6 @@ public void remove(String o) {
104104
node.isEndOfWord = false;
105105
}
106106

107-
// checks whether a prefix is present in Trie
108-
public boolean startsWith(String prefix) {
109-
TrieNode temp = root;
110-
for (int i = 0; i < prefix.length(); i++) {
111-
if (!temp.map.containsKey(prefix.charAt(i))) {
112-
return false;
113-
} else {
114-
temp = temp.map.get(prefix.charAt(i));
115-
}
116-
}
117-
return true;
118-
}
119107

120108
// When print is done to Trie toString metod is called
121109
@Override

0 commit comments

Comments
 (0)