mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-04-19 23:19:03 +02:00
Updated README
This commit is contained in:
parent
38c3c268c0
commit
3e5401bd9d
60
README.md
60
README.md
@ -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
|
||||||
|
@ -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())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user