11 Commits

Author SHA1 Message Date
72c94f280d Merge pull request #12 from AdrianKuta/release/v.2.1.0
Release 2.1.0
2022-11-24 23:06:48 +01:00
809914274a Update dependencies (#11) 2022-11-24 23:06:09 +01:00
6f16f1cb85 Update README.md 2022-11-23 09:49:48 +01:00
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
15 changed files with 123 additions and 53 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

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

3
.idea/gradle.xml generated
View File

@ -7,7 +7,7 @@
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="11 (2)" />
<option name="gradleJvm" value="Android Studio default JDK" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
@ -15,7 +15,6 @@
<option value="$PROJECT_DIR$/treedatastructure" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings>
</option>
</component>

2
.idea/misc.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

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)
[![License: MIT](https://img.shields.io/github/license/AdrianKuta/Tree-Data-Structure?style=plastic)](https://github.com/AdrianKuta/Tree-Data-Structure/blob/master/LICENSE)
[![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

@ -2,12 +2,12 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 31
buildToolsVersion "31.0.0"
compileSdkVersion 33
buildToolsVersion "33.0.0"
defaultConfig {
applicationId "com.github.adriankuta.treedatastructure"
minSdkVersion 15
targetSdkVersion 31
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@ -18,16 +18,17 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
namespace 'com.github.adriankuta.treedatastructure'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'com.github.adriankuta:tree-structure:1.2.3'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.github.adriankuta:tree-structure:2.0.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.adriankuta.treedatastructure">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"

View File

@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.5.30'
ext.kotlin_version = '1.7.20'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

View File

@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip

View File

@ -6,14 +6,14 @@ afterEvaluate {
}
android {
compileSdkVersion 31
buildToolsVersion "31.0.0"
compileSdkVersion 33
buildToolsVersion "33.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 31
versionName "2.0.2"
targetSdkVersion 33
versionName "2.1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
@ -24,16 +24,17 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
namespace 'com.github.adriankuta.datastructure.tree'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.core:core-ktx:1.9.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}
ext {

View File

@ -1 +1 @@
<manifest package="com.github.adriankuta.datastructure.tree" />
<manifest />

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