forked from spbu-coding-2022/trees-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the display of the binary search tree, add the ability to i…
…nsert a node and clear the tree.
- Loading branch information
1 parent
c52816b
commit 62cfba7
Showing
9 changed files
with
154 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,10 @@ | ||
package app | ||
|
||
import app.view.MainView | ||
import javafx.stage.Stage | ||
import tornadofx.* | ||
|
||
class MainApp: App(MainView::class){ | ||
override fun start(stage: Stage) { | ||
with(stage){ | ||
width = 600.0 | ||
height = 400.0 | ||
} | ||
super.start(stage) | ||
} | ||
class MyApp : App(MainView::class){ | ||
} | ||
|
||
fun main() { | ||
launch<MainApp>() | ||
launch<MyApp>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package app.controller | ||
import bst.BSTree | ||
import bst.nodes.BSTNode | ||
import javafx.scene.layout.Pane | ||
import javafx.scene.paint.Color | ||
import javafx.scene.shape.Circle | ||
import javafx.scene.shape.Line | ||
|
||
import tornadofx.Controller | ||
import javafx.scene.control.Label | ||
|
||
class BSTController: Controller(){ | ||
fun isNumeric(s: String): Boolean { | ||
return try { | ||
s.toInt() | ||
true | ||
} catch (e: NumberFormatException) { | ||
false | ||
} | ||
} | ||
fun insertNode(tree:BSTree<Int, String>, treePane: Pane, key: Int, value:String){ | ||
tree.insert(key, value) | ||
drawTree(tree, treePane) | ||
} | ||
fun clearTree(tree:BSTree<Int, String>, treePane: Pane) { | ||
tree.clear() | ||
treePane.children.clear() | ||
} | ||
|
||
//make here not null check | ||
private fun drawTree(tree:BSTree<Int, String>, treePane: Pane) { | ||
treePane.children.clear() | ||
if (tree.getRoot() != null) { | ||
drawNode(tree.getRoot()!!, treePane, treePane.width / 2.0, 50.0, treePane.width / 4.0) | ||
} | ||
} | ||
|
||
private fun drawNode(node: BSTNode<Int, String>, treePane: Pane, x: Double, y: Double, offsetX: Double) { | ||
val circleRadius = 20.0 | ||
val circle = Circle(circleRadius) | ||
circle.centerX = x | ||
circle.centerY = y | ||
circle.fill = Color.WHITE | ||
circle.stroke = Color.BLACK | ||
val nodeLabel = Label(node.key.toString()) | ||
nodeLabel.layoutX = circle.centerX - (circle.radius / 3) | ||
nodeLabel.layoutY = circle.centerY - (circle.radius / 3) | ||
treePane.children.addAll(circle,nodeLabel ) | ||
|
||
if (node.left != null) { | ||
val leftX = x - offsetX | ||
val leftY = y + 50 | ||
val leftLine = Line(x, y + circleRadius, leftX, leftY - circleRadius) | ||
treePane.children.add(leftLine) | ||
drawNode(node.left!!, treePane, leftX, leftY, offsetX / 2.0) | ||
} | ||
|
||
if (node.right != null) { | ||
val rightX = x + offsetX | ||
val rightY = y + 50 | ||
val rightLine = Line(x, y + circleRadius, rightX, rightY - circleRadius) | ||
treePane.children.add(rightLine) | ||
drawNode(node.right!!,treePane, rightX, rightY, offsetX / 2.0) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package app.view | ||
|
||
import tornadofx.Fragment | ||
import tornadofx.label | ||
|
||
class AdditionErrorFragment: Fragment() { | ||
override val root = label("Addition error") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 48 additions & 3 deletions
51
app/src/main/kotlin/app/view/treeView/BinarySearchTreeView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,62 @@ | ||
package app.view.treeView | ||
import tornadofx.* | ||
import app.controller.BSTController | ||
import app.view.AdditionErrorFragment | ||
import bst.BSTree | ||
import javafx.beans.property.SimpleStringProperty | ||
import javafx.scene.layout.Pane | ||
import javafx.stage.StageStyle | ||
|
||
class BinarySearchTreeView: View() { | ||
class BinarySearchTreeView : View() { | ||
private val controller: BSTController by inject() | ||
private val tree = BSTree<Int, String>() | ||
private val treePane = Pane() | ||
val key = SimpleStringProperty() | ||
val value = SimpleStringProperty() | ||
override val root = vbox { | ||
hbox { | ||
button("Clear") { | ||
action { controller.clearTree(tree, treePane) } | ||
} | ||
form { | ||
fieldset { | ||
field("Key input") { | ||
textfield(key) | ||
} | ||
field("Value input") { | ||
textfield(value) | ||
} | ||
|
||
button("Add Node") { | ||
action { | ||
if (controller.isNumeric(key.value)){ | ||
controller.insertNode(tree, treePane, key.value.toInt(), value.value) | ||
} | ||
else{ | ||
find<AdditionErrorFragment>().openModal(stageStyle = StageStyle.UTILITY) | ||
} | ||
key.value = "" | ||
value.value = "" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
button("AVL Tree") { | ||
action { | ||
replaceWith<AVLTreeView>() | ||
replaceWith(AVLTreeView::class, ViewTransition.Slide(0.3.seconds, ViewTransition.Direction.LEFT)) | ||
} | ||
} | ||
button("Red Black Tree") { | ||
action { | ||
replaceWith<RedBlackTreeView>() | ||
replaceWith(RedBlackTreeView::class, ViewTransition.Slide(0.3.seconds, ViewTransition.Direction.LEFT)) | ||
} | ||
} | ||
this += treePane | ||
treePane.apply { | ||
minWidth = 600.0 | ||
minHeight = 400.0 | ||
style = "-fx-border-color: black;" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters