Skip to content

Commit d18fa7b

Browse files
committed
Refactoring Disjoint-sets using generic types
1 parent 2053f29 commit d18fa7b

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

data-structures/basics/linkedList.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import LLNode from "./llNode";
22

3-
class LinkedList {
4-
head: null | LLNode;
5-
tail: null | LLNode;
3+
class LinkedList<T> {
4+
head: null | LLNode<T>;
5+
tail: null | LLNode<T>;
66
length: number;
77
constructor() {
88
this.head = null;
@@ -11,7 +11,7 @@ class LinkedList {
1111
}
1212

1313
// add an element at the last position
14-
push = (key: string) => {
14+
push = (key: T) => {
1515
const node = new LLNode(key, this);
1616
if (!this.head) {
1717
this.head = node;
@@ -67,7 +67,7 @@ class LinkedList {
6767
};
6868

6969
// add an element in the beginning of the list
70-
unshift = (key: string) => {
70+
unshift = (key: T) => {
7171
let node = new LLNode(key, this);
7272

7373
if (!this.head) {
@@ -97,15 +97,15 @@ class LinkedList {
9797
};
9898

9999
// set the kth element with val
100-
set = (k: number, key: string) => {
100+
set = (k: number, key: T) => {
101101
let node = this.get(k);
102102
if (!node) return false;
103103
node.key = key;
104104
return true;
105105
};
106106

107107
// insert a new node in the kth position
108-
insert = (k: number, key: string) => {
108+
insert = (k: number, key: T) => {
109109
if (k < 0 || k > this.length) return false;
110110
if (k === 0) {
111111
this.unshift(key);

data-structures/basics/llNode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import LinkedList from "./linkedList";
2-
class LLNode {
3-
next: null | LLNode;
4-
constructor(public key: string, public list: LinkedList) {
2+
class LLNode<T> {
3+
next: null | LLNode<T>;
4+
constructor(public key: T, public list: LinkedList<T>) {
55
this.next = null;
66
}
77
}

data-structures/disjoint-sets/forestNode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
class ForestNode {
1+
class ForestNode<T> {
22
rank: number;
3-
parent: ForestNode;
4-
constructor(public key: string) {
3+
parent: ForestNode<T>;
4+
constructor(public key: T) {
55
this.rank = 0;
66
this.parent = this;
77
}

data-structures/disjoint-sets/forestSet.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import ForestNode from "./forestNode";
22

3-
class ForestSet {
3+
class ForestSet<T> {
44
// node key to node
5-
pointers: Map<string, ForestNode>;
5+
pointers: Map<T, ForestNode<T>>;
66
constructor() {
77
this.pointers = new Map();
88
}
99

10-
makeSet = (x: string) => {
11-
const node = new ForestNode(x);
10+
makeSet = (x: T) => {
11+
const node = new ForestNode<T>(x);
1212
this.pointers.set(x, node);
1313
};
1414

15-
findSet = (x: string) => {
15+
findSet = (x: T) => {
1616
if (!this.pointers.get(x)) return null;
1717
let node = this.pointers.get(x)!;
1818

@@ -23,7 +23,7 @@ class ForestSet {
2323
return node.parent;
2424
};
2525

26-
link = (x: ForestNode, y: ForestNode) => {
26+
link = (x: ForestNode<T>, y: ForestNode<T>) => {
2727
// Union by Rank
2828
if (x.rank > y.rank) {
2929
y.parent = x;
@@ -33,7 +33,7 @@ class ForestSet {
3333
}
3434
};
3535

36-
union = (x: string, y: string) => {
36+
union = (x: T, y: T) => {
3737
this.link(this.findSet(x)!, this.findSet(y)!);
3838
};
3939
}

data-structures/disjoint-sets/listSet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class ListSet {
2020
return l.head;
2121
};
2222
this.union = (x, y) => {
23-
if (typeof x === "string" && !this.pointers.get(x))
23+
if (!(x instanceof llNode_1.default) && !this.pointers.get(x))
2424
return null;
25-
if (typeof y === "string" && !this.pointers.get(y))
25+
if (!(y instanceof llNode_1.default) && !this.pointers.get(y))
2626
return null;
2727
const xNode = x instanceof llNode_1.default ? x : this.pointers.get(x);
2828
const yNode = y instanceof llNode_1.default ? y : this.pointers.get(y);

data-structures/disjoint-sets/listSet.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import LinkedList from "../basics/linkedList";
22
import LLNode from "../basics/llNode";
33

4-
class ListSet {
4+
class ListSet<T> {
55
// leader key to Linked List
6-
lists: Map<string, LinkedList>;
6+
lists: Map<T, LinkedList<T>>;
77
// node key to node
8-
pointers: Map<string, LLNode>;
8+
pointers: Map<T, LLNode<T>>;
99
constructor() {
1010
this.lists = new Map();
1111
this.pointers = new Map();
1212
}
1313

14-
makeSet = (x: string) => {
15-
const list = new LinkedList();
14+
makeSet = (x: T) => {
15+
const list = new LinkedList<T>();
1616
const node = list.push(x);
1717
this.lists.set(x, list);
1818
this.pointers.set(x, node);
1919
};
2020

21-
findSet = (x: string) => {
21+
findSet = (x: T) => {
2222
if (!this.pointers.get(x)) return null;
2323
const node = this.pointers.get(x);
2424
const l = node!.list;
2525
return l.head;
2626
};
2727

28-
union = (x: LLNode | string, y: LLNode | string) => {
29-
if (typeof x === "string" && !this.pointers.get(x)) return null;
30-
if (typeof y === "string" && !this.pointers.get(y)) return null;
28+
union = (x: LLNode<T> | T, y: LLNode<T> | T) => {
29+
if (!(x instanceof LLNode) && !this.pointers.get(x)) return null;
30+
if (!(y instanceof LLNode) && !this.pointers.get(y)) return null;
3131

3232
const xNode = x instanceof LLNode ? x : this.pointers.get(x)!;
3333
const yNode = y instanceof LLNode ? y : this.pointers.get(y)!;

0 commit comments

Comments
 (0)