mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-07-01 15:27:58 +02:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
c2ebd5dfe4 | |||
c320411a40 | |||
3c3d418aa2 | |||
f00ab975dc | |||
eb2d5f07be |
@ -22,7 +22,7 @@ val poland = TreeNode("Poland")
|
||||
val france = TreeNode("France")
|
||||
europe.addChild(poland)
|
||||
europe.addChild(france)
|
||||
println(root)
|
||||
println(root.prettyString())
|
||||
```
|
||||
|
||||
**Pretty Kotlin**
|
||||
@ -55,7 +55,7 @@ TreeNode<String> poland = new TreeNode<>("Poland");
|
||||
TreeNode<String> france = new TreeNode<>("France");
|
||||
europe.addChild(poland);
|
||||
europe.addChild(france);
|
||||
System.out.println(root);
|
||||
System.out.println(root.prettyString());
|
||||
```
|
||||
|
||||
*Output:*
|
||||
|
@ -15,7 +15,7 @@ android {
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "1.2.0"
|
||||
versionName "1.2.3"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles 'consumer-rules.pro'
|
||||
|
@ -15,7 +15,8 @@ interface ChildDeclarationInterface<T> {
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* @return New created TreeNode.
|
||||
*/
|
||||
@JvmSynthetic
|
||||
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null)
|
||||
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null): TreeNode<T>
|
||||
}
|
@ -27,11 +27,12 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
|
||||
}
|
||||
|
||||
@JvmSynthetic
|
||||
override fun child(child: T, childDeclaration: ChildDeclaration<T>?) {
|
||||
override fun child(value: T, childDeclaration: ChildDeclaration<T>?): TreeNode<T> {
|
||||
val newChild = TreeNode(value)
|
||||
if(childDeclaration != null)
|
||||
newChild.childDeclaration()
|
||||
_children.add(newChild)
|
||||
return newChild
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,6 +92,10 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
fun prettyString(): String {
|
||||
val stringBuilder = StringBuilder()
|
||||
print(stringBuilder, "", "")
|
||||
return stringBuilder.toString()
|
||||
|
@ -128,18 +128,15 @@ class TreeNodeTest {
|
||||
europe.addChild(poland)
|
||||
europe.addChild(france)
|
||||
|
||||
val rootExt = treeNode("World") {
|
||||
treeNode("North America") {
|
||||
treeNode("USA")
|
||||
val rootExt = tree("World") {
|
||||
child("North America") {
|
||||
child("USA")
|
||||
}
|
||||
treeNode("Europe") {
|
||||
treeNode("Poland")
|
||||
treeNode("France")
|
||||
child("Europe") {
|
||||
child("Poland")
|
||||
child("France")
|
||||
}
|
||||
}
|
||||
println(root)
|
||||
assertEquals(root.toString(), rootExt.toString())
|
||||
assertEquals(root.prettyString(), rootExt.prettyString())
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user