mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-07-01 15:27:58 +02:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
cad5b2dd95 | |||
74a6a65434 | |||
b0a6c701ae | |||
1bd47fcb21 | |||
20bd96918c | |||
60805b7187 | |||
12b1df764c | |||
43b8982b88 | |||
7be868648b | |||
1702d8dfb5 | |||
8ac2901e23 | |||
e47c8593da |
24
.github/workflows/build.yml
vendored
Normal file
24
.github/workflows/build.yml
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
name: Build
|
||||
|
||||
on: pull_request
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Check project build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
- name: Set up JDK 11
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
distribution: adopt
|
||||
java-version: 11
|
||||
|
||||
# Builds the release artifacts of the library
|
||||
- name: Build
|
||||
run: ./gradlew assemble
|
||||
|
||||
# Generates other artifacts (javadocJar is optional)
|
||||
- name: Source jar and dokka
|
||||
run: ./gradlew androidSourcesJar javadocJar
|
23
.github/workflows/test.yml
vendored
Normal file
23
.github/workflows/test.yml
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Run unit tests
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
- name: Set up JDK 11
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
distribution: adopt
|
||||
java-version: 11
|
||||
|
||||
# Builds the release artifacts of the library
|
||||
- name: Test
|
||||
run: ./gradlew test
|
@ -1,7 +1,7 @@
|
||||
# Tree (Data Structure)
|
||||
[](https://mvnrepository.com/artifact/com.github.adriankuta/tree-structure)
|
||||
[](https://github.com/AdrianKuta/Design-Patterns-Kotlin/blob/master/LICENSE)
|
||||
[](https://circleci.com/gh/AdrianKuta/Tree-Data-Structure)
|
||||
[](https://github.com/AdrianKuta/Tree-Data-Structure/actions/workflows/publish.yml)
|
||||
|
||||
Simple implementation to store object in tree structure.
|
||||
|
||||
|
@ -13,7 +13,7 @@ android {
|
||||
defaultConfig {
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 31
|
||||
versionName "2.0.0"
|
||||
versionName "2.0.3"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles 'consumer-rules.pro'
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Tree is iterated by using `Pre-order Traversal Algorithm"
|
||||
* 1. Check if the current node is empty or null.
|
||||
* 2. Display the data part of the root (or current node).
|
||||
* 3. Traverse the left subtree by recursively calling the pre-order function.
|
||||
* 4. Traverse the right subtree by recursively calling the pre-order function.
|
||||
* ```
|
||||
* 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 = Stack<TreeNode<T>>()
|
||||
|
||||
init {
|
||||
stack.push(root)
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean = !stack.empty()
|
||||
|
||||
override fun next(): TreeNode<T> {
|
||||
val node = stack.pop()
|
||||
node.children
|
||||
.asReversed()
|
||||
.forEach { stack.push(it) }
|
||||
return node
|
||||
}
|
||||
}
|
@ -41,6 +41,7 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
|
||||
* @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
|
||||
@ -136,5 +137,5 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
|
||||
* Output: 1 2 5 10 6 11 12 13 3 4 7 8 9
|
||||
* ```
|
||||
*/
|
||||
override fun iterator(): Iterator<TreeNode<T>> = TreeNodeIterator(this)
|
||||
override fun iterator(): Iterator<TreeNode<T>> = PreOrderTreeIterator(this)
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package com.github.adriankuta.datastructure.tree
|
||||
|
||||
import java.util.*
|
||||
|
||||
class TreeNodeIterator<T>(root: TreeNode<T>) : Iterator<TreeNode<T>> {
|
||||
|
||||
private val stack = Stack<TreeNode<T>>()
|
||||
|
||||
init {
|
||||
stack.push(root)
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean = !stack.empty()
|
||||
|
||||
override fun next(): TreeNode<T> {
|
||||
val node = stack.pop()
|
||||
node.children
|
||||
.asReversed()
|
||||
.forEach { stack.push(it) }
|
||||
return node
|
||||
}
|
||||
}
|
@ -43,7 +43,8 @@ class TreeNodeTest {
|
||||
"│ └── Milk Shake\n" +
|
||||
"└── Curd\n" +
|
||||
" ├── yogurt\n" +
|
||||
" └── lassi\n", root.toString()
|
||||
" └── lassi\n",
|
||||
root.prettyString()
|
||||
)
|
||||
|
||||
println("Remove: ${curdNode.value}")
|
||||
@ -56,7 +57,8 @@ class TreeNodeTest {
|
||||
" ├── tea\n" +
|
||||
" │ └── normal tea\n" +
|
||||
" ├── coffee\n" +
|
||||
" └── Milk Shake\n", root.toString()
|
||||
" └── Milk Shake\n",
|
||||
root.prettyString()
|
||||
)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user