-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplayTree.h
78 lines (56 loc) · 1.59 KB
/
SplayTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef SPLAY_TREE_H
#define SPLAY_TREE_H
#include <iostream>
#include <vector>
using namespace std;
class SplayTree
{
public:
typedef struct Node {
int val;
int subtree_size;
struct Node* left;
struct Node* right;
struct Node* parent;
Node(int val, Node * p){
this->left = NULL;
this->right = NULL;
this->parent = p;
this->val = val;
this->subtree_size = 1;
}
} Node;
SplayTree();
~SplayTree() {
for(SplayTree::Node* a : SplayTree::nodes){
if(a) delete a;
}
delete &nodes;
}
Node * find_rank(int rank, Node * root);
int get_local_rank(Node * a);
int get_subtree_size(Node * e);
//Assumes no duplicate ranks
Node * insert(int rank, int val, Node * root);
Node * del(int rank, Node * root);
Node * del(Node * e);
void print_val(Node * root);
void print_rank(Node * root);
Node * max(Node * subroot);
Node * min(Node * subroot);
//Assumes trees disjoint
Node * join(Node * left, Node * right);
void split(int rank, Node * root, Node ** a, Node **b);
Node * splay(Node * e);
void delete_tree(Node * root);
private:
vector<Node *> nodes;
//Assume e is the child of a non-null node
void rotate_up(Node * e);
//Return e if it exists, otherwise return NULL
Node * no_splay_rank(Node * subroot, int rank);
Node * no_splay_insert(Node * subroot, int rank, int val, Node * p);
void print_subtree_rank(Node * subroot);
void print_subtree_val(Node * subroot);
};
#endif