-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathavltree.h
121 lines (101 loc) · 3.42 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//-< AVLTREE.H >-----------------------------------------------------*--------*
// POST++ Version 1.0 (c) 1998 GARRET * ? *
// (Persistent Object Storage) * /\| *
// * / \ *
// Created: 2-Feb-98 K.A. Knizhnik * / [] \ *
// Last update: 4-Feb-98 K.A. Knizhnik * GARRET *
//-------------------------------------------------------------------*--------*
// AVL-tree: example of POST++ class definition
//-------------------------------------------------------------------*--------*
#ifndef __AVLTREE_H__
#define __AVLTREE_H__
#include "object.h"
BEGIN_POST_NAMESPACE
class POST_DLL_ENTRY l2elem {
public:
l2elem* next;
l2elem* prev;
void link_after(l2elem* after) {
(next = after->next)->prev = this;
(prev = after)->next = this;
}
void link_before(l2elem* before) {
(prev = before->prev)->next = this;
(next = before)->prev = this;
}
void unlink() {
prev->next = next;
next->prev = prev;
}
void prune() {
next = prev = this;
}
bool empty() const {
return next == this;
};
CLASSINFO(l2elem, REF(next) REF(prev));
};
class POST_DLL_ENTRY avl_tree_key : public l2elem {
public:
//
// Returns negative if this key is less than key passed as parameter,
// positive if this key is greater, and 0 - if equal.
//
virtual int compare(avl_tree_key const* key) const = 0;
};
class POST_DLL_ENTRY avl_tree_node : public object {
public:
avl_tree_key* key;
avl_tree_node* left;
avl_tree_node* right;
int balance;
CLASSINFO(avl_tree_node, REF(key) REF(left) REF(right));
//
// This method returns true if height of branch increased after new
// key insertion. If there is a key with the same value in the tree,
// then new key is not inserted, and reference to the old key is
// returned through 'ins_key' parameter.
//
bool insert(avl_tree_node*& p, avl_tree_key*& ins_key);
//
// Method returns 1 if height of branch decreased,
// 0 if height not changed, -1 if key was not found in the tree
//
int remove(avl_tree_node*& p, avl_tree_key* del_key);
int remove(avl_tree_node*& p, avl_tree_node*& q);
int balance_left_branch(avl_tree_node*& p);
int balance_right_branch(avl_tree_node*& p);
avl_tree_node(avl_tree_key* new_key) {
key = new_key;
left = right = NULL;
balance = 0;
}
};
class POST_DLL_ENTRY avl_tree : public object, public l2elem {
protected:
avl_tree_node* root;
public:
//
// Insert key in AVL tree.
// If there is no such key in AVL tree then new key is inserted and
// returned as the result of the method, otherwise pointer to the key
// in AVL tree with the same value is returned (new key is not inserted).
//
avl_tree_key* insert(avl_tree_key* key);
//
// Remove key from AVL tree.
// Return false if there is no such key in AVL tree, true otherwise.
//
bool remove(avl_tree_key* key);
//
// Looks for a key with the same value in AVL tree.
//
avl_tree_key* search(avl_tree_key* key) const;
CLASSINFO(avl_tree, REF(root));
avl_tree(int) { // dummy parameter to distinguish from default constructor
prune();
root = NULL;
}
};
END_POST_NAMESPACE
#endif