Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you’re going to implement splay trees. BST Implementation For

ID: 3603380 • Letter: I

Question

In this assignment you’re going to implement splay trees.

BST Implementation

For splay trees, you should begin by implementing a basic (unbalanced) binary search tree, with integer keys and no values (i.e., a Set data structure). Use the following node type:

Maintaining parent pointers complicates some of the algorithms! I would suggest implementing the basic, unbalanced BST operations first, and then adding the parent pointers and making sure everything still works, and finally adding the balancing code. Of course, your code won’t pass the tests until the balancing code is correct.

Implementation

You must implement the following tree operations:

These functions should work in exactly the same way as described in class. Note that find and insertshould all do a splay step after their main operation, to rebalance the tree! (The splay function will be tested independently of them, to make sure that it works correctly on its own.) You do not need to implement removal, because it’s really hard to get right.

find is a bit different than the implementation shown in class: It takes the root of the tree by reference, finds the target node, and then splays it to the root of the tree. Thus, after a find, root->key == value and there’s no need to return the found node. Instead, it returns a bool, true if the node was found, and false otherwise. Note that if find returns false then the tree must be unchanged!

rotate does not return the new parent node: because we have parent pointers, we can rewrite the tree in-place.

insert does return the new root of the tree, and thus must be used like this:

Be sure to correctly handle the case where root == nullptr (i.e., the empty tree)!

When you compile, link with assign4_test.cpp. The test runner will first test your rotate function (because if that doesn’t work correctly, nothing else will, either) and then proceed to construct a BST by inserting and finding nodes. After each operation, it will verify the tree structure, to make sure that parent pointers are lined up and such, and after every insert and find, it will check to make sure the target node has been splayed to the root.

Explanation / Answer

*/ #include #include #include #include #include #include using std::cout; std::vector make_random_permutation( std::size_t len, int seed = 1) { std::default_random_engine generator(seed); std::vector ret(len, 0); // Initialize vector to 0...len-1 for(std::size_t i = 0; i left) { // One subtree, on the right return 0.5 * balance(root->left); } else if(!root->right) { return 0.5 * balance(root->right); } else // Two subtrees return (balance(root->right) + balance(root->left)) / 2; } // Safe find, that does not modify the tree structure bool safe_find(node* root, int value) { if(!root) return false; else if(root->key == value) return true; else if(value key) return safe_find(root->left, value); else // value key return safe_find(root->right, value); } int count_nodes(node* root) { if(!root) return 0; else return 1 + count_nodes(root->left) + count_nodes(root->right); } int tree_height(node* root) { if(!root) return 0; else return 1 + std::max(tree_height(root->left), tree_height(root->right)); } // Pretty-print a tree. This does cycle-checking at the same time, so that if // there's a cycle in the tree we won't get stuck in a loop. void print(node* root, int level, int parents, bool left_child, std::set& nodes) { if(level == 0) cout parent is not original child. "; return false; } else if(!check_for_cycles(child)) { cout parent = parent; rotate(child, parent); if(parent->parent != child) { cout right != parent) { cout