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);
}
Exemple #2
0
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;
}
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;
}
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;
}
Exemple #5
0
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;
}
Exemple #6
0
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;
}
Exemple #7
0
int main(int argc, char** argv)
{

    if (argc != 2) {
        cerr << "Provide the music file to process as an argument to the program." << endl;
        cerr << "Usage: " << argv[0] << " <filename>" << endl;
        exit(1);
    }
    
    // key --> artist name (string)
    // value --> Array of songs (array of strings)
    SplayTree<string, Array<string> > splayTree;
  
    ifstream ifs(argv[1]);
    string line;
    while(getline(ifs, line)) {
        string artist, musicEntry;
        processInput(line, artist, musicEntry);
        insertWithGrouping(splayTree, artist, musicEntry);
    }

    // Dump the initial splay tree
    dumpSplayTree(splayTree, "splay_tree0.dot");

    int count = 0;
    // Insertion into the splay tree is done
    // Now, let's go through the motions of inserting, deleting or finding
    while (true) {
        int input;
        ostringstream os;
        os << "splay_tree" << ++count << ".dot";
        string filename = os.str();
        cout << "Input choices:" << endl;
        cout << "* Insert (1)" << endl;
        cout << "* Find (2)" << endl;
        cout << "* Remove (3)" << endl;
        cout << "* Print sorted (4)" << endl;
        cout << "* Exit(0)" << endl;
        cin >> input;
        cin.ignore(256, '\n');
        switch(input) {
          case 1: {
              string artist, musicEntry;
              cout << "Artist: ";
              getline(cin, artist);
              cout << "Music: ";
              getline(cin, musicEntry);
              insertWithGrouping(splayTree, artist, musicEntry);
              dumpSplayTree(splayTree, filename);
              cout << "Done." << endl;
              break; 
          }
          case 2: {
              cout << "Find what (Enter artist name): ";
              string artist;
              getline(cin, artist);
              BSTNode<string, Array<string> >* approxMatchNode_lb = nullptr;
              BSTNode<string, Array<string> >* approxMatchNode_ub = nullptr;
              BSTNode<string, Array<string> >* node = 
                  splayTree.findApprox(artist, approxMatchNode_lb, approxMatchNode_ub);
              dumpSplayTree(splayTree, filename);
              if (node != nullptr) {
                  cout << "Accurately matched!" << endl;
              } else {
                  cout << "Approximate matches: " << endl;
                  if (approxMatchNode_lb) {
                      cout << "Artist: " << approxMatchNode_lb->key() << endl;
                      cout << "Songs: " << approxMatchNode_lb->value() << endl;
                  }
                  if (approxMatchNode_ub) {
                      cout << "Artist: " << approxMatchNode_ub->key() << endl;
                      cout << "Songs: " << approxMatchNode_ub->value() << endl;
                  }
              }
              break;
          }
          case 3: {
              cout << "Remove what (Enter artist name): ";
              string artist;
              getline(cin, artist);
              splayTree.remove(artist);
              dumpSplayTree(splayTree, filename);
              cout << "Done." << endl;
              break; 
          }
          case 4: {
              cout << "Sorted List: " << endl;
              splayTree.inorder();
              cout << "Done." << endl;
              break; 
          }
          case 0: {
              cout << "Exiting..." << endl;
              return 0;
          }
          default: {
              cout << "Invalid entry: Try again." << endl;
              break;
          }
        }
    }

    return 0;
}