An open source library written in Kotlin to work with data structures such as AVL tree, red-black tree, and binary search tree.
To build the library run
./gradlew build
To run PostgreSQL with docker:
./start-db.sh
or
./start-db.bat
Any data (provided with Comparable
key) can be stored in trees.
For example:
import bst.BSTree
val tree = BSTree(1, "apple")
tree.insert(7, "orange")
tree.insert(28, "Alice")
tree.insert(4, "Bob")
Constructor takes two arguments: key
and value
, thus instantiating a root node (you can delete it,
but you cannot create an empty tree).
insert
method also takes same arguments and adds a node with specified key
and value
properties to the tree.
Method setName
allows you to set the name of a tree.
Find or remove element from tree:
tree.find(4) // returns "Bob"
tree.remove(1)
find
and remove
methods take some Comparable
key as an argument.
AVL and red-black trees implement the same methods.
AVL tree can be saved to and loaded from JSON file. For example:
val tree = AVLTree(1, "apple")
tree.setName("test")
val controller = JsonController()
controller.saveTreeToJson(test)
println(controller.readFromJson("test")?.treeName)
You can also save binary search tree to SQL database:
val tree = BSTree(1, "apple")
tree.setName("test")
val controller = SQLController()
controller.saveTreeToDB(test_data)
val remTree = controller.getTree("test")
And you can save red-black tree to Neo4j database
val tree = RedBlackTree(1, "apple")
tree.setName("test")
val controller = Neoj4Conroller()
contoller.saveTree(tree)
val remTree = controller.loadTree("test")