-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorted_tree.hpp
68 lines (47 loc) · 1.47 KB
/
sorted_tree.hpp
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
#ifndef libnstd_sorted_tree_hpp
#define libnstd_sorted_tree_hpp
#include "tree_node.hpp"
#include <functional>
namespace nstd {
template <class T, class C = std::less<T>>
class sorted_tree_iterator {
tree_node_ptr<T> node;
public:
sorted_tree_iterator(tree_node_ptr<T>);
explicit operator bool()const;
};
template <class T, class C = std::less<T>>
class sorted_tree {
tree_node_ptr<T> root_node;
tree_node_ptr<T> find(T const&, tree_node_ptr<T>);
public:
sorted_tree_iterator<T> find(T const&);
};
}
template <class T, class C>
nstd::sorted_tree_iterator<T> nstd::sorted_tree<T, C>::find(T const& value) {
return nstd::sorted_tree_iterator<T>(find(value, root_node));
}
template <class T, class C>
nstd::tree_node_ptr<T>
nstd::sorted_tree<T, C>::find(T const& value,
nstd::tree_node_ptr<T> node) {
if (node) {
C compare;
if (compare(value, node->value)) {
return find(value, node->lnode);
} else if (compare(node->value, value)) {
return find(value, node->rnode);
}
}
return node;
}
template <class T, class C>
nstd::sorted_tree_iterator<T, C>::
sorted_tree_iterator(nstd::tree_node_ptr<T> n) : node(n) {
}
template <class T, class C>
nstd::sorted_tree_iterator<T, C>::operator bool()const {
return bool(node);
}
#endif