mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-04-19 23:19:03 +02:00
Implemented iterator. Kotlin extensions.
This commit is contained in:
parent
0a04ecd532
commit
0ece34daf2
@ -0,0 +1,21 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
interface ChildDeclarationInterface<T> {
|
||||
|
||||
/**
|
||||
* This method is used to easily create child in node.
|
||||
* ```
|
||||
* val root = treeNode("World") {
|
||||
* treeNode("North America") {
|
||||
* treeNode("USA")
|
||||
* }
|
||||
* treeNode("Europe") {
|
||||
* treeNode("Poland")
|
||||
* treeNode("Germany")
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
@JvmSynthetic
|
||||
fun treeNode(value: T, childDeclaration: ChildDeclaration<T>? = null)
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.github.adriankuta.datastructure.tree;
|
||||
|
||||
public class Temp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreeNode<String> temp = new TreeNode<>("Test");
|
||||
|
||||
}
|
||||
}
|
@ -26,7 +26,8 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
|
||||
_children.add(child)
|
||||
}
|
||||
|
||||
override fun child(value: T, childDeclaration: ChildDeclaration<T>?) {
|
||||
@JvmSynthetic
|
||||
override fun treeNode(value: T, childDeclaration: ChildDeclaration<T>?) {
|
||||
val newChild = TreeNode(value)
|
||||
if(childDeclaration != null)
|
||||
newChild.childDeclaration()
|
||||
|
@ -2,14 +2,16 @@ package com.github.adriankuta.datastructure.tree
|
||||
|
||||
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
|
||||
|
||||
/**
|
||||
* This method can be used to initialize new tree.
|
||||
* ```
|
||||
* val root = treeNode("World") { ... }
|
||||
* ```
|
||||
* @see [ChildDeclarationInterface.treeNode]
|
||||
*/
|
||||
@JvmSynthetic
|
||||
inline fun<reified T> treeNode(value: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> {
|
||||
val treeNode = TreeNode(value)
|
||||
treeNode.childDeclaration()
|
||||
return treeNode
|
||||
}
|
||||
|
||||
interface ChildDeclarationInterface<T> {
|
||||
|
||||
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null)
|
||||
}
|
@ -115,15 +115,14 @@ class TreeNodeTest {
|
||||
@Test
|
||||
fun kotlinExtTest() {
|
||||
val root = treeNode("World") {
|
||||
child("North America") {
|
||||
child("USA")
|
||||
treeNode("North America") {
|
||||
treeNode("USA")
|
||||
}
|
||||
child("Europe") {
|
||||
child("Poland")
|
||||
child("Germany")
|
||||
treeNode("Europe") {
|
||||
treeNode("Poland")
|
||||
treeNode("Germany")
|
||||
}
|
||||
}
|
||||
|
||||
println(root)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user