Commit 432269d5 authored by Ondřej Mička's avatar Ondřej Mička

Splay operation

parent 95b531a4
test: splay_operation_test
./$<
CXXFLAGS=-std=c++11 -O2 -Wall -Wextra -g -Wno-sign-compare
splay_operation_test: splay_operation.h splay_operation_test.cpp test_main.cpp
$(CXX) $(CXXFLAGS) $^ -o $@
clean::
rm -f splay_operation_test
.PHONY: clean test
// A node of the tree
class Node {
public:
int key;
Node* left;
Node* right;
Node* parent;
// Constructor
Node(int key, Node* parent=nullptr, Node* left=nullptr, Node* right=nullptr) {
this->key = key;
this->parent = parent;
this->left = left;
this->right = right;
if (left) left->parent = this;
if (right) right->parent = this;
}
};
// Binary tree
class Tree {
public:
// Pointer to root of the tree; nullptr if the tree is empty.
Node* root;
Tree(Node* root=nullptr) {
this->root = root;
}
// Rotate the given `node` up. Perform a single rotation of the edge
// between the node and its parent, choosing left or right rotation
// appropriately.
virtual void rotate(Node* node) {
if (node->parent) {
if (node->parent->left == node) {
if (node->right) node->right->parent = node->parent;
node->parent->left = node->right;
node->right = node->parent;
} else {
if (node->left) node->left->parent = node->parent;
node->parent->right = node->left;
node->left = node->parent;
}
if (node->parent->parent) {
if (node->parent->parent->left == node->parent)
node->parent->parent->left = node;
else
node->parent->parent->right = node;
} else {
root = node;
}
Node* original_parent = node->parent;
node->parent = node->parent->parent;
original_parent->parent = node;
}
}
// Look up the given key in the tree, returning the
// the node with the requested key or nullptr.
Node* lookup(int key) {
// TODO: Utilize splay suitably.
Node* node = root;
while (node) {
if (node->key == key) {
return node;
}
if (key < node->key)
node = node->left;
else
node = node->right;
}
return nullptr;
}
// Insert a key into the tree.
// If the key is already present, nothing happens.
void insert(int key) {
// TODO: Utilize splay suitably.
if (!root) {
root = new Node(key);
return;
}
Node* node = root;
while (node->key != key) {
if (key < node->key) {
if (!node->left)
node->left = new Node(key, node);
node = node->left;
} else {
if (!node->right)
node->right = new Node(key, node);
node = node->right;
}
}
}
// Delete given key from the tree.
// It the key is not present, do nothing.
void remove(int key) {
// TODO: Utilize splay suitably.
Node* node = root;
while (node && node->key != key) {
if (key < node->key)
node = node->left;
else
node = node->right;
}
if (node) {
if (node->left && node->right) {
Node* replacement = node->right;
while (replacement->left)
replacement = replacement->left;
node->key = replacement->key;
node = replacement;
}
Node* replacement = node->left ? node->left : node->right;
if (node->parent) {
if (node->parent->left == node) node->parent->left = replacement;
else node->parent->right = replacement;
} else {
root = replacement;
}
if (replacement)
replacement->parent = node->parent;
delete node;
}
}
// Splay the given node.
// If a single rotation needs to be performed, perform it as the last rotation
// (i.e., to move the splayed node to the root of the tree).
virtual void splay(Node* node) {
// TODO: Implement
}
// Destructor to free all allocated memory.
~Tree() {
Node* node = root;
while (node) {
Node* next;
if (node->left) {
next = node->left;
node->left = nullptr;
} else if (node->right) {
next = node->right;
node->right = nullptr;
} else {
next = node->parent;
delete node;
}
node = next;
}
}
};
#include <algorithm>
#include <cassert>
#include <fstream>
#include <functional>
#include <string>
#include <utility>
#include <vector>
#include "splay_operation.h"
using namespace std;
// If the condition is not true, report an error and halt.
#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0)
void expect_failed(const string& message);
// Flatten the tree: return a sorted list of all keys in the tree.
vector<int> flatten(const Tree& tree) {
constexpr int L = 0, R = 1, F = 2;
Node* node = tree.root;
vector<int> flattened, stack = {L};
while (!stack.empty()) {
if (stack.back() == L) {
stack.back() = R;
if (node->left) {
node = node->left;
stack.push_back(L);
}
} else if (stack.back() == R) {
flattened.push_back(node->key);
stack.back() = F;
if (node->right) {
node = node->right;
stack.push_back(L);
}
} else {
node = node->parent;
stack.pop_back();
}
}
return flattened;
}
// Test for splay operation with required helpers
class TestSplay {
public:
static Node* deserialize_node(const string& text, int& index) {
EXPECT(text[index++] == '(', "Internal error during example deserialization");
if (text[index] == ')') {
index++;
return nullptr;
} else {
int comma = text.find(',', index);
int key = stoi(text.substr(index, comma - index));
Node* left = deserialize_node(text, (index=comma + 1));
Node* right = deserialize_node(text, ++index);
EXPECT(text[index++] == ')', "Internal error during example deserialization");
return new Node(key, nullptr, left, right);
}
}
static Node* deserialize_root(const string& text) {
int index = 0;
Node* root = deserialize_node(text, index);
assert(index == text.size());
return root;
}
static string compare(Node* system, Node* gold) {
if (!system && gold) {
return "expected node with key " + to_string(gold->key) + ", found None";
} else if (system && !gold) {
return "expected None, found node with key " + to_string(system->key);
} else if (system && gold) {
if (system->key != gold->key)
return "expected node with key " + to_string(gold->key) + ", found " + to_string(system->key);
auto result = compare(system->left, gold->left);
if (!result.empty()) return result;
return compare(system->right, gold->right);
}
return string();
}
static void test() {
ifstream splay_tests_file("splay_tests.txt");
EXPECT(splay_tests_file.is_open(), "Cannot open splay_tests.txt file with the tests");
string original, splayed;
int target;
while (splay_tests_file >> original >> target >> splayed) {
Tree original_tree(deserialize_root(original));
Tree splayed_tree(deserialize_root(splayed));
Node* target_node = original_tree.root;
while (target_node && target_node->key != target)
if (target < target_node->key)
target_node = target_node->left;
else
target_node = target_node->right;
EXPECT(target_node, "Internal error during finding the target node in the tree to splay");
original_tree.splay(target_node);
auto error = compare(original_tree.root, splayed_tree.root);
EXPECT(error.empty(), "Error running splay on key " + to_string(target) + " of " + original + ": " + error);
}
}
};
void test_lookup() {
// Insert even numbers
Tree tree;
for (int i = 0; i < 5000000; i += 2)
tree.insert(i);
// Find non-existing
for (int i = 1; i < 5000000; i += 2)
for (int j = 0; j < 10; j++)
EXPECT(!tree.lookup(i), "Non-existing element was found");
// Find existing
for (int i = 0; i < 5000000; i += 2)
for (int j = 0; j < 10; j++)
EXPECT(tree.lookup(i), "Existing element was not found");
}
void test_insert() {
// Test validity first
{
Tree tree;
vector<int> sequence = {997};
for (int i = 2; i < 1999; i++)
sequence.push_back((sequence.back() * sequence.front()) % 1999);
for (const auto& i : sequence)
tree.insert(i);
vector<int> flattened = flatten(tree);
sort(sequence.begin(), sequence.end());
EXPECT(flattened == sequence, "Incorrect tree after a sequence of inserts");
}
// Test speed
{
Tree tree;
for (int i = 0; i < 5000000; i++)
for (int j = 0; j < 10; j++)
tree.insert(i);
}
}
void test_remove() {
// Test validity first
{
Tree tree;
for (int i = 2; i < 1999 * 2; i++)
tree.insert(i);
vector<int> sequence = {2 * 997};
for (int i = 2; i < 1999; i++)
sequence.push_back(2 * ((sequence.back() * sequence.front() / 4) % 1999));
for (const auto& i : sequence)
tree.remove(i + 1);
vector<int> flattened = flatten(tree);
sort(sequence.begin(), sequence.end());
EXPECT(flattened == sequence, "Correct tree after a sequence of removes");
}
// Test speed
{
Tree tree;
for (int i = 0; i < 5000000; i++)
tree.insert(i);
// Non-existing elements
for (int i = 1; i < 5000000; i += 2)
for (int j = 0; j < 10; j++)
tree.remove(i);
// Existing elements
for (int i = 2; i < 5000000; i += 2)
for (int j = 0; j < 10; j++)
tree.remove(i);
}
}
vector<pair<string, function<void()>>> tests = {
{ "splay", TestSplay::test },
{ "lookup", test_lookup },
{ "insert", test_insert },
{ "remove", test_remove },
};
This diff is collapsed.
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
extern vector<pair<string, function<void()>>> tests;
void expect_failed(const string& message) {
cerr << "Test error: " << message << endl;
exit(1);
}
int main(int argc, char* argv[]) {
vector<string> required_tests;
if (argc > 1) {
required_tests.assign(argv + 1, argv + argc);
} else {
for (const auto& test : tests)
required_tests.push_back(test.first);
}
for (const auto& required_test : required_tests) {
bool found = false;
for (const auto& test : tests)
if (required_test == test.first) {
cerr << "Running test " << required_test << endl;
test.second();
found = true;
break;
}
if (!found) {
cerr << "Unknown test " << required_test << endl;
return 1;
}
}
return 0;
}
#!/usr/bin/env python3
class Node:
"""Node in a binary tree `Tree`"""
def __init__(self, key, left=None, right=None, parent=None):
self.key = key
self.parent = parent
self.left = left
self.right = right
if left is not None: left.parent = self
if right is not None: right.parent = self
class Tree:
"""A simple binary search tree"""
def __init__(self, root=None):
self.root = root
def rotate(self, node):
""" Rotate the given `node` up.
Performs a single rotation of the edge between the given node
and its parent, choosing left or right rotation appropriately.
"""
if node.parent is not None:
if node.parent.left == node:
if node.right is not None: node.right.parent = node.parent
node.parent.left = node.right
node.right = node.parent
else:
if node.left is not None: node.left.parent = node.parent
node.parent.right = node.left
node.left = node.parent
if node.parent.parent is not None:
if node.parent.parent.left == node.parent:
node.parent.parent.left = node
else:
node.parent.parent.right = node
else:
self.root = node
node.parent.parent, node.parent = node, node.parent.parent
def lookup(self, key):
"""Look up the given key in the tree.
Returns the node with the requested key or `None`.
"""
# TODO: Utilize splay suitably.
node = self.root
while node is not None:
if node.key == key:
return node
if key < node.key:
node = node.left
else:
node = node.right
return None
def insert(self, key):
"""Insert key into the tree.
If the key is already present, do nothing.
"""
# TODO: Utilize splay suitably.
if self.root is None:
self.root = Node(key)
return
node = self.root
while node.key != key:
if key < node.key:
if node.left is None:
node.left = Node(key, parent=node)
node = node.left
else:
if node.right is None:
node.right = Node(key, parent=node)
node = node.right
def remove(self, key):
"""Remove given key from the tree.
It the key is not present, do nothing.
"""
# TODO: Utilize splay suitably.
node = self.root
while node is not None and node.key != key:
if key < node.key:
node = node.left
else:
node = node.right
if node is not None:
if node.left is not None and node.right is not None:
replacement = node.right
while replacement.left is not None:
replacement = replacement.left
node.key = replacement.key
node = replacement
replacement = node.left if node.left is not None else node.right
if node.parent is not None:
if node.parent.left == node: node.parent.left = replacement
else: node.parent.right = replacement
else:
self.root = replacement
if replacement is not None:
replacement.parent = node.parent
def splay(self, node):
"""Splay the given node.
If a single rotation needs to be performed, perform it as the last rotation
(i.e., to move the splayed node to the root of the tree).
"""
# TODO: Implement
raise NotImplementedError
#!/usr/bin/env python3
import itertools
import math
import sys
from splay_operation import Tree, Node
def flatten(tree):
"""Flatten given tree in ascending order."""
L, R, F = 0, 1, 2
node, stack, flattened = tree.root, [L], []
while node is not None:
if stack[-1] == L:
stack[-1] = R
if node.left is not None:
node = node.left
stack.append(L)
elif stack[-1] == R:
flattened.append(node.key)
stack[-1] = F
if node.right is not None:
node = node.right
stack.append(L)
else:
node = node.parent
stack.pop()
return flattened
def test_splay():
def deserialize_tree(string):
def deserialize_node(i):
assert string[i] == "("
i += 1
if string[i] == ")":
return i + 1, None
else:
comma = string.find(",", i)
comma2, left = deserialize_node(comma + 1)
rparen, right = deserialize_node(comma2 + 1)
assert string[rparen] == ")"
return rparen + 1, Node(int(string[i : comma]), left=left, right=right)
index, root = deserialize_node(0)
assert index == len(string)
return Tree(root)
def compare(system, gold):
if system is None and gold is not None:
return "expected node with key {}, found None".format(gold.key)
elif system is not None and gold is None:
return "expected None, found node with key {}".format(system.key)
elif system is not None and gold is not None:
if system.key != gold.key:
return "expected node with key {}, found {}".format(gold.key, system.key)
return compare(system.left, gold.left) or compare(system.right, gold.right)
with open("splay_tests.txt", "r") as splay_tests_file:
for line in splay_tests_file:
original_serialized, target_serialized, splayed_serialized = line.rstrip("\n").split()
original = deserialize_tree(original_serialized)
splayed = deserialize_tree(splayed_serialized)
target = int(target_serialized)
node = original.root
while node is not None and node.key != target:
if target < node.key: node = node.left
else: node = node.right
assert node is not None
original.splay(node)
error = compare(original.root, splayed.root)
assert not error, "Error running splay on key {} of {}: {}".format(node.key, original_serialized, error)
def test_lookup():
tree = Tree()
for elem in range(0, 100000, 2):
tree.insert(elem)
# Find non-existing
for elem in range(1, 100000, 2):
for _ in range(10):
assert tree.lookup(elem) is None, "Non-existing element was found"
# Find existing
for elem in range(0, 100000, 2):
for _ in range(10):