mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-04-19 23:19:03 +02:00
Renamed functions.
This commit is contained in:
parent
8dbbd3b2f8
commit
3f166aced0
34
README.md
34
README.md
@ -7,6 +7,7 @@ Simple implementation to store object in tree structure. Method `toString()` is
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
**Kotlin**
|
||||||
```kotlin
|
```kotlin
|
||||||
val root = TreeNode("World")
|
val root = TreeNode("World")
|
||||||
val northA = TreeNode("North America")
|
val northA = TreeNode("North America")
|
||||||
@ -24,20 +25,39 @@ europe.addChild(france)
|
|||||||
println(root)
|
println(root)
|
||||||
```
|
```
|
||||||
|
|
||||||
Or in more Kotlin like style:
|
**Pretty Kotlin**
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
val root = treeNode("World") {
|
val root =
|
||||||
treeNode("North America") {
|
tree("World") {
|
||||||
treeNode("USA")
|
child("North America") {
|
||||||
|
child("USA")
|
||||||
}
|
}
|
||||||
treeNode("Europe") {
|
child("Europe") {
|
||||||
treeNode("Poland")
|
child("Poland")
|
||||||
treeNode("Germany")
|
child("Germany")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Java**
|
||||||
|
```java
|
||||||
|
TreeNode<String> root = new TreeNode<>("World");
|
||||||
|
TreeNode<String> northA = new TreeNode<>("North America");
|
||||||
|
TreeNode<String> europe = new TreeNode<>("Europe");
|
||||||
|
root.addChild(northA);
|
||||||
|
root.addChild(europe);
|
||||||
|
|
||||||
|
TreeNode<String> usa = new TreeNode<>("USA");
|
||||||
|
northA.addChild(usa);
|
||||||
|
|
||||||
|
TreeNode<String> poland = new TreeNode<>("Poland");
|
||||||
|
TreeNode<String> france = new TreeNode<>("France");
|
||||||
|
europe.addChild(poland);
|
||||||
|
europe.addChild(france);
|
||||||
|
System.out.println(root);
|
||||||
|
```
|
||||||
|
|
||||||
*Output:*
|
*Output:*
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -15,7 +15,7 @@ android {
|
|||||||
minSdkVersion 15
|
minSdkVersion 15
|
||||||
targetSdkVersion 29
|
targetSdkVersion 29
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.1.0"
|
versionName "1.2.0"
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
consumerProguardFiles 'consumer-rules.pro'
|
consumerProguardFiles 'consumer-rules.pro'
|
||||||
|
@ -5,17 +5,17 @@ interface ChildDeclarationInterface<T> {
|
|||||||
/**
|
/**
|
||||||
* This method is used to easily create child in node.
|
* This method is used to easily create child in node.
|
||||||
* ```
|
* ```
|
||||||
* val root = treeNode("World") {
|
* val root = tree("World") {
|
||||||
* treeNode("North America") {
|
* child("North America") {
|
||||||
* treeNode("USA")
|
* child("USA")
|
||||||
* }
|
* }
|
||||||
* treeNode("Europe") {
|
* child("Europe") {
|
||||||
* treeNode("Poland")
|
* child("Poland")
|
||||||
* treeNode("Germany")
|
* child("Germany")
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
fun treeNode(value: T, childDeclaration: ChildDeclaration<T>? = null)
|
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null)
|
||||||
}
|
}
|
@ -27,7 +27,7 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
override fun treeNode(value: T, childDeclaration: ChildDeclaration<T>?) {
|
override fun child(child: T, childDeclaration: ChildDeclaration<T>?) {
|
||||||
val newChild = TreeNode(value)
|
val newChild = TreeNode(value)
|
||||||
if(childDeclaration != null)
|
if(childDeclaration != null)
|
||||||
newChild.childDeclaration()
|
newChild.childDeclaration()
|
||||||
|
@ -5,13 +5,14 @@ typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
|
|||||||
/**
|
/**
|
||||||
* This method can be used to initialize new tree.
|
* This method can be used to initialize new tree.
|
||||||
* ```
|
* ```
|
||||||
* val root = treeNode("World") { ... }
|
* val root = tree("World") { ... }
|
||||||
* ```
|
* ```
|
||||||
* @see [ChildDeclarationInterface.treeNode]
|
* @param root Root element of new tree.
|
||||||
|
* @see [ChildDeclarationInterface.child]
|
||||||
*/
|
*/
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
inline fun<reified T> treeNode(value: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> {
|
inline fun<reified T> tree(root: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> {
|
||||||
val treeNode = TreeNode(value)
|
val treeNode = TreeNode(root)
|
||||||
treeNode.childDeclaration()
|
treeNode.childDeclaration()
|
||||||
return treeNode
|
return treeNode
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user