Updated README

This commit is contained in:
Adrian Kuta 2020-01-11 00:55:15 +01:00
parent 38c3c268c0
commit 3e5401bd9d
2 changed files with 50 additions and 29 deletions

View File

@ -8,40 +8,46 @@ Simple implementation to store object in tree structure. Method `toString()` is
## Usage ## Usage
```kotlin ```kotlin
val root = TreeNode<String>("Root") val root = TreeNode("World")
val beveragesNode = TreeNode<String>("Beverages") val northA = TreeNode("North America")
val curdNode = TreeNode<String>("Curd") val europe = TreeNode("Europe")
root.addChild(beveragesNode) root.addChild(northA)
root.addChild(curdNode) root.addChild(europe)
val teaNode = TreeNode<String>("tea") val usa = TreeNode("USA")
val coffeeNode = TreeNode<String>("coffee") northA.addChild(usa)
val milkShakeNode = TreeNode<String>("Milk Shake")
beveragesNode.addChild(teaNode)
beveragesNode.addChild(coffeeNode)
beveragesNode.addChild(milkShakeNode)
val gingerTeaNode = TreeNode<String>("ginger tea")
val normalTeaNode = TreeNode<String>("normal tea")
teaNode.addChild(gingerTeaNode)
teaNode.addChild(normalTeaNode)
val yogurtNode = TreeNode<String>("yogurt")
val lassiNode = TreeNode<String>("lassi")
curdNode.addChild(yogurtNode)
curdNode.addChild(lassiNode)
val poland = TreeNode("Poland")
val france = TreeNode("France")
europe.addChild(poland)
europe.addChild(france)
println(root) println(root)
System.out.println("Remove: ${curdNode.value}") ```
root.removeChild(curdNode)
System.out.println("Remove: ${gingerTeaNode.value}") Or in more Kotlin like style:
root.removeChild(gingerTeaNode)
println(root) ```kotlin
val root = treeNode("World") {
treeNode("North America") {
treeNode("USA")
}
treeNode("Europe") {
treeNode("Poland")
treeNode("Germany")
}
}
``` ```
*Output:* *Output:*
<img src="https://github.com/AdrianKuta/Tree-Collection/blob/master/images/console_output.png" width=400> ```
World
├── North America
│ └── USA
└── Europe
├── Poland
└── France
```
## Download ## Download

View File

@ -114,16 +114,31 @@ class TreeNodeTest {
@Test @Test
fun kotlinExtTest() { fun kotlinExtTest() {
val root = treeNode("World") { 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 = treeNode("World") {
treeNode("North America") { treeNode("North America") {
treeNode("USA") treeNode("USA")
} }
treeNode("Europe") { treeNode("Europe") {
treeNode("Poland") treeNode("Poland")
treeNode("Germany") treeNode("France")
} }
} }
println(root) println(root)
assertEquals(root.toString(), rootExt.toString())
} }