-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.h
54 lines (47 loc) · 1.45 KB
/
AVLTree.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
#ifndef AVLTREE_H_INCLUDED
#define AVLTREE_H_INCLUDED
#include "header.h"
template <class Key,class Comp=less<Key>>
struct AVLNode
{
Key key;
int height;
int size;
AVLNode *left,*right,*parent;
AVLNode ();
AVLNode(Key);
~AVLNode();
static int getHeight(AVLNode *);
static int getBalance(AVLNode *);
static int getSize(AVLNode *);
void updateHeight();
void updateSize();
static AVLNode* rightRotate(AVLNode *node);
static AVLNode* leftRotate(AVLNode *node);
};
template <class Key,class Comp=less<Key>>
struct AVLTree
{
AVLNode<Key,Comp> *root;
AVLTree();
~AVLTree();
AVLNode<Key,Comp> * insert(Key);
int size();
void erase(Key);
AVLNode<Key,Comp> *min();
AVLNode<Key,Comp> *max();
AVLNode<Key,Comp> *find(Key);
AVLNode<Key,Comp>* succ(AVLNode<Key,Comp> *node);
AVLNode<Key,Comp>* pred(AVLNode<Key,Comp> *node);
AVLNode<Key,Comp>* nth(int index);
private:
int m_size;
AVLNode<Key,Comp> * lastInsert=NULL;
AVLNode<Key,Comp> * _insert(AVLNode<Key,Comp> *root,Key key);
AVLNode<Key,Comp>* _delete(AVLNode<Key,Comp> *root,Key key);
AVLNode<Key,Comp> *_min(AVLNode<Key,Comp> *root);
AVLNode<Key,Comp> *_max(AVLNode<Key,Comp> *root);
AVLNode<Key,Comp>* _find(AVLNode<Key,Comp> *root,Key key);
AVLNode<Key,Comp>* _find_nth(AVLNode<Key,Comp> *root,int index);
};
#endif // AVLTREE_H_INCLUDED