diff --git a/data_structures/doubly_linked_list.ts b/data_structures/list/doubly_linked_list.ts similarity index 100% rename from data_structures/doubly_linked_list.ts rename to data_structures/list/doubly_linked_list.ts diff --git a/data_structures/linked_list.ts b/data_structures/list/linked_list.ts similarity index 100% rename from data_structures/linked_list.ts rename to data_structures/list/linked_list.ts diff --git a/data_structures/singly_linked_list.ts b/data_structures/list/singly_linked_list.ts similarity index 100% rename from data_structures/singly_linked_list.ts rename to data_structures/list/singly_linked_list.ts diff --git a/data_structures/list/test/doubly_linked_list.test.ts b/data_structures/list/test/doubly_linked_list.test.ts new file mode 100644 index 00000000..2e41d3af --- /dev/null +++ b/data_structures/list/test/doubly_linked_list.test.ts @@ -0,0 +1,24 @@ +import { DoublyLinkedList } from "../doubly_linked_list"; +import { testLinkedList } from "./linked_list"; + +describe("DoublyLinkedList", () => { + testLinkedList(DoublyLinkedList); + + it("should reverse the list", () => { + const list: DoublyLinkedList = new DoublyLinkedList(); + + list.append(1); + list.append(2); + list.append(3); + list.reverse(); + + expect(list.get(0)).toBe(3); + expect(list.get(1)).toBe(2); + }); + + it("should return null for reverse when list is empty", () => { + const list: DoublyLinkedList = new DoublyLinkedList(); + + expect(list.reverse()).toBeNull(); + }); +}); diff --git a/data_structures/test/linked_list.ts b/data_structures/list/test/linked_list.ts similarity index 100% rename from data_structures/test/linked_list.ts rename to data_structures/list/test/linked_list.ts diff --git a/data_structures/test/singly_linked_list.test.ts b/data_structures/list/test/singly_linked_list.test.ts similarity index 100% rename from data_structures/test/singly_linked_list.test.ts rename to data_structures/list/test/singly_linked_list.test.ts diff --git a/data_structures/hashing/hash_map.ts b/data_structures/map/hash_map.ts similarity index 99% rename from data_structures/hashing/hash_map.ts rename to data_structures/map/hash_map.ts index 3e976553..e4ad04b6 100644 --- a/data_structures/hashing/hash_map.ts +++ b/data_structures/map/hash_map.ts @@ -1,4 +1,4 @@ -import { Map } from "../map"; +import { Map } from "./map"; /** * Represents a hash map. diff --git a/data_structures/map.ts b/data_structures/map/map.ts similarity index 86% rename from data_structures/map.ts rename to data_structures/map/map.ts index 46d3e8ac..1e15c893 100644 --- a/data_structures/map.ts +++ b/data_structures/map/map.ts @@ -1,4 +1,4 @@ -import { HashMapEntry } from "./hashing/hash_map"; +import { HashMapEntry } from "./hash_map"; /** * This interface is a representation of the Map data structure. diff --git a/data_structures/hashing/test/hash_map.test.ts b/data_structures/map/test/hash_map.test.ts similarity index 97% rename from data_structures/hashing/test/hash_map.test.ts rename to data_structures/map/test/hash_map.test.ts index acec9732..ef561d97 100644 --- a/data_structures/hashing/test/hash_map.test.ts +++ b/data_structures/map/test/hash_map.test.ts @@ -1,4 +1,4 @@ -import { HashMap } from "../hash_map"; +import { HashMap } from "../../map/hash_map"; describe("Hash Map", () => { let hashMap: HashMap; diff --git a/data_structures/array_queue.ts b/data_structures/queue/array_queue.ts similarity index 100% rename from data_structures/array_queue.ts rename to data_structures/queue/array_queue.ts diff --git a/data_structures/linked_queue.ts b/data_structures/queue/linked_queue.ts similarity index 100% rename from data_structures/linked_queue.ts rename to data_structures/queue/linked_queue.ts diff --git a/data_structures/queue.ts b/data_structures/queue/queue.ts similarity index 100% rename from data_structures/queue.ts rename to data_structures/queue/queue.ts diff --git a/data_structures/stack_queue.ts b/data_structures/queue/stack_queue.ts similarity index 89% rename from data_structures/stack_queue.ts rename to data_structures/queue/stack_queue.ts index cd27a391..e6f129d0 100644 --- a/data_structures/stack_queue.ts +++ b/data_structures/queue/stack_queue.ts @@ -1,17 +1,16 @@ /** - * A Stack Based Queue Implementation. - * The Queue data structure which follows the FIFO (First in First Out) rule. - * The dequeue operation in a normal stack based queue would be o(n), as the entire has to be shifted - * With the help of two stacks, the time complexity of this can be brought down to amortized-O(1). - * Here, one stack acts as an Enqueue stack where elements are added. + * A Stack Based Queue Implementation. + * The Queue data structure which follows the FIFO (First in First Out) rule. + * The dequeue operation in a normal stack based queue would be o(n), as the entire has to be shifted + * With the help of two stacks, the time complexity of this can be brought down to amortized-O(1). + * Here, one stack acts as an Enqueue stack where elements are added. * The other stack acts as a dequeue stack which helps in dequeuing the elements */ -import { Queue } from './queue'; -import { Stack } from './stack'; - -export class StackQueue implements Queue{ +import { Stack } from "../stack/stack"; +import { Queue } from "./queue"; +export class StackQueue implements Queue { private enqueueStack: Stack = new Stack(); private dequeueStack: Stack = new Stack(); @@ -44,7 +43,7 @@ export class StackQueue implements Queue{ /** * Shifts the elements from the enqueueStack to the dequeueStack - * In the worst case, all the elements from the enqueue stack needs to shifted, which needs O(n) time. + * In the worst case, all the elements from the enqueue stack needs to shifted, which needs O(n) time. * However, after the shift, elements can de dequeued at O(1). * This helps in dequeuing the elements in amortized O(1) time. */ diff --git a/data_structures/test/array_queue.test.ts b/data_structures/queue/test/array_queue.test.ts similarity index 100% rename from data_structures/test/array_queue.test.ts rename to data_structures/queue/test/array_queue.test.ts diff --git a/data_structures/test/linked_queue.test.ts b/data_structures/queue/test/linked_queue.test.ts similarity index 100% rename from data_structures/test/linked_queue.test.ts rename to data_structures/queue/test/linked_queue.test.ts diff --git a/data_structures/test/queue.ts b/data_structures/queue/test/queue.ts similarity index 99% rename from data_structures/test/queue.ts rename to data_structures/queue/test/queue.ts index 1dc7eaec..5fb57564 100644 --- a/data_structures/test/queue.ts +++ b/data_structures/queue/test/queue.ts @@ -1,4 +1,5 @@ import { Queue } from '../queue'; + type QueueConstructor = new () => Queue export function testQueue(Queue: QueueConstructor) { it("enqueue should add a new element to the queue", () => { diff --git a/data_structures/test/stack_queue.test.ts b/data_structures/queue/test/stack_queue.test.ts similarity index 100% rename from data_structures/test/stack_queue.test.ts rename to data_structures/queue/test/stack_queue.test.ts diff --git a/data_structures/hashing/hash_map_set.ts b/data_structures/set/hash_map_set.ts similarity index 80% rename from data_structures/hashing/hash_map_set.ts rename to data_structures/set/hash_map_set.ts index 35bfaf2b..ade41cbb 100644 --- a/data_structures/hashing/hash_map_set.ts +++ b/data_structures/set/hash_map_set.ts @@ -1,6 +1,6 @@ -import { Map } from "../map"; -import { MapSet } from "../map_set"; -import { HashMap } from "./hash_map"; +import { Map } from "../map/map"; +import { HashMap } from "../map/hash_map"; +import { MapSet } from "./map_set"; /** * This class is a representation of the Set data structure based on a hash map. diff --git a/data_structures/map_set.ts b/data_structures/set/map_set.ts similarity index 100% rename from data_structures/map_set.ts rename to data_structures/set/map_set.ts diff --git a/data_structures/set.ts b/data_structures/set/set.ts similarity index 100% rename from data_structures/set.ts rename to data_structures/set/set.ts diff --git a/data_structures/linked_list_stack.ts b/data_structures/stack/linked_list_stack.ts similarity index 97% rename from data_structures/linked_list_stack.ts rename to data_structures/stack/linked_list_stack.ts index 800a4150..11b1dbd9 100644 --- a/data_structures/linked_list_stack.ts +++ b/data_structures/stack/linked_list_stack.ts @@ -1,4 +1,4 @@ -import { SinglyLinkedList } from "./singly_linked_list"; +import { SinglyLinkedList } from "../list/singly_linked_list"; /** * This is an implementation of a stack, based on a linked list. diff --git a/data_structures/stack.ts b/data_structures/stack/stack.ts similarity index 100% rename from data_structures/stack.ts rename to data_structures/stack/stack.ts diff --git a/data_structures/test/linked_list_stack.test.ts b/data_structures/stack/test/linked_list_stack.test.ts similarity index 100% rename from data_structures/test/linked_list_stack.test.ts rename to data_structures/stack/test/linked_list_stack.test.ts diff --git a/data_structures/test/stack.test.ts b/data_structures/stack/test/stack.test.ts similarity index 100% rename from data_structures/test/stack.test.ts rename to data_structures/stack/test/stack.test.ts diff --git a/data_structures/test/doubly_linked_list.test.ts b/data_structures/test/doubly_linked_list.test.ts deleted file mode 100644 index 492af254..00000000 --- a/data_structures/test/doubly_linked_list.test.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { DoublyLinkedList } from '../doubly_linked_list'; -import { testLinkedList } from './linked_list'; - -describe("DoublyLinkedList", () => { - testLinkedList(DoublyLinkedList); - - it("should reverse the list", () => { - const list: DoublyLinkedList = new DoublyLinkedList(); - - list.append(1); - list.append(2); - list.append(3); - list.reverse(); - - expect(list.get(0)).toBe(3); - expect(list.get(1)).toBe(2); - }); - - it('should return null for reverse when list is empty', () => { - const list: DoublyLinkedList = new DoublyLinkedList(); - - expect(list.reverse()).toBeNull(); - }); -}); diff --git a/data_structures/binary_search_tree.ts b/data_structures/tree/binary_search_tree.ts similarity index 100% rename from data_structures/binary_search_tree.ts rename to data_structures/tree/binary_search_tree.ts diff --git a/data_structures/test/binary_search_tree.test.ts b/data_structures/tree/test/binary_search_tree.test.ts similarity index 100% rename from data_structures/test/binary_search_tree.test.ts rename to data_structures/tree/test/binary_search_tree.test.ts