本文整理汇总了C++中SplayTree::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ SplayTree::insert方法的具体用法?C++ SplayTree::insert怎么用?C++ SplayTree::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplayTree
的用法示例。
在下文中一共展示了SplayTree::insert方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SplayInt
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;
}
示例2: test_remove
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);
}
示例3: main
int main(int argn, char *argc[])
{
int n = 1000;
unsigned int t = time(0);
int value;
if (argn > 1)
n = atoi(argc[1]);
if (argn > 2)
t = atoi(argc[2]);
srand(t);
cout << "testSplayTree " << n << " " << t << endl;
SplayTree<int> tree;
SplayTree<int>::Node *node;
int i;
cout << "Inserting " << n << " random values in treee ...\n";
unsigned int insCount = 0, delCount = 0;
for (i = 0; i < n; i++)
{
value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
node = tree.search(value);
if (node == NULL)
{
insCount++;
node = new SplayTree<int>::Node (value);
tree.insert(node);
}
}
cout << insCount << " Items inserted" << endl;
for (i = 0; i < n; i++)
{
value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
node = tree.remove(value);
if (node != NULL)
{
delCount++;
delete node;
}
}
cout << delCount << " Items removed" << endl;
destroyRec(tree.getRoot());
cout << "testSplayTree " << n << " " << t << endl;
}
示例4: main
int main(){
SplayTree<int> tree;
while(true){
cout << "\n1 - dodaj wartosc\n";
cout << "2 - znajdz wartosc\n";
cout << "3 - usun wartosc\n";
char c;
cin >> c;
if(c == '1'){
cout << "Podaj wartosc: ";
int i;
cin >> i;
tree.insert(i, i);
}
else if(c == '2'){
示例5: main
int main()
{
SplayTree T;
while(1)
{
char c;
cin >> c;
if(c == 'i')
{
int i;
cin >> i;
T.insert(1,i);
//T.print(T.root);
}
else
{
示例6: main
int main() {
int command, id, key;
while(scanf("%d", &command)) {
if (!command) {
break;
} else if (command == 1) {
scanf("%d%d", &id, &key);
tree.insert(Client(id, key));
} else if (command == 2) {
printf("%d\n", tree.pop_max().id);
} else if (command == 3) {
printf("%d\n", tree.pop_min().id);
}
}
return 0;
}
示例7: main
int main(){
long long int sum=0;
int N;
SplayTree<int> tr;
scanf("%d",&N);
for(int i=0;i<N;++i){
int j;
scanf("%d",&j);
for(;j>0;--j){
int n;
scanf("%d",&n);
tr.insert(n);
}
sum+=(tr.findMax()-tr.findMin());
tr.remove(tr.findMax());
tr.remove(tr.findMin());
}
cout<<sum<<endl;
}
示例8: test_iterator
void test_iterator(){
MyAllocator allocator;
allocator.constructor(1000);
SplayTree<int,int> a;
a.constructor(&allocator);
SplayTreeIterator<int,int> b;
b.constructor(&a);
b.hasNext();
b.hasNext();
b.hasNext();
b.next();
a.insert(1);
b.constructor(&a);
b.hasNext();
b.hasNext();
b.hasNext();
b.next();
b.hasNext();
b.hasNext();
}
示例9: main
int main(){
MyAllocator allocator;
allocator.constructor(1000);
SplayTree<int,int> lol;
lol.constructor(&allocator);
srand(time(NULL));
int n=10000000;
int k=n;
while(k--){
lol.insert(k);
}
int i=0;
SplayTreeIterator<int,int> iterator(&lol);
while(iterator.hasNext()){
SplayNode<int,int>*node=iterator.next();
int v=node->getKey();
i++;
}
lol.freeze();
assert(i==n);
assert(n==lol.size());
SplayTreeIterator<int,int> iterator2(&lol);
i=0;
while(iterator2.hasNext()){
SplayNode<int,int>*node=iterator2.next();
int v=node->getKey();
i++;
}
assert(i==n);
assert(n==lol.size());
test_remove();
test_iterator();
return 0;
}
示例10: main
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;
}
示例11: main
int main() {
SplayTree st;
vector<int> a;
set<int> marked;
int n = 7;
for (int i = 0; i < n; ++i) {
int t = rand() % n + 1;
// remove duplicate elements.
if (marked.count(t)) {
--i;
continue;
}
marked.insert(t);
a.push_back(t);
}
for (int i = 0; i < n; ++i) {
st.insert(a[i], true /*print*/);
}
for (int i = 0; i < n; ++i) {
st.remove(a[i], true /*print*/);
}
return 0;
}
示例12: TestSplayTree_String
int TestSplayTree_String()
{
SplayTree<std::string, std::string> *splaytree = new SplayTree<std::string, std::string>();
std::string strings[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 = std::string("testkey");
splaytree->insert(tmp, std::string("encapsulation test"));
tmp = std::string("");
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++)
{
char buf[20];
buf[0] = (char)0;
TEST_ASSERT(strlen(buf) == 0);
sprintf(buf, "%08x", i);
strings[i] = std::string(buf);
TEST_ASSERT(strings[i].length() > 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++)
{
std::string val;
val.clear();
TEST_ASSERT(val.length() == 0);
TEST_ASSERT(splaytree->exists(strings[i]));
TEST_ASSERT((val = splaytree->find<std::string>(strings[i], "")) != std::string(""));
TEST_ASSERT(val.length() > 0);
TEST_ASSERT(Compare(val, 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]));
}
/* And finally, clear the tree */
delete splaytree;
return 0;
}
示例13: TestSplayTree_CString
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;
}
示例14: TestSplayTree_Int
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;
}