Compare commits

...

6 Commits
v3.0 ... master

Author SHA1 Message Date
04c3728fcd
24: Add iterator param to TreeNode's constructor. (#25) 2023-07-24 18:50:46 +02:00
d34050e9af
Merge pull request #23 from AdrianKuta/develop
Relese 3.0.1
2023-02-22 14:32:58 +01:00
2c8381deac Bump version to v3.0.1 2023-02-22 14:30:18 +01:00
4aacbc9dbc
21: Added path function to TreeNode (#22)
Path refers to the sequence of nodes along the edges of a tree.
2023-02-22 14:29:22 +01:00
8a4e677ebf Merge remote-tracking branch 'origin/master' into develop 2023-02-22 12:45:16 +01:00
e34a85ac39 Add javaDoc to release library 2022-12-16 22:22:29 +01:00
12 changed files with 86 additions and 17 deletions

View File

@ -2,7 +2,7 @@ name: Publish Snapshot
on: on:
push: push:
branches: [master, '14-**'] branches: [master]
jobs: jobs:
test: test:

2
.idea/vcs.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="" vcs="Git" />
</component> </component>
</project> </project>

View File

@ -9,7 +9,7 @@ plugins {
val PUBLISH_GROUP_ID = "com.github.adriankuta" val PUBLISH_GROUP_ID = "com.github.adriankuta"
val PUBLISH_ARTIFACT_ID = "tree-structure" val PUBLISH_ARTIFACT_ID = "tree-structure"
val PUBLISH_VERSION = "3.0" val PUBLISH_VERSION = "3.0.2"
val secretFile = File(rootProject.rootDir, "local.properties") val secretFile = File(rootProject.rootDir, "local.properties")
if (secretFile.exists()) { if (secretFile.exists()) {
@ -135,7 +135,11 @@ kotlin {
implementation(kotlin("test")) implementation(kotlin("test"))
} }
} }
val jvmMain by getting val jvmMain by getting {
dependencies {
implementation(kotlin("script-runtime"))
}
}
val jvmTest by getting val jvmTest by getting
val jsMain by getting val jsMain by getting
val jsTest by getting val jsTest by getting

View File

@ -1,11 +1,20 @@
package com.github.adriankuta.datastructure.tree package com.github.adriankuta.datastructure.tree
import com.github.adriankuta.datastructure.tree.TreeNodeIterators.* import com.github.adriankuta.datastructure.tree.exceptions.TreeNodeException
import com.github.adriankuta.datastructure.tree.iterators.LevelOrderTreeIterator
import com.github.adriankuta.datastructure.tree.iterators.PostOrderTreeIterator
import com.github.adriankuta.datastructure.tree.iterators.PreOrderTreeIterator
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators.*
import kotlin.jvm.JvmSynthetic import kotlin.jvm.JvmSynthetic
open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationInterface<T> { /**
* @param treeIterator Choose one of available iterators from [TreeNodeIterators]
*/
open class TreeNode<T>(val value: T, var treeIterator: TreeNodeIterators = PreOrder) : Iterable<TreeNode<T>>, ChildDeclarationInterface<T> {
private var _parent: TreeNode<T>? = null private var _parent: TreeNode<T>? = null
/** /**
* The converse notion of a child, an immediate ancestor. * The converse notion of a child, an immediate ancestor.
*/ */
@ -13,6 +22,7 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
get() = _parent get() = _parent
private val _children = mutableListOf<TreeNode<T>>() private val _children = mutableListOf<TreeNode<T>>()
/** /**
* A group of nodes with the same parent. * A group of nodes with the same parent.
*/ */
@ -20,9 +30,11 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
get() = _children get() = _children
/** /**
* Choose one of available iterators from [TreeNodeIterators] * Checks whether the current tree node is the root of the tree
* @return `true` if the current tree node is root of the tree, `false` otherwise.
*/ */
var defaultIterator: TreeNodeIterators = PreOrder val isRoot: Boolean
get() = _parent == null
/** /**
* Add new child to current node or root. * Add new child to current node or root.
@ -37,7 +49,8 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
@JvmSynthetic @JvmSynthetic
override fun child(value: T, childDeclaration: ChildDeclaration<T>?): TreeNode<T> { override fun child(value: T, childDeclaration: ChildDeclaration<T>?): TreeNode<T> {
val newChild = TreeNode(value) val newChild = TreeNode(value)
if(childDeclaration != null) newChild._parent = this
if (childDeclaration != null)
newChild.childDeclaration() newChild.childDeclaration()
_children.add(newChild) _children.add(newChild)
return newChild return newChild
@ -91,6 +104,31 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
return depth return depth
} }
/**
* Returns the collection of nodes, which connect the current node
* with its descendants
*
* @param descendant the bottom child node for which the path is calculated
* @return collection of nodes, which connect the current node with its descendants
* @throws TreeNodeException exception that may be thrown in case if the
* current node does not have such descendant or if the
* specified tree node is root
*/
@Throws(TreeNodeException::class)
fun path(descendant: TreeNode<T>): List<TreeNode<T>> {
val path = mutableListOf<TreeNode<T>>()
var node = descendant
path.add(node)
while (!node.isRoot) {
node = node.parent!!
path.add(node)
if (node == this)
return path
}
throw TreeNodeException("The specified tree node $descendant is not the descendant of tree node $this")
}
/** /**
* Remove all children from root and every node in tree. * Remove all children from root and every node in tree.
*/ */
@ -126,9 +164,9 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
} }
/** /**
* You can change default iterator by changing [defaultIterator] property. * You can change default iterator by changing [treeIterator] property.
*/ */
override fun iterator(): Iterator<TreeNode<T>> = when (defaultIterator) { override fun iterator(): Iterator<TreeNode<T>> = when (treeIterator) {
PreOrder -> PreOrderTreeIterator(this) PreOrder -> PreOrderTreeIterator(this)
PostOrder -> PostOrderTreeIterator(this) PostOrder -> PostOrderTreeIterator(this)
LevelOrder -> LevelOrderTreeIterator(this) LevelOrder -> LevelOrderTreeIterator(this)

View File

@ -1,5 +1,6 @@
package com.github.adriankuta.datastructure.tree package com.github.adriankuta.datastructure.tree
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators
import kotlin.jvm.JvmSynthetic import kotlin.jvm.JvmSynthetic
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
@ -18,8 +19,7 @@ inline fun <reified T> tree(
defaultIterator: TreeNodeIterators = TreeNodeIterators.PreOrder, defaultIterator: TreeNodeIterators = TreeNodeIterators.PreOrder,
childDeclaration: ChildDeclaration<T> childDeclaration: ChildDeclaration<T>
): TreeNode<T> { ): TreeNode<T> {
val treeNode = TreeNode(root) val treeNode = TreeNode(root, defaultIterator)
treeNode.defaultIterator = defaultIterator
treeNode.childDeclaration() treeNode.childDeclaration()
return treeNode return treeNode
} }

View File

@ -0,0 +1,6 @@
package com.github.adriankuta.datastructure.tree.exceptions
import kotlin.jvm.JvmOverloads
class TreeNodeException @JvmOverloads constructor(message: String? = null, cause: Throwable? = null) :
RuntimeException(message, cause)

View File

@ -1,4 +1,6 @@
package com.github.adriankuta.datastructure.tree package com.github.adriankuta.datastructure.tree.iterators
import com.github.adriankuta.datastructure.tree.TreeNode
/** /**
* Tree is iterated by using `Level-order Traversal Algorithm" * Tree is iterated by using `Level-order Traversal Algorithm"

View File

@ -1,4 +1,6 @@
package com.github.adriankuta.datastructure.tree package com.github.adriankuta.datastructure.tree.iterators
import com.github.adriankuta.datastructure.tree.TreeNode
/** /**
* Tree is iterated by using `Post-order Traversal Algorithm" * Tree is iterated by using `Post-order Traversal Algorithm"

View File

@ -1,4 +1,6 @@
package com.github.adriankuta.datastructure.tree package com.github.adriankuta.datastructure.tree.iterators
import com.github.adriankuta.datastructure.tree.TreeNode
/** /**
* Tree is iterated by using `Pre-order Traversal Algorithm" * Tree is iterated by using `Pre-order Traversal Algorithm"

View File

@ -1,4 +1,4 @@
package com.github.adriankuta.datastructure.tree package com.github.adriankuta.datastructure.tree.iterators
/** /**
* @see PreOrder * @see PreOrder

View File

@ -1,5 +1,6 @@
package com.github.adriankuta.datastructure.tree package com.github.adriankuta.datastructure.tree
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertContentEquals import kotlin.test.assertContentEquals
import kotlin.test.assertEquals import kotlin.test.assertEquals

View File

@ -0,0 +1,14 @@
import com.github.adriankuta.datastructure.tree.tree
val root =
tree("World") {
child("North America") {
child("USA")
}
child("Europe") {
child("Poland")
child("Germany")
}
}
print(root.prettyString())