本文整理汇总了C++中SplayTree类的典型用法代码示例。如果您正苦于以下问题:C++ SplayTree类的具体用法?C++ SplayTree怎么用?C++ SplayTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SplayTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: splay_tree
void splay_tree()
{
SplayTree<int> splayTree;
#define SPLAY_LOOP for (int i = 0; i < 20; i ++)
SPLAY_LOOP
{
splayTree.Insert(i);
}
SPLAY_LOOP
{
assert(splayTree.Exists(i));
}
SPLAY_LOOP
{
splayTree.Delete(i);
}
SPLAY_LOOP
{
assert(!splayTree.Exists(i));
}
}
示例2: dp
void dp()
{
int i,j,k;
ST st(start,0);
tree.Insert(st);
for (i=0;i<n;i++)
{
int lmin=maxl,rmin=maxl;
SplayTree<ST>::node *p;
while (1)
{
tree.Splay(tree.root,ST(a[i].l,0));
p=tree.root;
if (p==tree.nullnode||p->key>ST(a[i].r,0)) break;
lmin=Min(lmin,Abs(p->key.key-a[i].l)+p->key.data);
rmin=Min(rmin,Abs(a[i].r-p->key.key)+p->key.data);
tree.Delete(p->key);
}
tree.Insert(ST(a[i].l,lmin));
tree.Insert(ST(a[i].r,rmin));
}
int res=maxl;
int tot=0;
while (tree.root !=tree.nullnode)
{
tot++;
res=Min(res,tree.root->key.data+Abs(tree.root->key.key));
tree.Delete(tree.root->key);
}
cout<<tot<<endl;
printf("%d\n",res);
}
示例3: _liftUpToRoot
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;
}
示例4: 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;
}
示例5: SplayNodeRefresh
void SplayNodeRefresh(SplayTree splay, Tree node)
{
Compare cmp;
AVERT(SplayTree, splay);
AVERT(Tree, node);
AVER(!SplayTreeIsEmpty(splay)); /* must contain node, at least */
cmp = SplaySplay(splay, splay->nodeKey(node), splay->compare);
AVER(cmp == CompareEQUAL);
AVER(SplayTreeRoot(splay) == node);
splay->updateNode(splay, node);
}
示例6: 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'){
示例7: 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
{
示例8: SplayFindFirst
Bool SplayFindFirst(Tree *nodeReturn, SplayTree splay,
SplayTestNodeFunction testNode,
SplayTestTreeFunction testTree,
void *closureP, Size closureS)
{
SplayFindClosureStruct closureStruct;
Bool found;
AVER(nodeReturn != NULL);
AVERT(SplayTree, splay);
AVER(FUNCHECK(testNode));
AVER(FUNCHECK(testTree));
if (SplayTreeIsEmpty(splay) ||
!testTree(splay, SplayTreeRoot(splay), closureP, closureS))
return FALSE; /* no suitable nodes in tree */
closureStruct.p = closureP;
closureStruct.s = closureS;
closureStruct.testNode = testNode;
closureStruct.testTree = testTree;
closureStruct.splay = splay;
closureStruct.found = FALSE;
found = SplaySplay(splay, &closureStruct,
SplayFindFirstCompare) == CompareEQUAL &&
closureStruct.found;
while (!found) {
Tree oldRoot, newRoot;
/* FIXME: Rename to "seen" and "not yet seen" or something. */
oldRoot = SplayTreeRoot(splay);
newRoot = TreeRight(oldRoot);
if (newRoot == TreeEMPTY || !(*testTree)(splay, newRoot, closureP, closureS))
return FALSE; /* no suitable nodes in the rest of the tree */
/* Temporarily chop off the left half-tree, inclusive of root,
so that the search excludes any nodes we've seen already. */
SplayTreeSetRoot(splay, newRoot);
TreeSetRight(oldRoot, TreeEMPTY);
found = SplaySplay(splay, &closureStruct,
SplayFindFirstCompare) == CompareEQUAL &&
closureStruct.found;
/* Restore the left tree, then rotate left so that the node we
just splayed is at the root. Update both. */
newRoot = SplayTreeRoot(splay);
TreeSetRight(oldRoot, newRoot);
SplayTreeSetRoot(splay, oldRoot);
TreeRotateLeft(&splay->root);
splay->updateNode(splay, oldRoot);
splay->updateNode(splay, newRoot);
}
*nodeReturn = SplayTreeRoot(splay);
return TRUE;
}
示例9: accept_tree
void accept_tree(){
int n;
SplayTree st;
cout<<"Create splay tree: (enter 0 to finish)\n";
while(1) {
fflush(stdin);
cout<<"\nEnter node value: ";
cin>>n;
if(n==0) {
break;
}
root =st.Insert(n,root);
}
root = st.Delete(4, root);
}
示例10: SplayTreeSuccessor
static Tree SplayTreeSuccessor(SplayTree splay)
{
Tree oldRoot, newRoot;
AVERT(SplayTree, splay);
AVER(!SplayTreeIsEmpty(splay));
oldRoot = SplayTreeRoot(splay);
if (!TreeHasRight(oldRoot))
return TreeEMPTY; /* No successor */
/* temporarily chop off the left half-tree, inclusive of root */
SplayTreeSetRoot(splay, TreeRight(oldRoot));
TreeSetRight(oldRoot, TreeEMPTY);
(void)SplaySplay(splay, NULL, compareLess);
newRoot = SplayTreeRoot(splay);
AVER(newRoot != TreeEMPTY);
AVER(TreeLeft(newRoot) == TreeEMPTY);
TreeSetLeft(newRoot, oldRoot);
splay->updateNode(splay, oldRoot);
splay->updateNode(splay, newRoot);
return newRoot;
}
示例11: insertWithGrouping
void insertWithGrouping(SplayTree<string, Array<string> >& aSplayTree,
string aArtist,
string aMusicEntry)
{
static int debug_count = 0;
ostringstream os;
os << "debug" << debug_count++ << ".dot";
string filename = os.str();
// Here aArtist is the key and aMusicEntry contributes toward the value
BSTNode<string, Array<string> >* place = aSplayTree.findInsertionPoint(aArtist);
if (place == nullptr) {
// First node to be inserted
Array<string> musicList;
musicList.add(aMusicEntry);
aSplayTree.setRoot(new BSTNode<string, Array<string> >(aArtist, musicList));
} else if (aArtist == place->key()) {
// Key matched
// aArtist exists -- just group the current aMusicEntry with the existing
// associated musicList
Array<string> musicList = place->value();
// Aggregation
musicList.add(aMusicEntry);
// Replace the value with the updated musicList
place->setValue(musicList);
// Splay this node as it is just augmented
aSplayTree.splay(place);
} else {
// Key not matched
// So, this is going to be the first music entry for aArtist
Array<string> musicList;
musicList.add(aMusicEntry);
BSTNode<string, Array<string> >* newNode =
new BSTNode<string, Array<string> >(aArtist, musicList,
nullptr, nullptr, place);
if (aArtist < place->key()) {
// Node on the left of place
place->setLeft(newNode);
} else {
// Node on the right of place
place->setRight(newNode);
}
// dumpSplayTree(aSplayTree, filename);
aSplayTree.splay(newNode);
}
}
示例12: SplayNodeInit
void SplayNodeInit(SplayTree splay, Tree node)
{
AVERT(SplayTree, splay);
AVERT(Tree, node);
AVER(!TreeHasLeft(node)); /* otherwise, call SplayNodeRefresh */
AVER(!TreeHasRight(node)); /* otherwise, call SplayNodeRefresh */
splay->updateNode(splay, node);
}
示例13: SplayFindLast
Bool SplayFindLast(Tree *nodeReturn, SplayTree splay,
SplayTestNodeFunction testNode,
SplayTestTreeFunction testTree,
void *testClosure)
{
SplayFindClosureStruct closureStruct;
Bool found;
AVER_CRITICAL(nodeReturn != NULL);
AVERT_CRITICAL(SplayTree, splay);
AVER_CRITICAL(FUNCHECK(testNode));
AVER_CRITICAL(FUNCHECK(testTree));
if (SplayTreeIsEmpty(splay) ||
!testTree(splay, SplayTreeRoot(splay), testClosure))
return FALSE; /* no suitable nodes in tree */
closureStruct.testClosure = testClosure;
closureStruct.testNode = testNode;
closureStruct.testTree = testTree;
closureStruct.splay = splay;
found = SplaySplay(splay, &closureStruct,
SplayFindLastCompare) == CompareEQUAL &&
closureStruct.found;
while (!found) {
Tree oldRoot, newRoot;
oldRoot = SplayTreeRoot(splay);
newRoot = TreeLeft(oldRoot);
if (newRoot == TreeEMPTY || !(*testTree)(splay, newRoot, testClosure))
return FALSE; /* no suitable nodes in the rest of the tree */
/* Temporarily chop off the right half-tree, inclusive of root,
so that the search excludes any nodes we've seen already. */
SplayTreeSetRoot(splay, newRoot);
TreeSetLeft(oldRoot, TreeEMPTY);
found = SplaySplay(splay, &closureStruct,
SplayFindLastCompare) == CompareEQUAL &&
closureStruct.found;
/* Restore the right tree, then rotate right so that the node we
just splayed is at the root. Update both. */
newRoot = SplayTreeRoot(splay);
TreeSetLeft(oldRoot, newRoot);
SplayTreeSetRoot(splay, oldRoot);
TreeRotateRight(&splay->root);
splay->updateNode(splay, oldRoot);
splay->updateNode(splay, newRoot);
}
*nodeReturn = SplayTreeRoot(splay);
return TRUE;
}
示例14: SplayDebugUpdate
void SplayDebugUpdate(SplayTree splay, Tree tree)
{
AVERT(SplayTree, splay);
AVERT(Tree, tree);
if (tree == TreeEMPTY)
return;
SplayDebugUpdate(splay, TreeLeft(tree));
SplayDebugUpdate(splay, TreeRight(tree));
splay->updateNode(splay, tree);
}
示例15: SplayTreeInsert
Bool SplayTreeInsert(SplayTree splay, Tree node)
{
Tree neighbour;
AVERT(SplayTree, splay);
AVERT(Tree, node);
AVER(TreeLeft(node) == TreeEMPTY);
AVER(TreeRight(node) == TreeEMPTY);
if (SplayTreeIsEmpty(splay)) {
SplayTreeSetRoot(splay, node);
return TRUE;
}
switch (SplaySplay(splay, splay->nodeKey(node), splay->compare)) {
default:
NOTREACHED;
/* fall through */
case CompareEQUAL: /* duplicate node */
return FALSE;
case CompareGREATER: /* left neighbour is at root */
neighbour = SplayTreeRoot(splay);
SplayTreeSetRoot(splay, node);
TreeSetRight(node, TreeRight(neighbour));
TreeSetLeft(node, neighbour);
TreeSetRight(neighbour, TreeEMPTY);
break;
case CompareLESS: /* right neighbour is at root */
neighbour = SplayTreeRoot(splay);
SplayTreeSetRoot(splay, node);
TreeSetLeft(node, TreeLeft(neighbour));
TreeSetRight(node, neighbour);
TreeSetLeft(neighbour, TreeEMPTY);
break;
}
splay->updateNode(splay, neighbour);
splay->updateNode(splay, node);
return TRUE;
}