Skip to content

Commit 3ab3ef7

Browse files
committed
some changes in generic GraphGeneric and Heap
1 parent 5b10103 commit 3ab3ef7

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/Graphs/GraphGeneric.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
public class GraphGeneric<T> {
66

77
// example for generic graph is present in dir -> ./GenericGraphExample.java
8-
Map<Object, ArrayList<Object>> map;
8+
private Map<Object, ArrayList<Object>> map;
9+
private boolean bidirectional;
910

1011
public GraphGeneric() {
1112
map = new HashMap<>();
13+
this.bidirectional = true;
14+
}
15+
16+
public GraphGeneric(boolean bidirectional) {
17+
map = new HashMap<>();
18+
this.bidirectional = bidirectional;
1219
}
1320

1421
public void addEdge(T start, T end) {
@@ -23,16 +30,17 @@ public void addEdge(T start, T end) {
2330
if (end == null && start == null) {
2431
return;
2532
}
26-
if (map.containsKey(start)) {
27-
map.get(start).add(end);
28-
29-
} else {
30-
map.put(start, new ArrayList<>(Arrays.asList(end)));
33+
if (!map.containsKey(start)) {
34+
map.put(start, new ArrayList<>());
35+
}
36+
if (!map.containsKey(end)) {
37+
map.put(end, new ArrayList<>());
3138
}
32-
if (map.containsKey(end)) {
39+
if (bidirectional) {
40+
map.get(start).add(end);
3341
map.get(end).add(start);
3442
} else {
35-
map.put(end, new ArrayList<>(Arrays.asList(start)));
43+
map.get(start).add(end);
3644
}
3745
}
3846

src/Graphs/GraphGenericExample.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@ public String toString() {
2222
public class GraphGenericExample {
2323

2424
public static void main(String[] args) {
25-
GraphGeneric<User> graph = new GraphGeneric<>();
25+
GraphGeneric<User> graph = new GraphGeneric<>(false);
2626
User one = new User("Nisab", 05157435);
2727
User two = new User("Rahul", 65757567);
2828
User three = new User("James", 57576757);
2929
User four = new User("Tylor", 7575758);
3030

3131
// Adding edge basically means making friends
3232
graph.addEdge(one, two);
33-
graph.addEdge(one, three);
34-
graph.addEdge(one, four);
33+
graph.addEdge(two, three);
3534
graph.addEdge(three, four);
3635

3736
System.out.println(graph);
3837

3938
// returns friends circle of just nisab
4039
System.out.println(graph.dfs(one));
41-
4240
}
4341
}

src/Heaps/Heap.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,13 @@ public void clear() {
9999

100100
@Override
101101
public String toString() {
102-
StringBuilder ans = new StringBuilder("[ ");
103-
for (int i = 1; i <= index; i++) {
104-
ans.append(list.get(i) + " ");
102+
StringBuilder ans = new StringBuilder("[");
103+
if (list.size() == 1) return ans.append("]").toString();
104+
int i;
105+
for (i = 1; i < index; i++) {
106+
ans.append(list.get(i) + ", ");
105107
}
108+
ans.append(list.get(i));
106109
return ans.append("]").toString();
107110
}
108111
}

0 commit comments

Comments
 (0)