Skip to content

Commit 671aa26

Browse files
author
evitwilly
committed
edited data structures docs and added some tests in GraphTest
1 parent 9aee12a commit 671aa26

File tree

16 files changed

+123
-65
lines changed

16 files changed

+123
-65
lines changed
Binary file not shown.
Binary file not shown.

.gradle/7.1/fileHashes/fileHashes.bin

300 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

src/main/kotlin/structures/BinaryTree.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package structures
1919
* \
2020
* 4
2121
* the same complexity is true for adding and removing nodes
22+
*
2223
*/
2324

2425
class BinaryTree {
@@ -31,7 +32,7 @@ class BinaryTree {
3132
/**
3233
* adding a new element to the tree
3334
*
34-
* @value - element value
35+
* @param value - element value
3536
*/
3637
fun add(value: Int) {
3738
fun addRec(current: Node?, value: Int) : Node {
@@ -59,7 +60,7 @@ class BinaryTree {
5960
/**
6061
* removing an element from the tree
6162
*
62-
* @value - the value of the element to be removed
63+
* @param value - the value of the element to be removed
6364
*/
6465
fun remove(value: Int) {
6566
fun smallestValue(root: Node) : Int {
@@ -103,7 +104,7 @@ class BinaryTree {
103104
/**
104105
* checking for the existence of an element in the tree
105106
*
106-
* @value - element value
107+
* @param value - element value
107108
*
108109
* @return - returns true if the element exists
109110
*/
@@ -115,7 +116,11 @@ class BinaryTree {
115116
if (value == current.value()) {
116117
return true
117118
}
118-
return if (value < current.value()) containsRec(current.leftNode(), value) else containsRec(current.rightNode(), value)
119+
return if (value < current.value()) {
120+
containsRec(current.leftNode(), value)
121+
} else {
122+
containsRec(current.rightNode(), value)
123+
}
119124
}
120125

121126
return containsRec(root, value)
@@ -203,12 +208,9 @@ class BinaryTree {
203208
while (queue.isNotEmpty()) {
204209
val node = queue.remove()
205210
items.add(node.value())
206-
if (node.leftNode() != null) {
207-
queue.add(node.leftNode()!!)
208-
}
209-
if (node.rightNode() != null) {
210-
queue.add(node.rightNode()!!)
211-
}
211+
212+
node.leftNode()?.let(queue::add)
213+
node.rightNode()?.let(queue::add)
212214
}
213215

214216
return items
@@ -219,9 +221,11 @@ class BinaryTree {
219221
/**
220222
* represents a tree node
221223
*
222-
* @value - node value
223-
* @left - left child node
224-
* @right - right child node
224+
* @constructor
225+
* @property value - node value
226+
* @property left - left child node
227+
* @property right - right child node
228+
*
225229
*/
226230
class Node(
227231
private var value: Int,

src/main/kotlin/structures/CircularLinkedList.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package structures
88
* time to insert an element at the beginning and end of the list: O(1)
99
* insertion time in the middle by index: O(n)
1010
* delete: O(n)
11+
*
1112
*/
1213

1314
class CircularLinkedList<T>(
@@ -34,8 +35,10 @@ class CircularLinkedList<T>(
3435
/**
3536
* singly linked list node
3637
*
37-
* @value - node value
38-
* @next - link to the next element (assuming the element is not the last one)
38+
* @constructor
39+
* @property value - node value
40+
* @property next - link to the next element (assuming the element is not the last one)
41+
*
3942
*/
4043
class Node<T>(
4144
private val value: T,
@@ -85,7 +88,7 @@ class CircularLinkedList<T>(
8588
/**
8689
* checks if an element is in the list
8790
*
88-
* @value - element value
91+
* @param value - element value
8992
*
9093
* @return returns true if the value exists in the list
9194
*/
@@ -113,7 +116,7 @@ class CircularLinkedList<T>(
113116
/**
114117
* removes an element from the list
115118
*
116-
* @value - element value
119+
* @param value - element value
117120
*
118121
* @return returns true if the element was successfully removed
119122
*/
@@ -146,8 +149,8 @@ class CircularLinkedList<T>(
146149
/**
147150
* add element by index
148151
*
149-
* @index - the index where the new element should be added
150-
* @value - the value of the new element
152+
* @param index - the index where the new element should be added
153+
* @param value - the value of the new element
151154
*
152155
* @return returns true if the element was successfully added at the specified index
153156
*/
@@ -180,7 +183,7 @@ class CircularLinkedList<T>(
180183
/**
181184
* adds an element to the beginning of the list
182185
*
183-
* @value - element value
186+
* @param value - element value
184187
*/
185188
fun addFirst(value: T) {
186189
val node = Node(value)
@@ -198,7 +201,7 @@ class CircularLinkedList<T>(
198201
/**
199202
* adds an element to the end of the list
200203
*
201-
* @value - element value
204+
* @param value - element value
202205
*/
203206
fun addLast(value: T) {
204207
val newNode = Node(value)

src/main/kotlin/structures/DoubleLinkedList.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ class DoubleLinkedList<T>(
3434
/**
3535
* doubly linked list node
3636
*
37-
* @value - node value
38-
* @prev - link to the previous element (assuming the element is not the first one)
39-
* @next - link to the next element (assuming the element is not the last one)
37+
* @constructor
38+
* @property value - node value
39+
* @property prev - link to the previous element (assuming the element is not the first one)
40+
* @property next - link to the next element (assuming the element is not the last one)
41+
*
4042
*/
4143
class Node<T>(
4244
private val value: T,
@@ -87,7 +89,7 @@ class DoubleLinkedList<T>(
8789
/**
8890
* checks if an element is in the list
8991
*
90-
* @value - element value
92+
* @param value - element value
9193
*
9294
* @return returns true if the value exists in the list
9395
*/
@@ -114,7 +116,7 @@ class DoubleLinkedList<T>(
114116
/**
115117
* removes an element from the list
116118
*
117-
* @value - element value
119+
* @param value - element value
118120
*
119121
* @return returns true if the element was successfully removed
120122
*/
@@ -151,8 +153,8 @@ class DoubleLinkedList<T>(
151153
/**
152154
* add element by index
153155
*
154-
* @index - the index where the new element should be added
155-
* @value - the value of the new element
156+
* @param index - the index where the new element should be added
157+
* @param value - the value of the new element
156158
*
157159
* @return returns true if the element was successfully added at the specified index
158160
*/
@@ -191,7 +193,7 @@ class DoubleLinkedList<T>(
191193
/**
192194
* adds an element to the beginning of the list
193195
*
194-
* @value - element value
196+
* @param value - element value
195197
*/
196198
fun addFirst(value: T) {
197199
val firstNode = first
@@ -212,7 +214,7 @@ class DoubleLinkedList<T>(
212214
/**
213215
* adds an element to the end of the list
214216
*
215-
* @value - element value
217+
* @param value - element value
216218
*/
217219
fun addLast(value: T) {
218220
val lastNode = last

src/main/kotlin/structures/Graph.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class Graph<T> {
1616
/**
1717
* adds a new vertex
1818
*
19-
* @value - the value of the new vertex
19+
* @param value - the value of the new vertex
2020
*/
2121
fun addVertex(value: T) = data.putIfAbsent(Vertex(value), mutableListOf())
2222

2323
/**
2424
* removes a vertex from a graph
2525
*
26-
* @value - vertex value
26+
* @param value - vertex value
2727
*/
2828
fun removeVertex(value: T) {
2929
val removingVertex = Vertex(value)
@@ -36,8 +36,8 @@ class Graph<T> {
3636
/**
3737
* adds an edge between two vertices
3838
*
39-
* @value1 - first vertex value
40-
* @value2 - second vertex value
39+
* @param value1 - first vertex value
40+
* @param value2 - second vertex value
4141
*/
4242
fun addEdge(value1: T, value2: T) {
4343
val vertex1 = Vertex(value1)
@@ -49,8 +49,8 @@ class Graph<T> {
4949
/**
5050
* removes an edge between two vertices
5151
*
52-
* @value1 - first vertex value
53-
* @value2 - second vertex value
52+
* @param value1 - first vertex value
53+
* @param value2 - second vertex value
5454
*/
5555
fun removeEdge(value1: T, value2: T) {
5656
val vertex1 = Vertex(value1)
@@ -62,7 +62,7 @@ class Graph<T> {
6262
/**
6363
* returns the associated vertices with the given vertex
6464
*
65-
* @value - vertex value
65+
* @param value - vertex value
6666
*/
6767
fun connectedVertexes(value: T) = data[Vertex(value)] ?: listOf()
6868

@@ -93,8 +93,9 @@ class Graph<T> {
9393
fun breadthFirstTraversal() : List<Vertex<T>> {
9494
val visited = LinkedHashSet<Vertex<T>>()
9595
val queue = LinkedList<Vertex<T>>()
96-
queue.add(data.keys.first())
97-
visited.add(data.keys.first())
96+
val firstVertex = data.keys.firstOrNull() ?: return listOf()
97+
queue.add(firstVertex)
98+
visited.add(firstVertex)
9899
while (queue.isNotEmpty()) {
99100
val vertex = queue.poll()
100101
data[vertex]?.forEach { v ->

src/main/kotlin/structures/MinHeap.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package structures
1616
*
1717
*/
1818

19-
class MinHeap(private val maxSize: Int) {
19+
class MinHeap(maxSize: Int) {
2020

2121
private val heap = Array(maxSize) { 0 }
2222

src/main/kotlin/structures/DynamicArray.kt renamed to src/main/kotlin/structures/MyArrayList.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ package structures
33
import java.lang.IllegalStateException
44

55
/**
6-
* data structure: dynamic array
6+
* data structure: simple java.util.ArrayList implementation
77
*
88
* description: wrapper over a regular array, in which indexes are checked and
99
* when the array overflows, its size increases dynamically
1010
*
1111
* @constructor
12-
* @param capacity initial array size
12+
* @property capacity initial array size
13+
*
14+
* P.S. Kotlin lists use under the hood java.util.ArrayList
15+
* for example:
16+
* val numbers = listOf(1, 2, 3) // java.util.ArrayList
17+
* val symbols = mutableListOf('a', 'b', 'c') // also java.util.ArrayList
1318
*/
1419

15-
class DynamicArray(private var capacity: Int = 10) {
20+
class MyArrayList(private var capacity: Int = 10) {
1621
private var data = Array(capacity) { 0 }
1722
private var index = 0
1823

@@ -90,8 +95,8 @@ class DynamicArray(private var capacity: Int = 10) {
9095
}
9196

9297
/**
93-
* returns the size of the array
9498
*
99+
* @return returns the size of the array
95100
*/
96101
fun capacity() = capacity
97102

src/main/kotlin/structures/Queue.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import kotlin.collections.ArrayList
77
*
88
* description: the queue is organized on a FIFO basis (first in, first out), all operations are performed in O(1),
99
* except for the operation of deleting from the middle of the queue, which in the worst case takes O(n) time
10+
*
1011
*/
1112

1213
interface Queue<T> {
1314

1415
/**
1516
* adding to the front of the queue
1617
*
17-
* @item - added element
18+
* @param item - added element
1819
*/
1920
fun offer(item: T)
2021

@@ -47,8 +48,8 @@ interface Queue<T> {
4748
fun poll() : T?
4849

4950
/**
50-
* returns true if the queue is empty
5151
*
52+
* @return returns true if the queue is empty
5253
*/
5354
fun isEmpty() : Boolean
5455

@@ -61,7 +62,7 @@ interface Queue<T> {
6162
/**
6263
* removes an element from the middle of the queue
6364
*
64-
* returns true if the element was successfully removed
65+
* @return returns true if the element was successfully removed
6566
*/
6667
fun remove(item: T) : Boolean
6768

0 commit comments

Comments
 (0)