35 std::shared_ptr<node> left;
36 std::shared_ptr<node> right;
39 explicit node(int64_t len, std::shared_ptr<node> left, std::shared_ptr<node> right) {
41 this->is_leaf =
false;
46 explicit node(std::string s) {
50 this->right =
nullptr;
55 void _inorder(std::function<
void(std::shared_ptr<node>)> callback, std::shared_ptr<node> root) {
57 _inorder(callback, root->left);
59 _inorder(callback, root->right);
63 std::vector<std::shared_ptr<node>> ropes;
64 std::shared_ptr<node> root;
68 explicit rope(std::string s, int64_t splits = 2) : root(
nullptr), _height(-1) {
69 this->create(s, splits);
72 void create(std::string s, int64_t splits = 2);
74 std::string inorder();
76 int64_t height() {
return this->_height; }
78#ifdef TREE_VISUALIZATION_H
80 std::string _generated = generate_visualization();
81 tree_visualization::visualize(_generated);
86 std::string generate_visualization() {
87 std::string _generate = _inorder_gen(root);
91 std::shared_ptr<node> split_tree(std::shared_ptr<node> root, int64_t& splits) {
92 std::queue<std::shared_ptr<node>> q;
95 q = std::queue<std::shared_ptr<node>>{};
97 int64_t count_height = -1;
99 int64_t size = q.size();
100 for (int64_t i = 0; i < size; i++) {
101 std::shared_ptr<node> curr = q.front();
104 q = std::queue<std::shared_ptr<node>>{};
107 if (curr->is_leaf ==
true) {
108 std::string s1 = (curr->data).substr(0,
int(curr->len) / 2 + 1);
109 std::string s2 = (curr->data)
110 .substr(
int(curr->len) / 2 + 1,
111 int(curr->len) -
int(curr->len) / 2);
112 std::shared_ptr<node> split1 = std::make_shared<node>(s1);
113 std::shared_ptr<node> split2 = std::make_shared<node>(s2);
115 std::shared_ptr<node> block_node =
116 std::make_shared<node>(curr->len, split1, split2);
118 curr->is_leaf = block_node->is_leaf;
119 curr->len = block_node->len;
120 curr->left = block_node->left;
121 curr->right = block_node->right;
126 if (curr->left !=
nullptr) {
129 if (curr->right !=
nullptr) {
134 _height = std::max(_height, count_height);
140 std::string _inorder_gen(std::shared_ptr<node> root) {
144 _s +=
'"' + std::to_string(root->len) +
',' + root->data +
'"';
146 _s +=
'"' + std::to_string(root->len) +
'"';
149 if (root->left->is_leaf) {
150 _s +=
'"' + std::to_string(root->left->len) +
',' + root->left->data +
'"';
152 _s +=
'"' + std::to_string(root->left->len) +
'"';
155 _s += _inorder_gen(root->left);
159 _s +=
'"' + std::to_string(root->len) +
',' + root->data +
'"';
161 _s +=
'"' + std::to_string(root->len) +
'"';
164 if (root->right->is_leaf) {
165 _s +=
'"' + std::to_string(root->right->len) +
',' + root->right->data +
'"';
167 _s +=
'"' + std::to_string(root->right->len) +
'"';
170 _s += _inorder_gen(root->right);