void test_remove(){ MyAllocator allocator; allocator.constructor(1000); SplayTree<int,int> a; a.constructor(&allocator); for(int k=0;k<100;k++){ a.insert(k); assert(a.find(k)!=NULL); } assert(a.size()==100); a.remove(9); assert(a.size()==99); assert(a.find(9)==NULL); a.remove(50); assert(a.size()==98); assert(a.find(50)==NULL); a.remove(50); assert(a.size()==98); assert(a.find(50)==NULL); a.remove(-1); assert(a.size()==98); }
int main() { SplayTree<SplayInt, SplayInt> tree; MOZ_RELEASE_ASSERT(tree.empty()); MOZ_RELEASE_ASSERT(!tree.find(SplayInt(0))); static const int N = mozilla::ArrayLength(gValues); // Insert the values, and check each one is findable just after insertion. for (int i = 0; i < N; i++) { tree.insert(new SplayInt(gValues[i])); MOZ_RELEASE_ASSERT(tree.find(SplayInt(gValues[i]))); tree.checkCoherency(); } // Check they're all findable after all insertions. for (int i = 0; i < N; i++) { MOZ_RELEASE_ASSERT(tree.find(SplayInt(gValues[i]))); tree.checkCoherency(); } // Check that non-inserted values cannot be found. MOZ_RELEASE_ASSERT(!tree.find(SplayInt(-1))); MOZ_RELEASE_ASSERT(!tree.find(SplayInt(N))); MOZ_RELEASE_ASSERT(!tree.find(SplayInt(0x7fffffff))); // Remove the values, and check each one is not findable just after removal. for (int i = 0; i < N; i++) { SplayInt* removed = tree.remove(SplayInt(gValues[i])); MOZ_RELEASE_ASSERT(removed->mValue == gValues[i]); MOZ_RELEASE_ASSERT(!tree.find(*removed)); delete removed; tree.checkCoherency(); } MOZ_RELEASE_ASSERT(tree.empty()); // Reinsert the values, in reverse order to last time. for (int i = 0; i < N; i++) { tree.insert(new SplayInt(gValues[N - i - 1])); tree.checkCoherency(); } // Remove the minimum value repeatedly. for (int i = 0; i < N; i++) { SplayInt* removed = tree.removeMin(); MOZ_RELEASE_ASSERT(removed->mValue == i); delete removed; tree.checkCoherency(); } MOZ_RELEASE_ASSERT(tree.empty()); return 0; }
Node* LinkCutTree::_cutout(Node* vertex) { _liftUpToRoot(vertex); std::pair<SplayTree*, SplayTree*> splitedTrees = SplayTree::split(vertex->treePtr, Node::getSize(vertex->leftChild) + 1); SplayTree* right = splitedTrees.second; if(right->getRoot()) { right->find(0)->link = vertex; } else { delete right; } return vertex; }
int main() { int n, v; char cmd[32]; while (scanf("%d", &n) != EOF) { SplayTree splay; for (int op = 0; op < n; ++op) { scanf("%s", cmd); if (!strcmp(cmd, "show")) { splay.show(); } else if (!strcmp(cmd, "insert")) { scanf("%d", &v); splay.insert(v); } else if (!strcmp(cmd, "find")) { scanf("%d", &v); SplayTreeNode *ret = splay.find(v); printf("Find %d : %x\n", v, (unsigned)ret); } else if (!strcmp(cmd, "remove")) { scanf("%d", &v); splay.remove(v); } } } return 0; }
int TestSplayTree_CString() { SplayTree<const char *, const char *> *splaytree = new SplayTree<const char *, const char *>(); char *strings[TREE_ITEMS], *tmp; /* Make sure allocation worked */ TEST_ASSERT(splaytree != NULL); /* Make sure the size starts at 0 */ TEST_ASSERT(splaytree->size() == 0); memset(strings, 0, sizeof(strings)); /* Make sure the tree encapsulates keys properly */ tmp = cc_strdup("testkey"); splaytree->insert(tmp, "encapsulation test"); free(tmp); tmp = NULL; TEST_ASSERT(splaytree->size() == 1); TEST_ASSERT(splaytree->exists("testkey")); TEST_ASSERT(splaytree->erase("testkey")); TEST_ASSERT(!splaytree->exists("testkey")); TEST_ASSERT(!splaytree->erase("testkey")); TEST_ASSERT(splaytree->size() == 0); /* Simplest sanity checks done, now create some random data */ for (unsigned int i = 0; i < TREE_ITEMS; i++) { TEST_ASSERT(strings[i] == NULL); strings[i] = new char[20]; TEST_ASSERT(strings[i] != NULL); memset(strings[i], 0, 20); TEST_ASSERT(strlen(strings[i]) == 0); sprintf(strings[i], "%08x", i); TEST_ASSERT(strlen(strings[i]) > 0); } /* Fill the tree */ for (size_t i = 0; i < TREE_ITEMS; i++) { TEST_ASSERT(splaytree->insert(strings[i], strings[(TREE_ITEMS - 1) - i])); } /* Verify existence of all the added data */ for (size_t i = 0; i < TREE_ITEMS; i++) { const char *val = NULL; TEST_ASSERT(splaytree->exists(strings[i])); TEST_ASSERT((val = splaytree->find(strings[i])) != NULL); TEST_ASSERT(Compare(val, (const char *)strings[(TREE_ITEMS - 1) - i]) == 0); } /* Verify existence of all the added data, in a different order */ for (size_t i = TREE_ITEMS - 1; i < TREE_ITEMS; i--) { TEST_ASSERT(splaytree->exists(strings[i])); } /* Try to remove all the data */ for (size_t i = 0; i < TREE_ITEMS; i++) { TEST_ASSERT(splaytree->erase(strings[i])); TEST_ASSERT(!splaytree->exists(strings[i])); } /* Clean up the random data */ for (size_t i = 0; i < TREE_ITEMS; i++) { delete [] strings[i]; strings[i] = NULL; } /* And finally, clear the tree */ delete splaytree; return 0; }
int TestSplayTree_Int() { SplayTree<int, int> *splaytree = new SplayTree<int, int>(); int data[TREE_ITEMS], tmp; /* Make sure allocation worked */ TEST_ASSERT(splaytree != NULL); /* Make sure the size starts at 0 */ TEST_ASSERT(splaytree->size() == 0); /* Make sure the tree encapsulates keys properly */ tmp = 256; splaytree->insert(tmp, RandomNumber()); tmp = 0; TEST_ASSERT(splaytree->size() == 1); TEST_ASSERT(splaytree->exists(256)); TEST_ASSERT(splaytree->erase(256)); TEST_ASSERT(!splaytree->exists(256)); TEST_ASSERT(!splaytree->erase(256)); TEST_ASSERT(splaytree->size() == 0); /* Simplest sanity checks done, now create some random data */ for (size_t i = 0; i < TREE_ITEMS; i++) { data[i] = i; } /* Fill the tree */ for (size_t i = 0; i < TREE_ITEMS; i++) { TEST_ASSERT(splaytree->insert(data[i], data[TREE_ITEMS - 1 - i])); } /* Verify existence of all the added data */ for (size_t i = 0; i < TREE_ITEMS; i++) { int val; TEST_ASSERT(splaytree->exists(data[i])); TEST_ASSERT((val = splaytree->find(data[i], -1)) != -1); TEST_ASSERT(Compare(val, data[TREE_ITEMS - 1 - i]) == 0); } /* Verify existence of all the added data, in a different order */ for (size_t i = TREE_ITEMS - 1; i < TREE_ITEMS; i--) { TEST_ASSERT(splaytree->exists(data[i])); } /* Try to remove all the data */ for (size_t i = 0; i < TREE_ITEMS; i++) { TEST_ASSERT(splaytree->erase(data[i])); TEST_ASSERT(!splaytree->exists(data[i])); } /* And finally, clear the tree */ delete splaytree; return 0; }