From 3e5401bd9dfecfaac1d6d86985c882351f65623a Mon Sep 17 00:00:00 2001 From: Adrian Kuta Date: Sat, 11 Jan 2020 00:55:15 +0100 Subject: [PATCH] Updated README --- README.md | 60 ++++++++++--------- .../datastructure/tree/TreeNodeTest.kt | 19 +++++- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index bf2fb0c..3a41995 100644 --- a/README.md +++ b/README.md @@ -8,40 +8,46 @@ Simple implementation to store object in tree structure. Method `toString()` is ## Usage ```kotlin -val root = TreeNode("Root") -val beveragesNode = TreeNode("Beverages") -val curdNode = TreeNode("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("tea") -val coffeeNode = TreeNode("coffee") -val milkShakeNode = TreeNode("Milk Shake") -beveragesNode.addChild(teaNode) -beveragesNode.addChild(coffeeNode) -beveragesNode.addChild(milkShakeNode) - -val gingerTeaNode = TreeNode("ginger tea") -val normalTeaNode = TreeNode("normal tea") -teaNode.addChild(gingerTeaNode) -teaNode.addChild(normalTeaNode) - -val yogurtNode = TreeNode("yogurt") -val lassiNode = TreeNode("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:* - +``` +World +├── North America +│ └── USA +└── Europe + ├── Poland + └── France +``` ## Download diff --git a/treedatastructure/src/test/java/com/github/adriankuta/datastructure/tree/TreeNodeTest.kt b/treedatastructure/src/test/java/com/github/adriankuta/datastructure/tree/TreeNodeTest.kt index 6f63fc6..6ad71b5 100644 --- a/treedatastructure/src/test/java/com/github/adriankuta/datastructure/tree/TreeNodeTest.kt +++ b/treedatastructure/src/test/java/com/github/adriankuta/datastructure/tree/TreeNodeTest.kt @@ -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()) }