mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-04-20 07:29:03 +02:00
Compare commits
No commits in common. "master" and "v3.0" have entirely different histories.
2
.github/workflows/publishSnapshot.yml
vendored
2
.github/workflows/publishSnapshot.yml
vendored
@ -2,7 +2,7 @@ name: Publish Snapshot
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
branches: [master, '14-**']
|
||||
|
||||
jobs:
|
||||
test:
|
||||
|
2
.idea/vcs.xml
generated
2
.idea/vcs.xml
generated
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -9,7 +9,7 @@ plugins {
|
||||
|
||||
val PUBLISH_GROUP_ID = "com.github.adriankuta"
|
||||
val PUBLISH_ARTIFACT_ID = "tree-structure"
|
||||
val PUBLISH_VERSION = "3.0.2"
|
||||
val PUBLISH_VERSION = "3.0"
|
||||
|
||||
val secretFile = File(rootProject.rootDir, "local.properties")
|
||||
if (secretFile.exists()) {
|
||||
@ -135,11 +135,7 @@ kotlin {
|
||||
implementation(kotlin("test"))
|
||||
}
|
||||
}
|
||||
val jvmMain by getting {
|
||||
dependencies {
|
||||
implementation(kotlin("script-runtime"))
|
||||
}
|
||||
}
|
||||
val jvmMain by getting
|
||||
val jvmTest by getting
|
||||
val jsMain by getting
|
||||
val jsTest by getting
|
||||
|
@ -1,6 +1,4 @@
|
||||
package com.github.adriankuta.datastructure.tree.iterators
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.TreeNode
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Level-order Traversal Algorithm"
|
@ -1,6 +1,4 @@
|
||||
package com.github.adriankuta.datastructure.tree.iterators
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.TreeNode
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Post-order Traversal Algorithm"
|
@ -1,6 +1,4 @@
|
||||
package com.github.adriankuta.datastructure.tree.iterators
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.TreeNode
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Pre-order Traversal Algorithm"
|
@ -1,20 +1,11 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
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 com.github.adriankuta.datastructure.tree.TreeNodeIterators.*
|
||||
import kotlin.jvm.JvmSynthetic
|
||||
|
||||
/**
|
||||
* @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> {
|
||||
open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationInterface<T> {
|
||||
|
||||
private var _parent: TreeNode<T>? = null
|
||||
|
||||
/**
|
||||
* The converse notion of a child, an immediate ancestor.
|
||||
*/
|
||||
@ -22,7 +13,6 @@ open class TreeNode<T>(val value: T, var treeIterator: TreeNodeIterators = PreOr
|
||||
get() = _parent
|
||||
|
||||
private val _children = mutableListOf<TreeNode<T>>()
|
||||
|
||||
/**
|
||||
* A group of nodes with the same parent.
|
||||
*/
|
||||
@ -30,11 +20,9 @@ open class TreeNode<T>(val value: T, var treeIterator: TreeNodeIterators = PreOr
|
||||
get() = _children
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Choose one of available iterators from [TreeNodeIterators]
|
||||
*/
|
||||
val isRoot: Boolean
|
||||
get() = _parent == null
|
||||
var defaultIterator: TreeNodeIterators = PreOrder
|
||||
|
||||
/**
|
||||
* Add new child to current node or root.
|
||||
@ -49,8 +37,7 @@ open class TreeNode<T>(val value: T, var treeIterator: TreeNodeIterators = PreOr
|
||||
@JvmSynthetic
|
||||
override fun child(value: T, childDeclaration: ChildDeclaration<T>?): TreeNode<T> {
|
||||
val newChild = TreeNode(value)
|
||||
newChild._parent = this
|
||||
if (childDeclaration != null)
|
||||
if(childDeclaration != null)
|
||||
newChild.childDeclaration()
|
||||
_children.add(newChild)
|
||||
return newChild
|
||||
@ -104,31 +91,6 @@ open class TreeNode<T>(val value: T, var treeIterator: TreeNodeIterators = PreOr
|
||||
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.
|
||||
*/
|
||||
@ -164,9 +126,9 @@ open class TreeNode<T>(val value: T, var treeIterator: TreeNodeIterators = PreOr
|
||||
}
|
||||
|
||||
/**
|
||||
* You can change default iterator by changing [treeIterator] property.
|
||||
* You can change default iterator by changing [defaultIterator] property.
|
||||
*/
|
||||
override fun iterator(): Iterator<TreeNode<T>> = when (treeIterator) {
|
||||
override fun iterator(): Iterator<TreeNode<T>> = when (defaultIterator) {
|
||||
PreOrder -> PreOrderTreeIterator(this)
|
||||
PostOrder -> PostOrderTreeIterator(this)
|
||||
LevelOrder -> LevelOrderTreeIterator(this)
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators
|
||||
import kotlin.jvm.JvmSynthetic
|
||||
|
||||
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
|
||||
@ -19,7 +18,8 @@ inline fun <reified T> tree(
|
||||
defaultIterator: TreeNodeIterators = TreeNodeIterators.PreOrder,
|
||||
childDeclaration: ChildDeclaration<T>
|
||||
): TreeNode<T> {
|
||||
val treeNode = TreeNode(root, defaultIterator)
|
||||
val treeNode = TreeNode(root)
|
||||
treeNode.defaultIterator = defaultIterator
|
||||
treeNode.childDeclaration()
|
||||
return treeNode
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.github.adriankuta.datastructure.tree.iterators
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* @see PreOrder
|
@ -1,6 +0,0 @@
|
||||
package com.github.adriankuta.datastructure.tree.exceptions
|
||||
|
||||
import kotlin.jvm.JvmOverloads
|
||||
|
||||
class TreeNodeException @JvmOverloads constructor(message: String? = null, cause: Throwable? = null) :
|
||||
RuntimeException(message, cause)
|
@ -1,6 +1,5 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
@ -1,14 +0,0 @@
|
||||
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())
|
Loading…
x
Reference in New Issue
Block a user