mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-07-01 23:27:59 +02:00
Set the correct file structure to maintain backward compatibility. (#18)
This commit is contained in:
@ -0,0 +1,24 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
import kotlin.jvm.JvmSynthetic
|
||||
|
||||
interface ChildDeclarationInterface<T> {
|
||||
|
||||
/**
|
||||
* This method is used to easily create child in node.
|
||||
* ```
|
||||
* val root = tree("World") {
|
||||
* child("North America") {
|
||||
* child("USA")
|
||||
* }
|
||||
* child("Europe") {
|
||||
* child("Poland")
|
||||
* child("Germany")
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* @return New created TreeNode.
|
||||
*/
|
||||
@JvmSynthetic
|
||||
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null): TreeNode<T>
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Level-order Traversal Algorithm"
|
||||
* In level-order traversal we iterating nodes level by level,
|
||||
* starting from root, and going deeper and deeper in tree.
|
||||
* ```
|
||||
* E.g.
|
||||
* 1
|
||||
* / | \
|
||||
* / | \
|
||||
* 2 3 4
|
||||
* / \ / | \
|
||||
* 5 6 7 8 9
|
||||
* / / | \
|
||||
* 10 11 12 13
|
||||
*
|
||||
* Output: 1 2 3 4 5 6 7 8 9 10 11 12 13
|
||||
* ```
|
||||
*/
|
||||
class LevelOrderTreeIterator<T>(root: TreeNode<T>) : Iterator<TreeNode<T>> {
|
||||
|
||||
private val stack = ArrayDeque<TreeNode<T>>()
|
||||
|
||||
init {
|
||||
stack.addLast(root)
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean = stack.isNotEmpty()
|
||||
|
||||
override fun next(): TreeNode<T> {
|
||||
val node = stack.removeFirst()
|
||||
node.children
|
||||
.forEach { stack.addLast(it) }
|
||||
return node
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Post-order Traversal Algorithm"
|
||||
* In post-order traversal, we starting from most left child.
|
||||
* First visit all children of parent, then parent.
|
||||
* ```
|
||||
* E.g.
|
||||
* 1
|
||||
* / | \
|
||||
* / | \
|
||||
* 2 3 4
|
||||
* / \ / | \
|
||||
* 5 6 7 8 9
|
||||
* / / | \
|
||||
* 10 11 12 13
|
||||
*
|
||||
* Output: 10 5 11 12 13 6 2 3 7 8 9 4 1
|
||||
* ```
|
||||
*/
|
||||
class PostOrderTreeIterator<T>(root: TreeNode<T>) : Iterator<TreeNode<T>> {
|
||||
|
||||
private val stack = ArrayDeque<TreeNode<T>>()
|
||||
|
||||
init {
|
||||
stack.addAll(getChildrenStack(root))
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean = stack.isNotEmpty()
|
||||
|
||||
override fun next(): TreeNode<T> {
|
||||
return stack.removeFirst()
|
||||
}
|
||||
|
||||
private fun getChildrenStack(node: TreeNode<T>): ArrayDeque<TreeNode<T>> {
|
||||
val stack = ArrayDeque<TreeNode<T>>()
|
||||
if(node.children.isEmpty()) {
|
||||
return ArrayDeque(listOf(node))
|
||||
}
|
||||
node.children.forEach {
|
||||
stack.addAll(getChildrenStack(it))
|
||||
}
|
||||
stack.addLast(node)
|
||||
return stack
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Pre-order Traversal Algorithm"
|
||||
* The pre-order traversal is a topologically sorted one,
|
||||
* because a parent node is processed before any of its child nodes is done.
|
||||
* ```
|
||||
* E.g.
|
||||
* 1
|
||||
* / | \
|
||||
* / | \
|
||||
* 2 3 4
|
||||
* / \ / | \
|
||||
* 5 6 7 8 9
|
||||
* / / | \
|
||||
* 10 11 12 13
|
||||
*
|
||||
* Output: 1 2 5 10 6 11 12 13 3 4 7 8 9
|
||||
* ```
|
||||
*/
|
||||
class PreOrderTreeIterator<T>(root: TreeNode<T>) : Iterator<TreeNode<T>> {
|
||||
|
||||
private val stack = ArrayDeque<TreeNode<T>>()
|
||||
|
||||
init {
|
||||
stack.addLast(root)
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean = stack.isNotEmpty()
|
||||
|
||||
override fun next(): TreeNode<T> {
|
||||
val node = stack.removeLast()
|
||||
node.children
|
||||
.asReversed()
|
||||
.forEach { stack.addLast(it) }
|
||||
return node
|
||||
}
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.TreeNodeIterators.*
|
||||
import kotlin.jvm.JvmSynthetic
|
||||
|
||||
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.
|
||||
*/
|
||||
val parent: TreeNode<T>?
|
||||
get() = _parent
|
||||
|
||||
private val _children = mutableListOf<TreeNode<T>>()
|
||||
/**
|
||||
* A group of nodes with the same parent.
|
||||
*/
|
||||
val children: List<TreeNode<T>>
|
||||
get() = _children
|
||||
|
||||
/**
|
||||
* Choose one of available iterators from [TreeNodeIterators]
|
||||
*/
|
||||
var defaultIterator: TreeNodeIterators = PreOrder
|
||||
|
||||
/**
|
||||
* Add new child to current node or root.
|
||||
*
|
||||
* @param child A node which will be directly connected to current node.
|
||||
*/
|
||||
fun addChild(child: TreeNode<T>) {
|
||||
child._parent = this
|
||||
_children.add(child)
|
||||
}
|
||||
|
||||
@JvmSynthetic
|
||||
override fun child(value: T, childDeclaration: ChildDeclaration<T>?): TreeNode<T> {
|
||||
val newChild = TreeNode(value)
|
||||
if(childDeclaration != null)
|
||||
newChild.childDeclaration()
|
||||
_children.add(newChild)
|
||||
return newChild
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a single instance of the specified node from this tree, if it is present.
|
||||
*
|
||||
* @return `true` if the node has been successfully removed; `false` if it was not present in the tree.
|
||||
*/
|
||||
fun removeChild(child: TreeNode<T>): Boolean {
|
||||
println(child.value)
|
||||
val removed = child._parent?._children?.remove(child)
|
||||
child._parent = null
|
||||
return removed ?: false
|
||||
}
|
||||
|
||||
/**
|
||||
* This function go through tree and counts children. Root element is not counted.
|
||||
* @return All child and nested child count.
|
||||
*/
|
||||
fun nodeCount(): Int {
|
||||
if (_children.isEmpty())
|
||||
return 0
|
||||
return _children.size +
|
||||
_children.sumOf { it.nodeCount() }
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The number of edges on the longest path between current node and a descendant leaf.
|
||||
*/
|
||||
fun height(): Int {
|
||||
val childrenMaxDepth = _children.map { it.height() }
|
||||
.maxOrNull()
|
||||
?: -1 // -1 because this method counts nodes, and edges are always one less then nodes.
|
||||
return childrenMaxDepth + 1
|
||||
}
|
||||
|
||||
/**
|
||||
* Distance is the number of edges along the shortest path between two nodes.
|
||||
* @return The distance between current node and the root.
|
||||
*/
|
||||
fun depth(): Int {
|
||||
var depth = 0
|
||||
var tempParent = parent
|
||||
|
||||
while (tempParent != null) {
|
||||
depth++
|
||||
tempParent = tempParent.parent
|
||||
}
|
||||
return depth
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all children from root and every node in tree.
|
||||
*/
|
||||
fun clear() {
|
||||
_parent = null
|
||||
_children.forEach { it.clear() }
|
||||
_children.clear()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
fun prettyString(): String {
|
||||
val stringBuilder = StringBuilder()
|
||||
print(stringBuilder, "", "")
|
||||
return stringBuilder.toString()
|
||||
}
|
||||
|
||||
private fun print(stringBuilder: StringBuilder, prefix: String, childrenPrefix: String) {
|
||||
stringBuilder.append(prefix)
|
||||
stringBuilder.append(value)
|
||||
stringBuilder.append('\n')
|
||||
val childIterator = _children.iterator()
|
||||
while (childIterator.hasNext()) {
|
||||
val node = childIterator.next()
|
||||
if (childIterator.hasNext()) {
|
||||
node.print(stringBuilder, "$childrenPrefix├── ", "$childrenPrefix│ ")
|
||||
} else {
|
||||
node.print(stringBuilder, "$childrenPrefix└── ", "$childrenPrefix ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* You can change default iterator by changing [defaultIterator] property.
|
||||
*/
|
||||
override fun iterator(): Iterator<TreeNode<T>> = when (defaultIterator) {
|
||||
PreOrder -> PreOrderTreeIterator(this)
|
||||
PostOrder -> PostOrderTreeIterator(this)
|
||||
LevelOrder -> LevelOrderTreeIterator(this)
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
import kotlin.jvm.JvmSynthetic
|
||||
|
||||
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
|
||||
|
||||
/**
|
||||
* This method can be used to initialize new tree.
|
||||
* ```
|
||||
* val root = tree("World") { ... }
|
||||
* ```
|
||||
* @param root Root element of new tree.
|
||||
* @see [ChildDeclarationInterface.child]
|
||||
*/
|
||||
@JvmSynthetic
|
||||
inline fun <reified T> tree(
|
||||
root: T,
|
||||
defaultIterator: TreeNodeIterators = TreeNodeIterators.PreOrder,
|
||||
childDeclaration: ChildDeclaration<T>
|
||||
): TreeNode<T> {
|
||||
val treeNode = TreeNode(root)
|
||||
treeNode.defaultIterator = defaultIterator
|
||||
treeNode.childDeclaration()
|
||||
return treeNode
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
/**
|
||||
* @see PreOrder
|
||||
* @see PostOrder
|
||||
* @see LevelOrder
|
||||
*/
|
||||
enum class TreeNodeIterators {
|
||||
/**
|
||||
* Tree is iterated by using `Pre-order Traversal Algorithm"
|
||||
* The pre-order traversal is a topologically sorted one,
|
||||
* because a parent node is processed before any of its child nodes is done.
|
||||
* ```
|
||||
* E.g.
|
||||
* 1
|
||||
* / | \
|
||||
* / | \
|
||||
* 2 3 4
|
||||
* / \ / | \
|
||||
* 5 6 7 8 9
|
||||
* / / | \
|
||||
* 10 11 12 13
|
||||
*
|
||||
* Output: 1 2 5 10 6 11 12 13 3 4 7 8 9
|
||||
* ```
|
||||
*/
|
||||
PreOrder,
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Post-order Traversal Algorithm"
|
||||
* In post-order traversal, we starting from most left child.
|
||||
* First visit all children of parent, then parent.
|
||||
* ```
|
||||
* E.g.
|
||||
* 1
|
||||
* / | \
|
||||
* / | \
|
||||
* 2 3 4
|
||||
* / \ / | \
|
||||
* 5 6 7 8 9
|
||||
* / / | \
|
||||
* 10 11 12 13
|
||||
*
|
||||
* Output: 10 5 11 12 13 6 2 3 7 8 9 4 1
|
||||
* ```
|
||||
*/
|
||||
PostOrder,
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Level-order Traversal Algorithm"
|
||||
* In level-order traversal we iterating nodes level by level,
|
||||
* starting from root, and going deeper and deeper in tree.
|
||||
* ```
|
||||
* E.g.
|
||||
* 1
|
||||
* / | \
|
||||
* / | \
|
||||
* 2 3 4
|
||||
* / \ / | \
|
||||
* 5 6 7 8 9
|
||||
* / / | \
|
||||
* 10 11 12 13
|
||||
*
|
||||
* Output: 1 2 3 4 5 6 7 8 9 10 11 12 13
|
||||
* ```
|
||||
*/
|
||||
LevelOrder
|
||||
}
|
Reference in New Issue
Block a user