Kalkwst / MicroLib

MIT License
3 stars 0 forks source link

:sparkles: Add Scapegoat Tree Implementation #40

Open Kalkwst opened 1 year ago

Kalkwst commented 1 year ago

A scapegoat tree is a type of self-balancing binary search tree, which is used to efficiently store and retrieve data. It is similar to other types of self-balancing trees, such as AVL and red-black trees, in that it automatically adjusts the structure of the tree to maintain a balance between its left and right branches. However, it is different in that it adjusts the tree based on a dynamic threshold, rather than a fixed one, which can lead to more efficient use of memory and improved performance of search and insertion operations.

From a more technical perspective, a scapegoat tree is a self-balancing binary search tree that uses a dynamic threshold to ensure balance. It maintains a balance by periodically rebuilding the tree if the size of a subtree exceeds a certain threshold, the value of which is adjusted dynamically. The threshold is calculated by comparing the size of the subtree to the number of nodes in the tree. When a new node is added to the tree, it is placed in the correct position based on its value. If the size of a subtree exceeds the threshold, the subtree is rebuilt to maintain balance. The time complexity for search, insertion and deletion operations in a scapegoat tree is O(log n), but it's slightly worse than AVL or red-black trees, as it needs to rebuild the tree periodically.