|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Demonstrates root insertion in BST. More...
#include <ctime>#include <iostream>#include <fstream>#include <tclap/CmdLine.h>#include <aleph.H>#include <tpl_binNode.H>#include <tpl_binNodeUtils.H>Go to the source code of this file.
Functions | |
| static void | printNode (BinNode< int > *node, int, int) |
| int | main (int argc, char *argv[]) |
Variables | |
| ofstream | output |
Demonstrates root insertion in BST.
This program demonstrates root insertion, an alternative insertion strategy for binary search trees. Unlike standard insertion (which places new elements at leaves), root insertion always places new elements at the root using rotations. This creates a self-adjusting tree structure that adapts to insertion patterns.
Root insertion works by:
The rotations maintain the BST property while moving the node upward:
✅ Temporal locality: Recently inserted elements near root ✅ Cache friendly: Frequently accessed elements at top ✅ Self-adjusting: Tree adapts to insertion patterns ✅ Fast access: Recent elements accessed quickly ✅ Splay-like: Similar to splay tree behavior
❌ No balance guarantee: Tree may become unbalanced ❌ More rotations: Extra overhead compared to standard insertion ❌ Worst case: Still O(n) height possible ❌ Overhead: Rotations add computational cost
| Aspect | Root Insertion | Splay Trees |
|---|---|---|
| When rotates | On insertion | On access |
| Rotates | New nodes only | Any accessed node |
| Complexity | O(log n) avg | O(log n) amortized |
| Balance | No guarantee | No guarantee |
| Use case | Insert-heavy | Access-heavy |
Recent insertions cluster near root!
insert_root-aux.Tree**: Tree structure in preorder formatThe visualization shows how recently inserted elements cluster near the root, creating a different structure than standard BST insertion.
| Operation | Standard Insertion | Root Insertion |
|---|---|---|
| Insert | O(log n) | O(log n) + rotations |
| Search (recent) | O(log n) | O(1) to O(log n) |
| Search (old) | O(log n) | O(log n) to O(n) |
| Balance | No | No |
Definition in file writeInsertRoot.C.
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 222 of file writeInsertRoot.C.
References Aleph::check_bst(), Aleph::computeHeightRec(), Aleph::destroyRec(), Aleph::insert_root(), KEY, Aleph::maps(), output, Aleph::preOrderRec(), printNode(), and root().
|
static |
Definition at line 217 of file writeInsertRoot.C.
References Aleph::BinNode< Key >::get_key(), and output.
Referenced by main().
| ofstream output |
Definition at line 215 of file writeInsertRoot.C.
Referenced by main(), and printNode().