Created
November 2, 2019 15:33
-
-
Save simondegheselle/cffce3568563f28f05097e3fdd8eb3b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Created by simondegheselle on 11/2/19. | |
| // | |
| #ifndef SPLAY_TREES_TOP_DOWN_SPLAY_TREE_H | |
| #define SPLAY_TREES_TOP_DOWN_SPLAY_TREE_H | |
| #include <cassert> | |
| #include "search_tree.h" | |
| template <class Key, class Data> | |
| class TopDownSplayTree : public SearchTree<Key, Data> { | |
| public: | |
| using SearchTree<Key, Data>::SearchTree; | |
| void zoek(const Key &sleutel, SearchNode<Key, Data> *&ouder, SearchTree<Key, Data> *&plaats) override; | |
| }; | |
| template<class Key, class Data> | |
| void | |
| TopDownSplayTree<Key, Data>::zoek(const Key &key, SearchNode<Key, Data> *&ouder, SearchTree<Key, Data> *&plaats) { | |
| assert(*this); | |
| SearchTree<Key, Data> L; | |
| SearchTree<Key, Data> R; | |
| SearchTree<Key, Data>* maximumL = &L; | |
| SearchTree<Key, Data>* minimumR = &R; | |
| SearchTree<Key, Data> * M = this; | |
| bool first_left = key < (*M)->sleutel; | |
| bool second_left = (*M)->geefKind(first_left) && key <(*M)->geefKind(first_left)->sleutel; | |
| while ((*M)->sleutel != key && (*M)->geefKind(first_left)) { | |
| if (first_left == second_left && (*M)->geefKind(first_left)) { | |
| M->rotate(first_left); | |
| } | |
| auto tmp = move(*M); | |
| *M = move(tmp->geefKind(first_left)); | |
| if (first_left) { | |
| *minimumR = move(tmp); | |
| } else { | |
| *maximumL = move(tmp); | |
| } | |
| while ((*minimumR) && (*minimumR)->links) { | |
| minimumR = &(*minimumR)->links; | |
| } | |
| while ((*maximumL) && (*maximumL)->rechts) { | |
| maximumL = &(*maximumL)->rechts; | |
| } | |
| first_left = key < (*M)->sleutel; | |
| second_left = (*M)->geefKind(first_left) && key <(*M)->geefKind(first_left)->sleutel; | |
| } | |
| (*M)->links = move(L); | |
| (*M)->rechts = move(R); | |
| *this = move(*M); | |
| } | |
| #endif //SPLAY_TREES_TOP_DOWN_SPLAY_TREE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment