edited data structures docs and added some tests in GraphTest
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -19,6 +19,7 @@ package structures
|
||||
* \
|
||||
* 4
|
||||
* the same complexity is true for adding and removing nodes
|
||||
*
|
||||
*/
|
||||
|
||||
class BinaryTree {
|
||||
@ -31,7 +32,7 @@ class BinaryTree {
|
||||
/**
|
||||
* adding a new element to the tree
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*/
|
||||
fun add(value: Int) {
|
||||
fun addRec(current: Node?, value: Int) : Node {
|
||||
@ -59,7 +60,7 @@ class BinaryTree {
|
||||
/**
|
||||
* removing an element from the tree
|
||||
*
|
||||
* @value - the value of the element to be removed
|
||||
* @param value - the value of the element to be removed
|
||||
*/
|
||||
fun remove(value: Int) {
|
||||
fun smallestValue(root: Node) : Int {
|
||||
@ -103,7 +104,7 @@ class BinaryTree {
|
||||
/**
|
||||
* checking for the existence of an element in the tree
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*
|
||||
* @return - returns true if the element exists
|
||||
*/
|
||||
@ -115,7 +116,11 @@ class BinaryTree {
|
||||
if (value == current.value()) {
|
||||
return true
|
||||
}
|
||||
return if (value < current.value()) containsRec(current.leftNode(), value) else containsRec(current.rightNode(), value)
|
||||
return if (value < current.value()) {
|
||||
containsRec(current.leftNode(), value)
|
||||
} else {
|
||||
containsRec(current.rightNode(), value)
|
||||
}
|
||||
}
|
||||
|
||||
return containsRec(root, value)
|
||||
@ -203,12 +208,9 @@ class BinaryTree {
|
||||
while (queue.isNotEmpty()) {
|
||||
val node = queue.remove()
|
||||
items.add(node.value())
|
||||
if (node.leftNode() != null) {
|
||||
queue.add(node.leftNode()!!)
|
||||
}
|
||||
if (node.rightNode() != null) {
|
||||
queue.add(node.rightNode()!!)
|
||||
}
|
||||
|
||||
node.leftNode()?.let(queue::add)
|
||||
node.rightNode()?.let(queue::add)
|
||||
}
|
||||
|
||||
return items
|
||||
@ -219,9 +221,11 @@ class BinaryTree {
|
||||
/**
|
||||
* represents a tree node
|
||||
*
|
||||
* @value - node value
|
||||
* @left - left child node
|
||||
* @right - right child node
|
||||
* @constructor
|
||||
* @property value - node value
|
||||
* @property left - left child node
|
||||
* @property right - right child node
|
||||
*
|
||||
*/
|
||||
class Node(
|
||||
private var value: Int,
|
||||
|
||||
@ -8,6 +8,7 @@ package structures
|
||||
* time to insert an element at the beginning and end of the list: O(1)
|
||||
* insertion time in the middle by index: O(n)
|
||||
* delete: O(n)
|
||||
*
|
||||
*/
|
||||
|
||||
class CircularLinkedList<T>(
|
||||
@ -34,8 +35,10 @@ class CircularLinkedList<T>(
|
||||
/**
|
||||
* singly linked list node
|
||||
*
|
||||
* @value - node value
|
||||
* @next - link to the next element (assuming the element is not the last one)
|
||||
* @constructor
|
||||
* @property value - node value
|
||||
* @property next - link to the next element (assuming the element is not the last one)
|
||||
*
|
||||
*/
|
||||
class Node<T>(
|
||||
private val value: T,
|
||||
@ -85,7 +88,7 @@ class CircularLinkedList<T>(
|
||||
/**
|
||||
* checks if an element is in the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*
|
||||
* @return returns true if the value exists in the list
|
||||
*/
|
||||
@ -113,7 +116,7 @@ class CircularLinkedList<T>(
|
||||
/**
|
||||
* removes an element from the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*
|
||||
* @return returns true if the element was successfully removed
|
||||
*/
|
||||
@ -146,8 +149,8 @@ class CircularLinkedList<T>(
|
||||
/**
|
||||
* add element by index
|
||||
*
|
||||
* @index - the index where the new element should be added
|
||||
* @value - the value of the new element
|
||||
* @param index - the index where the new element should be added
|
||||
* @param value - the value of the new element
|
||||
*
|
||||
* @return returns true if the element was successfully added at the specified index
|
||||
*/
|
||||
@ -180,7 +183,7 @@ class CircularLinkedList<T>(
|
||||
/**
|
||||
* adds an element to the beginning of the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*/
|
||||
fun addFirst(value: T) {
|
||||
val node = Node(value)
|
||||
@ -198,7 +201,7 @@ class CircularLinkedList<T>(
|
||||
/**
|
||||
* adds an element to the end of the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*/
|
||||
fun addLast(value: T) {
|
||||
val newNode = Node(value)
|
||||
|
||||
@ -34,9 +34,11 @@ class DoubleLinkedList<T>(
|
||||
/**
|
||||
* doubly linked list node
|
||||
*
|
||||
* @value - node value
|
||||
* @prev - link to the previous element (assuming the element is not the first one)
|
||||
* @next - link to the next element (assuming the element is not the last one)
|
||||
* @constructor
|
||||
* @property value - node value
|
||||
* @property prev - link to the previous element (assuming the element is not the first one)
|
||||
* @property next - link to the next element (assuming the element is not the last one)
|
||||
*
|
||||
*/
|
||||
class Node<T>(
|
||||
private val value: T,
|
||||
@ -87,7 +89,7 @@ class DoubleLinkedList<T>(
|
||||
/**
|
||||
* checks if an element is in the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*
|
||||
* @return returns true if the value exists in the list
|
||||
*/
|
||||
@ -114,7 +116,7 @@ class DoubleLinkedList<T>(
|
||||
/**
|
||||
* removes an element from the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*
|
||||
* @return returns true if the element was successfully removed
|
||||
*/
|
||||
@ -151,8 +153,8 @@ class DoubleLinkedList<T>(
|
||||
/**
|
||||
* add element by index
|
||||
*
|
||||
* @index - the index where the new element should be added
|
||||
* @value - the value of the new element
|
||||
* @param index - the index where the new element should be added
|
||||
* @param value - the value of the new element
|
||||
*
|
||||
* @return returns true if the element was successfully added at the specified index
|
||||
*/
|
||||
@ -191,7 +193,7 @@ class DoubleLinkedList<T>(
|
||||
/**
|
||||
* adds an element to the beginning of the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*/
|
||||
fun addFirst(value: T) {
|
||||
val firstNode = first
|
||||
@ -212,7 +214,7 @@ class DoubleLinkedList<T>(
|
||||
/**
|
||||
* adds an element to the end of the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*/
|
||||
fun addLast(value: T) {
|
||||
val lastNode = last
|
||||
|
||||
@ -16,14 +16,14 @@ class Graph<T> {
|
||||
/**
|
||||
* adds a new vertex
|
||||
*
|
||||
* @value - the value of the new vertex
|
||||
* @param value - the value of the new vertex
|
||||
*/
|
||||
fun addVertex(value: T) = data.putIfAbsent(Vertex(value), mutableListOf())
|
||||
|
||||
/**
|
||||
* removes a vertex from a graph
|
||||
*
|
||||
* @value - vertex value
|
||||
* @param value - vertex value
|
||||
*/
|
||||
fun removeVertex(value: T) {
|
||||
val removingVertex = Vertex(value)
|
||||
@ -36,8 +36,8 @@ class Graph<T> {
|
||||
/**
|
||||
* adds an edge between two vertices
|
||||
*
|
||||
* @value1 - first vertex value
|
||||
* @value2 - second vertex value
|
||||
* @param value1 - first vertex value
|
||||
* @param value2 - second vertex value
|
||||
*/
|
||||
fun addEdge(value1: T, value2: T) {
|
||||
val vertex1 = Vertex(value1)
|
||||
@ -49,8 +49,8 @@ class Graph<T> {
|
||||
/**
|
||||
* removes an edge between two vertices
|
||||
*
|
||||
* @value1 - first vertex value
|
||||
* @value2 - second vertex value
|
||||
* @param value1 - first vertex value
|
||||
* @param value2 - second vertex value
|
||||
*/
|
||||
fun removeEdge(value1: T, value2: T) {
|
||||
val vertex1 = Vertex(value1)
|
||||
@ -62,7 +62,7 @@ class Graph<T> {
|
||||
/**
|
||||
* returns the associated vertices with the given vertex
|
||||
*
|
||||
* @value - vertex value
|
||||
* @param value - vertex value
|
||||
*/
|
||||
fun connectedVertexes(value: T) = data[Vertex(value)] ?: listOf()
|
||||
|
||||
@ -93,8 +93,9 @@ class Graph<T> {
|
||||
fun breadthFirstTraversal() : List<Vertex<T>> {
|
||||
val visited = LinkedHashSet<Vertex<T>>()
|
||||
val queue = LinkedList<Vertex<T>>()
|
||||
queue.add(data.keys.first())
|
||||
visited.add(data.keys.first())
|
||||
val firstVertex = data.keys.firstOrNull() ?: return listOf()
|
||||
queue.add(firstVertex)
|
||||
visited.add(firstVertex)
|
||||
while (queue.isNotEmpty()) {
|
||||
val vertex = queue.poll()
|
||||
data[vertex]?.forEach { v ->
|
||||
|
||||
@ -16,7 +16,7 @@ package structures
|
||||
*
|
||||
*/
|
||||
|
||||
class MinHeap(private val maxSize: Int) {
|
||||
class MinHeap(maxSize: Int) {
|
||||
|
||||
private val heap = Array(maxSize) { 0 }
|
||||
|
||||
|
||||
@ -3,16 +3,21 @@ package structures
|
||||
import java.lang.IllegalStateException
|
||||
|
||||
/**
|
||||
* data structure: dynamic array
|
||||
* data structure: simple java.util.ArrayList implementation
|
||||
*
|
||||
* description: wrapper over a regular array, in which indexes are checked and
|
||||
* when the array overflows, its size increases dynamically
|
||||
*
|
||||
* @constructor
|
||||
* @param capacity initial array size
|
||||
* @property capacity initial array size
|
||||
*
|
||||
* P.S. Kotlin lists use under the hood java.util.ArrayList
|
||||
* for example:
|
||||
* val numbers = listOf(1, 2, 3) // java.util.ArrayList
|
||||
* val symbols = mutableListOf('a', 'b', 'c') // also java.util.ArrayList
|
||||
*/
|
||||
|
||||
class DynamicArray(private var capacity: Int = 10) {
|
||||
class MyArrayList(private var capacity: Int = 10) {
|
||||
private var data = Array(capacity) { 0 }
|
||||
private var index = 0
|
||||
|
||||
@ -90,8 +95,8 @@ class DynamicArray(private var capacity: Int = 10) {
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the size of the array
|
||||
*
|
||||
* @return returns the size of the array
|
||||
*/
|
||||
fun capacity() = capacity
|
||||
|
||||
@ -7,6 +7,7 @@ import kotlin.collections.ArrayList
|
||||
*
|
||||
* description: the queue is organized on a FIFO basis (first in, first out), all operations are performed in O(1),
|
||||
* except for the operation of deleting from the middle of the queue, which in the worst case takes O(n) time
|
||||
*
|
||||
*/
|
||||
|
||||
interface Queue<T> {
|
||||
@ -14,7 +15,7 @@ interface Queue<T> {
|
||||
/**
|
||||
* adding to the front of the queue
|
||||
*
|
||||
* @item - added element
|
||||
* @param item - added element
|
||||
*/
|
||||
fun offer(item: T)
|
||||
|
||||
@ -47,8 +48,8 @@ interface Queue<T> {
|
||||
fun poll() : T?
|
||||
|
||||
/**
|
||||
* returns true if the queue is empty
|
||||
*
|
||||
* @return returns true if the queue is empty
|
||||
*/
|
||||
fun isEmpty() : Boolean
|
||||
|
||||
@ -61,7 +62,7 @@ interface Queue<T> {
|
||||
/**
|
||||
* removes an element from the middle of the queue
|
||||
*
|
||||
* returns true if the element was successfully removed
|
||||
* @return returns true if the element was successfully removed
|
||||
*/
|
||||
fun remove(item: T) : Boolean
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ package structures
|
||||
* time to insert an element at the beginning and end of the list: O(1)
|
||||
* insertion time in the middle by index: O(n)
|
||||
* delete: O(n)
|
||||
*
|
||||
*/
|
||||
|
||||
class SingleLinkedList<T>(
|
||||
@ -34,8 +35,10 @@ class SingleLinkedList<T>(
|
||||
/**
|
||||
* singly linked list node
|
||||
*
|
||||
* @value - node value
|
||||
* @next - link to the next element (assuming the element is not the last one)
|
||||
* @constructor
|
||||
* @property value - node value
|
||||
* @property next - link to the next element (assuming the element is not the last one)
|
||||
*
|
||||
*/
|
||||
class Node<T>(
|
||||
private val value: T,
|
||||
@ -76,7 +79,7 @@ class SingleLinkedList<T>(
|
||||
/**
|
||||
* checks if an element is in the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*
|
||||
* @return returns true if the value exists in the list
|
||||
*/
|
||||
@ -103,7 +106,7 @@ class SingleLinkedList<T>(
|
||||
/**
|
||||
* removes an element from the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*
|
||||
* @return returns true if the element was successfully removed
|
||||
*/
|
||||
@ -135,8 +138,8 @@ class SingleLinkedList<T>(
|
||||
/**
|
||||
* add element by index
|
||||
*
|
||||
* @index - the index where the new element should be added
|
||||
* @value - the value of the new element
|
||||
* @param index - the index where the new element should be added
|
||||
* @param value - the value of the new element
|
||||
*
|
||||
* @return returns true if the element was successfully added at the specified index
|
||||
*/
|
||||
@ -169,7 +172,7 @@ class SingleLinkedList<T>(
|
||||
/**
|
||||
* adds an element to the beginning of the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*/
|
||||
fun addFirst(value: T) {
|
||||
val node = Node(value)
|
||||
@ -186,7 +189,7 @@ class SingleLinkedList<T>(
|
||||
/**
|
||||
* adds an element to the end of the list
|
||||
*
|
||||
* @value - element value
|
||||
* @param value - element value
|
||||
*/
|
||||
fun addLast(value: T) {
|
||||
val newNode = Node(value)
|
||||
|
||||
@ -7,13 +7,15 @@ import kotlin.collections.ArrayList
|
||||
* data structure: stack
|
||||
*
|
||||
* description: the stack uses the LIFO principle (last in, first out), all operations are performed in O(1) time
|
||||
*
|
||||
*/
|
||||
|
||||
interface Stack<T> {
|
||||
|
||||
/**
|
||||
* adds an element to the top of the stack
|
||||
*
|
||||
* @item - element
|
||||
* @param item - element
|
||||
*/
|
||||
fun push(item: T)
|
||||
|
||||
@ -42,9 +44,9 @@ interface Stack<T> {
|
||||
fun clear()
|
||||
|
||||
/**
|
||||
* implementation using dynamic ArrayList
|
||||
* implementation using ArrayList
|
||||
*
|
||||
* @T - stack element type
|
||||
* @param T - stack element type
|
||||
*/
|
||||
class ArrayListStack<T> : Stack<T> {
|
||||
private val data = ArrayList<T>()
|
||||
@ -77,7 +79,7 @@ interface Stack<T> {
|
||||
/**
|
||||
* linked list implementation
|
||||
*
|
||||
* @T - тип элементов стэка
|
||||
* @param T - тип элементов стэка
|
||||
*/
|
||||
class LinkedListStack<T> : Stack<T> {
|
||||
private val data = java.util.LinkedList<T>()
|
||||
|
||||
@ -5,6 +5,43 @@ import org.junit.jupiter.api.Assertions.*
|
||||
|
||||
internal class GraphTest {
|
||||
|
||||
@Test
|
||||
fun test_add_and_remove_vertexes() {
|
||||
val graph = Graph<Int>()
|
||||
|
||||
graph.addVertex(10)
|
||||
graph.addVertex(20)
|
||||
graph.addVertex(30)
|
||||
|
||||
graph.addEdge(10, 20)
|
||||
graph.addEdge(10, 30)
|
||||
graph.addEdge(20, 30)
|
||||
|
||||
graph.removeVertex(10)
|
||||
|
||||
assertEquals(listOf(Vertex(20), Vertex(30)), graph.breadthFirstTraversal())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_empty() {
|
||||
val graph = Graph<Int>()
|
||||
|
||||
graph.addVertex(10)
|
||||
graph.addVertex(20)
|
||||
graph.addVertex(30)
|
||||
|
||||
graph.addEdge(10, 20)
|
||||
graph.addEdge(10, 30)
|
||||
graph.addEdge(20, 30)
|
||||
|
||||
graph.removeVertex(10)
|
||||
graph.removeVertex(20)
|
||||
graph.removeVertex(30)
|
||||
|
||||
val expected: List<Vertex<Int>> = listOf()
|
||||
assertEquals(expected, graph.breadthFirstTraversal())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_depth_first_traversal() {
|
||||
val graph = Graph<Int>()
|
||||
|
||||
@ -3,11 +3,11 @@ package structures
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
|
||||
internal class DynamicArrayTest {
|
||||
internal class MyArrayListTest {
|
||||
|
||||
@Test
|
||||
fun test_add() {
|
||||
val array = DynamicArray().apply {
|
||||
val array = MyArrayList().apply {
|
||||
for (i in 0 until 10) { add(i) }
|
||||
}
|
||||
assertEquals(5, array.get(5))
|
||||
@ -15,7 +15,7 @@ internal class DynamicArrayTest {
|
||||
|
||||
@Test
|
||||
fun test_set() {
|
||||
val array = DynamicArray().apply {
|
||||
val array = MyArrayList().apply {
|
||||
add(1)
|
||||
add(2)
|
||||
add(3)
|
||||
@ -27,7 +27,7 @@ internal class DynamicArrayTest {
|
||||
|
||||
@Test
|
||||
fun test_contains() {
|
||||
val array = DynamicArray().apply {
|
||||
val array = MyArrayList().apply {
|
||||
add(1)
|
||||
add(2)
|
||||
add(100)
|
||||
@ -38,7 +38,7 @@ internal class DynamicArrayTest {
|
||||
|
||||
@Test
|
||||
fun test_remove() {
|
||||
val array = DynamicArray().apply {
|
||||
val array = MyArrayList().apply {
|
||||
add(0)
|
||||
add(1)
|
||||
add(2)
|
||||
@ -54,7 +54,7 @@ internal class DynamicArrayTest {
|
||||
|
||||
@Test
|
||||
fun test_increasing() {
|
||||
val array = DynamicArray().apply {
|
||||
val array = MyArrayList().apply {
|
||||
for (i in 0 until 10) { add(i) }
|
||||
}
|
||||
assertEquals(false, array.contains(10))
|
||||
Reference in New Issue
Block a user