Skip to content

Commit 2b6eb36

Browse files
committed
feat: add comments to code to trees package
1 parent 934d808 commit 2b6eb36

File tree

12 files changed

+142
-59
lines changed

12 files changed

+142
-59
lines changed

app/src/main/kotlin/composeApp/Controller.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55

66
import androidx.compose.ui.awt.ComposeWindow
77
import com.google.gson.Gson
8-
import repo.JsonRepository
9-
import repo.serialization.SerializableValue
10-
import repo.serialization.strategies.AVLStrategy
8+
import repository.JsonRepository
9+
import repository.serialization.SerializableValue
10+
import repository.serialization.strategies.AVLStrategy
1111
import java.awt.FileDialog
1212
import java.io.File
1313

14-
/*
15-
* Copyright (c) 2023 teemEight
16-
* SPDX-License-Identifier: Apache-2.0
17-
*/
18-
1914
class Controller {
2015
fun openFileDialog(
2116
window: ComposeWindow, title: String, allowedExtensions: List<String>, allowMultiSelection: Boolean = true
@@ -40,7 +35,7 @@ class Controller {
4035
return gson.fromJson(string.value, DrawableNode::class.java)!!
4136
}
4237

43-
val repo = JsonRepository(AVLStrategy(::drawableNodeToString, ::jsonStringToDrawableNode), dirPath, filename)
38+
val repo = JsonRepository(AVLStrategy(::drawableNodeToString, ::jsonStringToDrawableNode), dirPath)
4439
return repo.getNames()
4540
}
4641

app/src/main/kotlin/composeApp/DrawableNode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import app.trees.KeyValue
6+
import trees.KeyValue
77

88
class DrawableNode(
99
var data: KeyValue<Int, String>,

app/src/main/kotlin/composeApp/MainActivity.kt

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import androidx.compose.runtime.Composable
77
import androidx.compose.runtime.mutableStateOf
88
import androidx.compose.runtime.remember
99
import androidx.compose.ui.awt.ComposeWindow
10-
import composeApp.OpenTree
1110
import java.io.File
1211

1312
/*
@@ -48,44 +47,44 @@ fun run(window: ComposeWindow) {
4847

4948
val listNames = remember { mutableStateOf(listOf("test", "main")) }
5049
val nameState = remember { mutableStateOf("") }
51-
when (state.value) {
52-
States.OPENING_TREE -> OpenTree(
53-
listOfDatabase = listOfDatabase,
54-
onTypeOfDatabaseChanged = { newType ->
55-
stringTypeOfDatabaseState.value = newType
56-
typeOfDatabaseState.value = databases.getOrDefault(stringTypeOfDatabaseState.value, TypeOfDatabase.Json)
57-
},
58-
typeOfDatabaseState = typeOfDatabaseState,
59-
stringTypeOfDatabaseState = stringTypeOfDatabaseState,
60-
61-
pathState = pathState,
62-
file = file,
63-
onPathChanged = { newPath -> pathState.value = newPath },
64-
onFilePicked = {
65-
file.value =
66-
controller.value.openFileDialog(
67-
window,
68-
"Load a file",
69-
listOf(".json"),
70-
allowMultiSelection = false
71-
)
72-
},
73-
74-
listOfNames = listNames.value,
75-
nameState = nameState,
76-
onNameChanged = { newName -> nameState.value = newName },
77-
78-
onLoadTree = {
79-
// controller.value.loadTree(nameState.value)
80-
state.value = States.DRAW_TREE
81-
}
82-
83-
84-
)
85-
86-
else -> {
87-
window.setSize(1080, 800)
88-
Editor()
89-
}
90-
}
50+
// when (state.value) {
51+
// States.OPENING_TREE -> OpenTree(
52+
// listOfDatabase = listOfDatabase,
53+
// onTypeOfDatabaseChanged = { newType ->
54+
// stringTypeOfDatabaseState.value = newType
55+
// typeOfDatabaseState.value = databases.getOrDefault(stringTypeOfDatabaseState.value, TypeOfDatabase.Json)
56+
// },
57+
// typeOfDatabaseState = typeOfDatabaseState,
58+
// stringTypeOfDatabaseState = stringTypeOfDatabaseState,
59+
//
60+
// pathState = pathState,
61+
// file = file,
62+
// onPathChanged = { newPath -> pathState.value = newPath },
63+
// onFilePicked = {
64+
// file.value =
65+
// controller.value.openFileDialog(
66+
// window,
67+
// "Load a file",
68+
// listOf(".json"),
69+
// allowMultiSelection = false
70+
// )
71+
// },
72+
//
73+
// listOfNames = listNames.value,
74+
// nameState = nameState,
75+
// onNameChanged = { newName -> nameState.value = newName },
76+
//
77+
// onLoadTree = {
78+
//// controller.value.loadTree(nameState.value)
79+
// state.value = States.DRAW_TREE
80+
// }
81+
//
82+
//
83+
// )
84+
//
85+
// else -> {
86+
// window.setSize(1080, 800)
87+
// Editor()
88+
// }
89+
// }
9190
}

lib/src/main/kotlin/trees/AVLTree.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@ package trees
88
import trees.nodes.AVLNode
99

1010
class AVLTree<T : Comparable<T>> : AbstractTree<T, AVLNode<T>>() {
11-
11+
//adds a new node to the AVL tree.
1212
override fun add(data: T) {
1313
root = balancedAdd(root, AVLNode(data))
1414
root?.updateHeight()
1515
root?.parent = null
1616
}
1717

18+
//checks if the given value is contained in the AVL tree.
1819
override fun contains(data: T): Boolean {
1920
return (contains(root, AVLNode(data)) != null)
2021
}
2122

23+
//removes the node with the given value from the AVL tree.
2224
override fun delete(data: T) {
2325
root = balancedDelete(root, AVLNode(data))
2426
root?.updateHeight()
2527
root?.parent = null
2628
}
2729

30+
//method that balances the tree starting from the given node.
2831
override fun balance(initNode: AVLNode<T>?): AVLNode<T>? {
2932
if (initNode == null) {
3033
return null
@@ -54,10 +57,12 @@ class AVLTree<T : Comparable<T>> : AbstractTree<T, AVLNode<T>>() {
5457
return initNode
5558
}
5659

60+
//method that returns the value of the node with the given value.
5761
fun get(data: T): T? {
5862
return contains(root, AVLNode(data))?.data
5963
}
6064

65+
//helper method of the AVLTree class that updates the height of the passed node's child nodes.
6166
private fun updateChildrenHeight(node: AVLNode<T>?) {
6267
node?.left?.updateHeight()
6368
node?.right?.updateHeight()

lib/src/main/kotlin/trees/AbstractTree.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
1212
var root: NodeType? = null
1313
internal set
1414

15+
//This function is called after a node has been inserted or removed from the tree,
16+
// and is used to balance the tree if necessary.
17+
// The default implementation simply returns the starting node without performing any balancing.
1518
protected open fun balance(initNode: NodeType?): NodeType? {
1619
return initNode
1720
}
1821

22+
//This function is used to add a node to a tree while ensuring that the tree remains balanced.
23+
//It recursively traverses the tree until it finds the correct position for the new node,
24+
// and then calls balance to balance the tree.
1925
protected fun balancedAdd(initNode: NodeType?, node: NodeType): NodeType? {
2026

2127
if (initNode == null) {
@@ -32,6 +38,8 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
3238
return balance(initNode)
3339
}
3440

41+
//This function is used to remove a node from a tree while ensuring that the tree remains balanced.
42+
// It recursively traverses the tree until it finds the node to be removed, and then calls
3543
protected fun balancedDelete(initNode: NodeType?, node: NodeType): NodeType? {
3644
if (initNode == null) {
3745
return null
@@ -57,6 +65,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
5765
return balance(initNode)
5866
}
5967

68+
//This function is used to check if a node matches with the same value as
6069
protected fun contains(initNode: NodeType?, node: NodeType): NodeType? {
6170
if (initNode == null) {
6271
return null
@@ -71,6 +80,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
7180
}
7281
}
7382

83+
//This function returns the node with the smallest value in the subtree rooted at the specified node.
7484
protected fun getMinimal(node: NodeType): NodeType {
7585
var minNode = node
7686
while (true) {
@@ -79,6 +89,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
7989
return minNode
8090
}
8191

92+
//This function returns the node with the largest value in the subtree rooted at the specified node.
8293
protected fun getMaximal(node: NodeType): NodeType {
8394
var maxNode = node
8495
while (true) {
@@ -87,6 +98,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
8798
return maxNode
8899
}
89100

101+
//This function performs a left rotation on the specified node, changing its position to its right child node.
90102
protected fun rotateLeft(node: NodeType): NodeType? {
91103
val rightChild = node.right
92104
val secondSubtree = rightChild?.left
@@ -97,6 +109,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
97109
return rightChild
98110
}
99111

112+
//This function performs a right rotation for the specified node, changing its position to its left child.
100113
protected fun rotateRight(node: NodeType): NodeType? {
101114
val leftChild = node.left
102115
val secondSubtree = leftChild?.right
@@ -107,6 +120,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
107120
return leftChild
108121
}
109122

123+
//This function replaces the specified child node of a node with a new child node
110124
protected fun replaceChild(child: NodeType, newChild: NodeType?): NodeType? {
111125
if (child == root) {
112126
root = newChild
@@ -122,6 +136,9 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
122136
return newChild
123137
}
124138

139+
//This function returns a preview of the tree as a list of nodes.
140+
// It uses the walk internal function to recursively traverse the tree in advance
141+
// and add each node to the list.
125142
fun preOrder(): List<NodeType> {
126143
val result = mutableListOf<NodeType>()
127144
fun walk(node: NodeType, lst: MutableList<NodeType>) {

lib/src/main/kotlin/trees/BSTree.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ import trees.nodes.BSNode
99

1010
class BSTree<T : Comparable<T>> : AbstractTree<T, BSNode<T>>() {
1111

12-
12+
//method that adds a new node
1313
override fun add(data: T) {
1414
root = balancedAdd(root, BSNode(data))
1515
}
1616

17+
//method that checks if the given value is contained
1718
override fun contains(data: T): Boolean {
1819
return (contains(root, BSNode(data)) != null)
1920
}
2021

22+
//method that removes a node with a given value
2123
override fun delete(data: T) {
2224
root = balancedDelete(root, BSNode(data))
2325
root?.parent = null
2426
}
2527

28+
//method that returns the value of the node with the given value.
2629
fun get(data: T): T? {
2730
return contains(root, BSNode(data))?.data
2831
}

lib/src/main/kotlin/trees/KeyValue.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@ class KeyValue<K : Comparable<K>, V>(
1010
var value: V?
1111
) : Comparable<KeyValue<K, V>> {
1212

13+
//Compares KeyValue objects based on their keys.
1314
override fun compareTo(other: KeyValue<K, V>): Int {
1415
return key.compareTo(other.key)
1516
}
1617

18+
//Checks if a KeyValue object is equal to another object.
19+
//Two objects are considered equal if their keys are equal.
1720
override fun equals(other: Any?): Boolean {
1821
if (other is KeyValue<*, *>) {
1922
return key == other.key
2023
}
2124
return false
2225
}
2326

27+
//Returns the string representation of the KeyValue object in "key: value" format.
2428
override fun toString(): String {
2529
return "$key: $value"
2630
}
2731

32+
//Returns the hash code of the KeyValue object based on the hash code of the key.
2833
override fun hashCode(): Int {
2934
return key.hashCode()
3035
}

0 commit comments

Comments
 (0)