Skip to content

ancavar/trees-3

 
 

Repository files navigation

Binary search trees library

An open source library written in Kotlin to work with data structures such as AVL tree, red-black tree, and binary search tree.

🖍 Used technology

Kotlin Junit Neo4j Postgresql Docker

📦 Getting started

To build the library run

./gradlew build

To run PostgreSQL with docker:

./start-db.sh

or

./start-db.bat

Using binary search trees

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.

Storing binary search trees

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")

About

trees-3 created by GitHub Classroom

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 99.6%
  • Other 0.4%