Skip to content

Commit

Permalink
feat: add ability to delete node
Browse files Browse the repository at this point in the history
  • Loading branch information
SurfaceYellowDuck committed May 1, 2023
1 parent f5d46de commit 7ebf862
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 40 deletions.
13 changes: 8 additions & 5 deletions app/src/main/kotlin/app/controller/BSTController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import javafx.scene.control.Label
import bst.db.controllers.SQLController
import javafx.collections.FXCollections.observableArrayList
import javafx.collections.ObservableList
import kotlin.reflect.jvm.internal.impl.resolve.constants.KClassValue.Value

class BSTController : Controller() {
fun isNumeric(s: String): Boolean {
Expand Down Expand Up @@ -90,13 +91,15 @@ class BSTController : Controller() {

fun deleteTreeFromDB(name: String) {
SQLController().run {
deleteTree(name)
removeTree(name)
}
}
fun saveTree(tree: BSTree<Int, String>) {
fun saveTree(tree: BSTree<Int, String>, treeName: String) {
val controller = SQLController()
controller.saveTreeToDB(tree)
controller.saveTree(tree, treeName)
}
fun deleteNode(value: Int, tree: BSTree<Int, String>, treePane: Pane){
tree.remove(value)
drawTree(tree, treePane)
}
}


23 changes: 0 additions & 23 deletions app/src/main/kotlin/app/view/SetTreeNameFragment.kt

This file was deleted.

24 changes: 21 additions & 3 deletions app/src/main/kotlin/app/view/treeView/BinarySearchTreeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BinarySearchTreeView : View() {
private var trees = controller.getTreesList()
private var selectedItem: String? = ""
private val treeName = SimpleStringProperty()
private val valueFotDeletion = SimpleStringProperty()
override val root = vbox {
hbox {
val availableTrees = combobox<String> {
Expand Down Expand Up @@ -72,20 +73,37 @@ class BinarySearchTreeView : View() {
key.value = ""
value.value = ""
}

}
field("Value input"){
textfield(valueFotDeletion)
}
button("Delete node"){
action {
if (controller.isNumeric(valueFotDeletion.value)){
controller.deleteNode(valueFotDeletion.value.toInt(), tree, treePane)
}
else{
alert(type = Alert.AlertType.ERROR, header = "Deletion Error")
}
}
}

field("Input tree name") {
textfield(treeName)
}
button("Save tree") {
action {
if (tree.getRoot() != null) {
tree.treeName = treeName.value
controller.saveTree(tree)
// tree.treeName = treeName.value
controller.saveTree(tree, treeName.value)
if (!availableTrees.items.contains(treeName.value)) {
availableTrees.items.add(treeName.value)
}
}
else{
alert(type = Alert.AlertType.ERROR, header = "Can not save tree with empty root")

}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions trees/src/main/kotlin/bst/AbstractBST.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ abstract class AbstractBST<K : Comparable<K>, V, Self : BinaryNode<K, V, Self>>

internal var rootNode: Self? = null

fun setName(treeName: String){
this.treeName = treeName
}
// fun setName(treeName: String){
// this.treeName = treeName
// }

fun getRoot(): Self? = this.rootNode

Expand Down
27 changes: 21 additions & 6 deletions trees/src/main/kotlin/bst/db/controllers/SQLController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import bst.db.models.sql.Trees
import bst.db.serializeClasses.SerializableNode
import bst.db.serializeClasses.SerializableTree
import bst.nodes.BSTNode
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

class SQLController : Controller<BSTNode<Int, String>, BSTree<Int, String>> {
Expand All @@ -26,8 +24,13 @@ class SQLController : Controller<BSTNode<Int, String>, BSTree<Int, String>> {

override fun removeTree(treeName: String) {
transaction {
val treeEntity = Tree.find { (Trees.name eq treeName) }.firstOrNull()
treeEntity?.delete()
try{
Tree.find { (Trees.name eq treeName) }
.firstOrNull()?.delete()
}
catch (e: ExposedSQLException){
println("Tree does not exists")
}
}
}

Expand Down Expand Up @@ -145,4 +148,16 @@ class SQLController : Controller<BSTNode<Int, String>, BSTree<Int, String>> {
}
return deserializeTree(deserializedTree)
}
fun getAllTrees(): List<String> {
val notes = mutableListOf<String>()
connectDB()
transaction {
Trees.selectAll().forEach {
val name = it[Trees.name]
notes.add(name)
}
}

return notes
}
}

0 comments on commit 7ebf862

Please sign in to comment.