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
```kotlin
val root = TreeNode<String>("Root")
val beveragesNode = TreeNode<String>("Beverages")
val curdNode = TreeNode<String>("Curd")
root.addChild(beveragesNode)
root.addChild(curdNode)
val root = TreeNode("World")
val northA = TreeNode("North America")
val europe = TreeNode("Europe")
root.addChild(northA)
root.addChild(europe)
val teaNode = TreeNode<String>("tea")
val coffeeNode = TreeNode<String>("coffee")
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 usa = TreeNode("USA")
northA.addChild(usa)
val poland = TreeNode("Poland")
val france = TreeNode("France")
europe.addChild(poland)
europe.addChild(france)
println(root)
System.out.println("Remove: ${curdNode.value}")
root.removeChild(curdNode)
System.out.println("Remove: ${gingerTeaNode.value}")
root.removeChild(gingerTeaNode)
println(root)
```
Or in more Kotlin like style:
```kotlin
val root = treeNode("World") {
treeNode("North America") {
treeNode("USA")
}
treeNode("Europe") {
treeNode("Poland")
treeNode("Germany")
}
}
```
*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

View File

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