13 Commits

Author SHA1 Message Date
cad5b2dd95 Iterator class refactor 2021-09-03 13:35:18 +02:00
74a6a65434 Merge pull request #9 from AdrianKuta/pr_test
Set up GitHub Actions
2021-09-03 13:23:25 +02:00
b0a6c701ae Update README.md 2021-09-03 13:23:15 +02:00
1bd47fcb21 Set up GitHub Actions 2021-09-03 13:19:09 +02:00
20bd96918c Set up GitHub Actions 2021-09-03 13:17:27 +02:00
60805b7187 Set up GitHub Actions 2021-09-03 13:14:25 +02:00
12b1df764c Set up GitHub Actions 2021-09-03 13:12:09 +02:00
43b8982b88 Update README.md 2021-09-03 13:02:32 +02:00
7be868648b Set up GitHub Actions 2021-09-03 12:45:31 +02:00
1702d8dfb5 Set up GitHub Actions 2021-09-03 12:39:38 +02:00
8ac2901e23 Set up GitHub Actions 2021-09-03 12:37:28 +02:00
e47c8593da Set up GitHub Actions 2021-09-03 12:31:31 +02:00
060f557752 fixed github actions 2021-09-03 12:16:19 +02:00
9 changed files with 100 additions and 30 deletions

24
.github/workflows/build.yml vendored Normal file
View 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

View File

@ -14,9 +14,9 @@ jobs:
uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
distribution: adopt
java-version: 11
with:
distribution: adopt
java-version: 11
# Builds the release artifacts of the library
- name: Release build

23
.github/workflows/test.yml vendored Normal file
View 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

View File

@ -1,7 +1,7 @@
# Tree (Data Structure)
[![maven](https://img.shields.io/maven-central/v/com.github.adriankuta/tree-structure?style=plastic)](https://mvnrepository.com/artifact/com.github.adriankuta/tree-structure)
[![License: MIT](https://img.shields.io/github/license/AdrianKuta/Tree-Data-Structure?style=plastic)](https://github.com/AdrianKuta/Design-Patterns-Kotlin/blob/master/LICENSE)
[![CircleCI](https://img.shields.io/circleci/build/github/AdrianKuta/Tree-Data-Structure/master?label=CircleCI&style=plastic&logo=circleci)](https://circleci.com/gh/AdrianKuta/Tree-Data-Structure)
[![Publish](https://github.com/AdrianKuta/Tree-Data-Structure/actions/workflows/publish.yml/badge.svg)](https://github.com/AdrianKuta/Tree-Data-Structure/actions/workflows/publish.yml)
Simple implementation to store object in tree structure.

View File

@ -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'
}

View File

@ -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
}
}

View File

@ -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)
}

View File

@ -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
}
}

View File

@ -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()
)
}