BinTreeKit
is a library that provides implementations for three types of trees: SimpleBinarySearchTree
, AVLSearchTree
and RBSearchTree
. The library is designed to simplify the process of managing hierarchical data, allowing Kotlin developers to focus on building robust and scalable applications.
- Importing Classes:
import main.trees.SimpleBinarySearchTree
import main.trees.AVLSearchTree
import main.trees.rbTree
- Instantiate Trees:
val map = mapOf(Pair(1, "cat"), Pair(2, "dog"))
// create a Simple Binary Search Tree
val emptyBstTree = SimpleBinarySearchTree()
val bstTree = SimpleBinarySearchTree(map)
// create an AVL Search Tree
val emptyAvlTree = AVLSearchTree()
val avlTree = AVLSearchTree(map)
// create a Red-Black Search Tree
val emptyRbTree = RBSearchTree()
val rbTree = RBSearchTree(map)
- Use Tree Methods:
// put key-value pairs with different values of replaceIfExists perematers
bstTree.putAll(map, true)
rbTree.put(4, "horse", false)
// remove key-value pair from tree and return value
println(rbTree.remove(4)) // output: horse
bstTree.remove(1)
//get key-value pair from tree
println(avlTree.getMin()) //output: cat
// pairwise iteration
for (pair in avlTree) {
print("${pair.second}, ")
} // output: cat, dog
-
constructor(comparator: Comparator<K>? = null)
- constructs a new AbstractBinarySearchTree instance with the given comparator. -
constructor(map: Map<K, V>, replaceIfExists: Boolean = true, comparator: Comparator<K>? = null)
- constructs a new AbstractBinarySearchTree instance initialized with the contents of the given map.The
comparator
to be used optional for ordering keys in the tree.
-
put(key: K, value: V, replaceIfExists : Boolean = true)
- inserts the specified key-value pair into the tree. -
putAll(map: Map<K, V>, replaceIfExists: Boolean = true)
- inserts all key-value pairs from the given map into the tree.replaceIfExists
is an optional (default is true) Boolean flag indicating whether to replace the value if the key already exists in the tree. -
remove(key: K): V?
- removes the key-value pair with the specified key from the tree and returns value associated with the removed key, ornull
if the key was not found in the tree. -
get(key: K): V?
- retrieves the value associated with the specified key from the tree and returns value associated with the specified key, ornull
if the key was not found in the tree. -
getPair(key: K): Pair<K, V>?
- retrieves the key-value pair associated with the specified key from the tree and returns the key-value pair associated with the specified key, ornull
if the key was not found in the tree. -
getMin(): V?
- retrieves the value associated with the minimum key in the tree and returns the value associated with the minimum key, ornull
if the tree is empty. -
getMax(): V?
- retrieves the value associated with the maximum key in the tree and returns the value associated with the maximum key, ornull
if the tree is empty. -
getMinKeyPair(): Pair<K, V>?
- retrieves the key-value pair associated with the minimum key in the tree and returns the key-value pair associated with the minimum key, ornull
if the tree is empty. -
getMaxKeyPair(): Pair<K, V>?
- retrieves the key-value pair associated with the maximum key in the tree and returns the key-value pair associated with the maximum key, ornull
if the tree is empty. -
size(): Long
- returns the number of key-value pairs in the tree. -
isEmpty(): Boolean
checks whether the tree is empty and returnstrue
if the tree is empty,false
otherwise.
-
size: Long
- the number of key-value pairs currently stored in the tree. -
isEmpty: Boolean
- indicates whether the tree is empty.
-
constructor(vertex: N?)
- constructs a new TreeIterator instance with the specified starting vertex.vertex
is the starting vertex of the iterator.
-
hasNext(): Boolean
- checks if there are more elements in the iteration and returnstrue
if there are more elements,false
otherwise. -
next(): Pair<K, V>
- retrieves the next key-value pair in the iteration and returns the next key-value pair in the iteration.
- vicitori - Victoria Lutsyuk (Simple Binary Search Tree)
- Szczucki - Nikita Shchutskii (AVL Search Tree)
- TerrMen - Kostya Oreshin (RB Search Tree)
Quick start
- Create new branch branching off
develop
, name it as feature you want to implement
git checkout develop
git switch -c my_feature
- Commit the changes and write messages according to commits convention
git commit -m "feat: implement new feature"
- Push changes to a remote repository
git push origin my_feature
- Open pull request to
develop
This project uses the APACHE LICENSE, VERSION 2.0. See the LICENSE for more info.