Skip to content

References

Dmitrii Chirkov edited this page Apr 7, 2024 · 2 revisions

Create

Tree Description
class RegularTree<K : Comparable, V> Creates an Instance of regular binary search tree
class AVLTree<K : Comparable, V> Creates an Instance of avl binary search tree, balanced
class RedBlackTree<K : Comparable, V> Creates an Instance of red black binary search tree, balanced

You can only use comparable types for keys.

Properties

Property Description
val entries: Set<Pair<K, V>> Entries of the tree in a form of key-value pair inorder
val keys: Set Returns keys inorder
val size: Int Number of entries in a tree
val values: Collection Returns values inorder
val traversed: BSTTraversed<K, V, R> Tree traversal. see Traverse for more details

Same across all trees.

Functions

Method Description
fun clean() Resets the tree
fun containsKey(key: K): Boolean Returns true if tree specific key
fun containsValue(value: V): Boolean Returns true if tree specific value
fun getOrDefault(key: K, defaultValue: @UnsafeVariance V): V Return value by key, or if no value exists default value
fun isEmpty(): Boolean Return true if tree contains no entries, false otherwise
fun isNotEmpty(): Boolean Return false if tree contains no entries, true otherwise
fun put(key: K, value: V) Insert new value by key in tree
fun putAll(from: List<Pair<K, V>>) Insert a list of new values by their keys in tree
fun remove(key: K): V? Removes entry by key
fun search(key: K): V? Searches for value by key

Same across all trees.

Traverse

Traversal Description
fun inOrder(extractionFunction: (R) -> T): List Performs an inorder traversal of the tree
fun preOrder(extractionFunction: (R) -> T): List Performs a preorder traversal of the tree
fun postOrder(extractionFunction: (R) -> T): List Performs a postorder traversal of the tree
fun levelOrder(extractionFunction: (R) -> T): List Performs a levl order traversal of the tree

Where R is a type of the tree node, see Nodes for more details.

Nodes

Node method Description
fun getKey(): K Returns key of a node
fun getValue(): V Returns value of a node
fun getLeft(): R? Returns node left to a node, or null if left node does not exist
fun getRight(): R? Returns node right to a node, or null if right node does not exist

These methods are common to all nodes. There are trees with nodes, which have more properties. R is a type of the node.

Avl tree Node

Node method Description
fun getHeight(): Int Returns height of a node, conting from the root. height for root is 1
fun getBalanceFactor(): Int Returns a balnce factor of a node. balance factor is calculated as difference between height of right subtree and left subtree
fun getParent(): R? Returns a parent of a node, or null if node is root

Red Black tree Node

Node method Description
fun isBlack(): Boolean Returns true if node color is black, false otherwise
fun isRed(): Boolean Returns true if node color is red, false otherwise
fun getParent(): R? Returns a parent of a node, or null if node is root

Clone this wiki locally