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)
|
_children.add(child)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun child(value: T, childDeclaration: ChildDeclaration<T>?) {
|
@JvmSynthetic
|
||||||
|
override fun treeNode(value: T, childDeclaration: ChildDeclaration<T>?) {
|
||||||
val newChild = TreeNode(value)
|
val newChild = TreeNode(value)
|
||||||
if(childDeclaration != null)
|
if(childDeclaration != null)
|
||||||
newChild.childDeclaration()
|
newChild.childDeclaration()
|
||||||
|
@ -2,14 +2,16 @@ package com.github.adriankuta.datastructure.tree
|
|||||||
|
|
||||||
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
|
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method can be used to initialize new tree.
|
||||||
|
* ```
|
||||||
|
* val root = treeNode("World") { ... }
|
||||||
|
* ```
|
||||||
|
* @see [ChildDeclarationInterface.treeNode]
|
||||||
|
*/
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
inline fun<reified T> treeNode(value: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> {
|
inline fun<reified T> treeNode(value: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> {
|
||||||
val treeNode = TreeNode(value)
|
val treeNode = TreeNode(value)
|
||||||
treeNode.childDeclaration()
|
treeNode.childDeclaration()
|
||||||
return treeNode
|
return treeNode
|
||||||
}
|
|
||||||
|
|
||||||
interface ChildDeclarationInterface<T> {
|
|
||||||
|
|
||||||
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null)
|
|
||||||
}
|
}
|
@ -115,15 +115,14 @@ class TreeNodeTest {
|
|||||||
@Test
|
@Test
|
||||||
fun kotlinExtTest() {
|
fun kotlinExtTest() {
|
||||||
val root = treeNode("World") {
|
val root = treeNode("World") {
|
||||||
child("North America") {
|
treeNode("North America") {
|
||||||
child("USA")
|
treeNode("USA")
|
||||||
}
|
}
|
||||||
child("Europe") {
|
treeNode("Europe") {
|
||||||
child("Poland")
|
treeNode("Poland")
|
||||||
child("Germany")
|
treeNode("Germany")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println(root)
|
println(root)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user