10 Convert into multiplatform library (#13)

* 10: Convert library into Kotlin Multiplatform library

* 10: Setup CI

* 10: Setup CI

* 10: Test CI

* 10: Test CI

* 10: Test CI

* 10: Test CI

* 10: Test CI

* 10: Test CI

* 10: Test CI
This commit is contained in:
2022-12-15 15:34:36 +01:00
committed by GitHub
parent 72c94f280d
commit dd773e3b16
63 changed files with 2467 additions and 1104 deletions

View File

@ -0,0 +1,24 @@
package com.github.adriankuta
import kotlin.jvm.JvmSynthetic
interface ChildDeclarationInterface<T> {
/**
* This method is used to easily create child in node.
* ```
* val root = tree("World") {
* child("North America") {
* child("USA")
* }
* child("Europe") {
* child("Poland")
* child("Germany")
* }
* }
* ```
* @return New created TreeNode.
*/
@JvmSynthetic
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null): TreeNode<T>
}

View File

@ -0,0 +1,40 @@
package com.github.adriankuta
/**
* Tree is iterated by using `Pre-order Traversal Algorithm"
* 1. Check if the current node is empty or null.
* 2. Display the data part of the root (or current node).
* 3. Traverse the left subtree by recursively calling the pre-order function.
* 4. Traverse the right subtree by recursively calling the pre-order function.
* ```
* E.g.
* 1
* / | \
* / | \
* 2 3 4
* / \ / | \
* 5 6 7 8 9
* / / | \
* 10 11 12 13
*
* Output: 1 2 5 10 6 11 12 13 3 4 7 8 9
* ```
*/
class PreOrderTreeIterator<T>(root: TreeNode<T>) : Iterator<TreeNode<T>> {
private val stack = ArrayDeque<TreeNode<T>>()
init {
stack.addLast(root)
}
override fun hasNext(): Boolean = stack.isNotEmpty()
override fun next(): TreeNode<T> {
val node = stack.removeLast()
node.children
.asReversed()
.forEach { stack.addLast(it) }
return node
}
}

View File

@ -0,0 +1,143 @@
package com.github.adriankuta
import kotlin.jvm.JvmSynthetic
open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationInterface<T> {
private var _parent: TreeNode<T>? = null
/**
* The converse notion of a child, an immediate ancestor.
*/
val parent: TreeNode<T>?
get() = _parent
private val _children = mutableListOf<TreeNode<T>>()
/**
* A group of nodes with the same parent.
*/
val children: List<TreeNode<T>>
get() = _children
/**
* Add new child to current node or root.
*
* @param child A node which will be directly connected to current node.
*/
fun addChild(child: TreeNode<T>) {
child._parent = this
_children.add(child)
}
@JvmSynthetic
override fun child(value: T, childDeclaration: ChildDeclaration<T>?): TreeNode<T> {
val newChild = TreeNode(value)
if(childDeclaration != null)
newChild.childDeclaration()
_children.add(newChild)
return newChild
}
/**
* Removes a single instance of the specified node from this tree, if it is present.
*
* @return `true` if the node has been successfully removed; `false` if it was not present in the tree.
*/
fun removeChild(child: TreeNode<T>): Boolean {
println(child.value)
val removed = child._parent?._children?.remove(child)
child._parent = null
return removed ?: false
}
/**
* This function go through tree and counts children. Root element is not counted.
* @return All child and nested child count.
*/
fun nodeCount(): Int {
if (_children.isEmpty())
return 0
return _children.size +
_children.sumOf { it.nodeCount() }
}
/**
* @return The number of edges on the longest path between current node and a descendant leaf.
*/
fun height(): Int {
val childrenMaxDepth = _children.map { it.height() }
.maxOrNull()
?: -1 // -1 because this method counts nodes, and edges are always one less then nodes.
return childrenMaxDepth + 1
}
/**
* Distance is the number of edges along the shortest path between two nodes.
* @return The distance between current node and the root.
*/
fun depth(): Int {
var depth = 0
var tempParent = parent
while (tempParent != null) {
depth++
tempParent = tempParent.parent
}
return depth
}
/**
* Remove all children from root and every node in tree.
*/
fun clear() {
_parent = null
_children.forEach { it.clear() }
_children.clear()
}
override fun toString(): String {
return value.toString()
}
fun prettyString(): String {
val stringBuilder = StringBuilder()
print(stringBuilder, "", "")
return stringBuilder.toString()
}
private fun print(stringBuilder: StringBuilder, prefix: String, childrenPrefix: String) {
stringBuilder.append(prefix)
stringBuilder.append(value)
stringBuilder.append('\n')
val childIterator = _children.iterator()
while (childIterator.hasNext()) {
val node = childIterator.next()
if (childIterator.hasNext()) {
node.print(stringBuilder, "$childrenPrefix├── ", "$childrenPrefix")
} else {
node.print(stringBuilder, "$childrenPrefix└── ", "$childrenPrefix ")
}
}
}
/**
* Tree is iterated by using `Pre-order Traversal Algorithm"
* 1. Check if the current node is empty or null.
* 2. Display the data part of the root (or current node).
* 3. Traverse the left subtree by recursively calling the pre-order function.
* 4. Traverse the right subtree by recursively calling the pre-order function.
* ```
* E.g.
* 1
* / | \
* / | \
* 2 3 4
* / \ / | \
* 5 6 7 8 9
* / / | \
* 10 11 12 13
*
* Output: 1 2 5 10 6 11 12 13 3 4 7 8 9
* ```
*/
override fun iterator(): Iterator<TreeNode<T>> = PreOrderTreeIterator(this)
}

View File

@ -0,0 +1,20 @@
package com.github.adriankuta
import kotlin.jvm.JvmSynthetic
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
/**
* This method can be used to initialize new tree.
* ```
* val root = tree("World") { ... }
* ```
* @param root Root element of new tree.
* @see [ChildDeclarationInterface.child]
*/
@JvmSynthetic
inline fun<reified T> tree(root: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> {
val treeNode = TreeNode(root)
treeNode.childDeclaration()
return treeNode
}

View File

@ -0,0 +1,16 @@
package com.github.adriankuta
import kotlin.test.Test
import kotlin.test.assertEquals
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}

View File

@ -0,0 +1,144 @@
package com.github.adriankuta
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
class TreeNodeTest {
@Test
fun removeNodeTest() {
val root = TreeNode("Root")
val beveragesNode = TreeNode("Beverages")
val curdNode = TreeNode("Curd")
root.addChild(beveragesNode)
root.addChild(curdNode)
val teaNode = TreeNode("tea")
val coffeeNode = TreeNode("coffee")
val milkShakeNode = TreeNode("Milk Shake")
beveragesNode.addChild(teaNode)
beveragesNode.addChild(coffeeNode)
beveragesNode.addChild(milkShakeNode)
val gingerTeaNode = TreeNode("ginger tea")
val normalTeaNode = TreeNode("normal tea")
teaNode.addChild(gingerTeaNode)
teaNode.addChild(normalTeaNode)
val yogurtNode = TreeNode("yogurt")
val lassiNode = TreeNode("lassi")
curdNode.addChild(yogurtNode)
curdNode.addChild(lassiNode)
assertEquals(
"Root\n" +
"├── Beverages\n" +
"│ ├── tea\n" +
"│ │ ├── ginger tea\n" +
"│ │ └── normal tea\n" +
"│ ├── coffee\n" +
"│ └── Milk Shake\n" +
"└── Curd\n" +
" ├── yogurt\n" +
" └── lassi\n",
root.prettyString(),
"Pretty print test"
)
println("Remove: ${curdNode.value}")
root.removeChild(curdNode)
println("Remove: ${gingerTeaNode.value}")
root.removeChild(gingerTeaNode)
assertEquals(
"Root\n" +
"└── Beverages\n" +
" ├── tea\n" +
" │ └── normal tea\n" +
" ├── coffee\n" +
" └── Milk Shake\n",
root.prettyString(),
"Remove node test"
)
}
@Test
fun clearTest() {
val root = TreeNode("Root")
val beveragesNode = TreeNode("Beverages")
val curdNode = TreeNode("Curd")
root.addChild(beveragesNode)
root.addChild(curdNode)
val teaNode = TreeNode("tea")
val coffeeNode = TreeNode("coffee")
val milkShakeNode = TreeNode("Milk Shake")
beveragesNode.addChild(teaNode)
beveragesNode.addChild(coffeeNode)
beveragesNode.addChild(milkShakeNode)
val gingerTeaNode = TreeNode("ginger tea")
val normalTeaNode = TreeNode("normal tea")
teaNode.addChild(gingerTeaNode)
teaNode.addChild(normalTeaNode)
val yogurtNode = TreeNode("yogurt")
val lassiNode = TreeNode("lassi")
curdNode.addChild(yogurtNode)
curdNode.addChild(lassiNode)
println(root.toString())
println(curdNode.height())
root.clear()
assertEquals(root.children, emptyList())
assertEquals(beveragesNode.children, emptyList())
assertEquals(curdNode.children, emptyList())
assertEquals(teaNode.children, emptyList())
assertEquals(coffeeNode.children, emptyList())
assertEquals(milkShakeNode.children, emptyList())
assertEquals(gingerTeaNode.children, emptyList())
assertEquals(normalTeaNode.children, emptyList())
assertEquals(yogurtNode.children, emptyList())
assertEquals(lassiNode.children, emptyList())
assertNull(root.parent)
assertNull(beveragesNode.parent)
assertNull(curdNode.parent)
assertNull(teaNode.parent)
assertNull(coffeeNode.parent)
assertNull(milkShakeNode.parent)
assertNull(gingerTeaNode.parent)
assertNull(normalTeaNode.parent)
assertNull(yogurtNode.parent)
assertNull(lassiNode.parent)
}
@Test
fun kotlinExtTest() {
val root = TreeNode("World")
val northA = TreeNode("North America")
val europe = TreeNode("Europe")
root.addChild(northA)
root.addChild(europe)
val usa = TreeNode("USA")
northA.addChild(usa)
val poland = TreeNode("Poland")
val france = TreeNode("France")
europe.addChild(poland)
europe.addChild(france)
val rootExt = tree("World") {
child("North America") {
child("USA")
}
child("Europe") {
child("Poland")
child("France")
}
}
assertEquals(root.prettyString(), rootExt.prettyString())
}
}