mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2025-07-01 15:27:58 +02:00
Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
72c94f280d | |||
809914274a | |||
6f16f1cb85 | |||
cad5b2dd95 | |||
74a6a65434 | |||
b0a6c701ae | |||
1bd47fcb21 | |||
20bd96918c | |||
60805b7187 | |||
12b1df764c | |||
43b8982b88 | |||
7be868648b | |||
1702d8dfb5 | |||
8ac2901e23 | |||
e47c8593da | |||
060f557752 |
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
|
6
.github/workflows/publish.yml
vendored
6
.github/workflows/publish.yml
vendored
@ -14,9 +14,9 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
- name: Set up JDK 11
|
- name: Set up JDK 11
|
||||||
uses: actions/setup-java@v2
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
distribution: adopt
|
distribution: adopt
|
||||||
java-version: 11
|
java-version: 11
|
||||||
|
|
||||||
# Builds the release artifacts of the library
|
# Builds the release artifacts of the library
|
||||||
- name: Release build
|
- name: Release build
|
||||||
|
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
|
3
.idea/gradle.xml
generated
3
.idea/gradle.xml
generated
@ -7,7 +7,7 @@
|
|||||||
<option name="testRunner" value="GRADLE" />
|
<option name="testRunner" value="GRADLE" />
|
||||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
<option name="gradleJvm" value="11 (2)" />
|
<option name="gradleJvm" value="Android Studio default JDK" />
|
||||||
<option name="modules">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
@ -15,7 +15,6 @@
|
|||||||
<option value="$PROJECT_DIR$/treedatastructure" />
|
<option value="$PROJECT_DIR$/treedatastructure" />
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
<option name="resolveModulePerSourceSet" value="false" />
|
|
||||||
</GradleProjectSettings>
|
</GradleProjectSettings>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<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" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# Tree (Data Structure)
|
# Tree (Data Structure)
|
||||||
[](https://mvnrepository.com/artifact/com.github.adriankuta/tree-structure)
|
[](https://mvnrepository.com/artifact/com.github.adriankuta/tree-structure)
|
||||||
[](https://github.com/AdrianKuta/Design-Patterns-Kotlin/blob/master/LICENSE)
|
[](https://github.com/AdrianKuta/Tree-Data-Structure/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.
|
Simple implementation to store object in tree structure.
|
||||||
|
|
||||||
|
@ -2,12 +2,12 @@ apply plugin: 'com.android.application'
|
|||||||
apply plugin: 'kotlin-android'
|
apply plugin: 'kotlin-android'
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 31
|
compileSdkVersion 33
|
||||||
buildToolsVersion "31.0.0"
|
buildToolsVersion "33.0.0"
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.github.adriankuta.treedatastructure"
|
applicationId "com.github.adriankuta.treedatastructure"
|
||||||
minSdkVersion 15
|
minSdkVersion 15
|
||||||
targetSdkVersion 31
|
targetSdkVersion 33
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0"
|
versionName "1.0"
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
@ -18,16 +18,17 @@ android {
|
|||||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
namespace 'com.github.adriankuta.treedatastructure'
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||||
implementation 'androidx.core:core-ktx:1.6.0'
|
implementation 'androidx.core:core-ktx:1.9.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||||
implementation 'com.github.adriankuta:tree-structure:1.2.3'
|
implementation 'com.github.adriankuta:tree-structure:2.0.3'
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
package="com.github.adriankuta.treedatastructure">
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
ext.kotlin_version = '1.5.30'
|
ext.kotlin_version = '1.7.20'
|
||||||
repositories {
|
repositories {
|
||||||
google()
|
google()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
dependencies {
|
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"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
// in the individual module build.gradle files
|
// in the individual module build.gradle files
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
|||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
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
|
||||||
|
@ -6,14 +6,14 @@ afterEvaluate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 31
|
compileSdkVersion 33
|
||||||
buildToolsVersion "31.0.0"
|
buildToolsVersion "33.0.0"
|
||||||
|
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 15
|
minSdkVersion 15
|
||||||
targetSdkVersion 31
|
targetSdkVersion 33
|
||||||
versionName "2.0.0"
|
versionName "2.1.0"
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
consumerProguardFiles 'consumer-rules.pro'
|
consumerProguardFiles 'consumer-rules.pro'
|
||||||
}
|
}
|
||||||
@ -24,16 +24,17 @@ android {
|
|||||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
namespace 'com.github.adriankuta.datastructure.tree'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||||
implementation 'androidx.core:core-ktx:1.6.0'
|
implementation 'androidx.core:core-ktx:1.9.0'
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
ext {
|
ext {
|
||||||
|
@ -1 +1 @@
|
|||||||
<manifest package="com.github.adriankuta.datastructure.tree" />
|
<manifest />
|
||||||
|
@ -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.
|
* @return `true` if the node has been successfully removed; `false` if it was not present in the tree.
|
||||||
*/
|
*/
|
||||||
fun removeChild(child: TreeNode<T>): Boolean {
|
fun removeChild(child: TreeNode<T>): Boolean {
|
||||||
|
println(child.value)
|
||||||
val removed = child._parent?._children?.remove(child)
|
val removed = child._parent?._children?.remove(child)
|
||||||
child._parent = null
|
child._parent = null
|
||||||
return removed ?: false
|
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
|
* 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" +
|
"│ └── Milk Shake\n" +
|
||||||
"└── Curd\n" +
|
"└── Curd\n" +
|
||||||
" ├── yogurt\n" +
|
" ├── yogurt\n" +
|
||||||
" └── lassi\n", root.toString()
|
" └── lassi\n",
|
||||||
|
root.prettyString()
|
||||||
)
|
)
|
||||||
|
|
||||||
println("Remove: ${curdNode.value}")
|
println("Remove: ${curdNode.value}")
|
||||||
@ -56,7 +57,8 @@ class TreeNodeTest {
|
|||||||
" ├── tea\n" +
|
" ├── tea\n" +
|
||||||
" │ └── normal tea\n" +
|
" │ └── normal tea\n" +
|
||||||
" ├── coffee\n" +
|
" ├── coffee\n" +
|
||||||
" └── Milk Shake\n", root.toString()
|
" └── Milk Shake\n",
|
||||||
|
root.prettyString()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user