forest-group
is a library that lets you easily create and use Binary search trees, AVL trees and Red-black trees in your applications!
Trees are well-known for their speed and efficiency.
But there are lots of tree implementations that are strict to data types that can be stored.
The solution is easy - just use keys to store anything you want!
To create a tree, pass the key type and your stored data type to a generic. Note that your key should implement Comparable type.
val myBSTree = BSTree<Int, String>()
val myAVLTree = AVLTree<Char, Long>()
val myRBTree = RBTree<String, Float>()
You now can simply insert and replace values in your tree:
val myKey = 10
myBSTree.insert(myKey, "Something important")
val replacedValue = myBSTree.insert(myKey, "Something more important")
You can also search for values and delete values from tree by keys:
val myValue1 = myBSTree.search(myKey)
val myValue2 = myBSTree.delete(myKey)
println(myValue1 == myValue2) // true
All trees are iterable by Pair(key, value), so they can be used in a for loop.
Iterator implements inorder traversal (every next key is greater than the previous).
for ((key, value) in myRBTree) {
keysList.add(key)
valuesList.add(value)
}
myRBTree.forEach { println(it) } // prints every pair
If you have found a bug, or want to propose some useful feature for our project, please firstly read our Contribution Rules and do the following:
- Fork the Project
- Create your Feature Branch (git checkout -b feature/my-feature)
- Commit your Changes (git commit -m 'add some feature')
- Push to the Branch (git push origin feature/my-feature)
- Open a Pull Request
Distributed under the MIT License.