-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.hpp
145 lines (132 loc) · 2.87 KB
/
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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef TREE_HPP
#define TREE_HPP
#include <algorithm>
#include <iostream>
template <typename type>
void print_(type x)
{
std::cout << x << " ";
}
template <typename node, typename type>
class tree
{
node *root;
public:
tree() { this->root = NULL; }
tree(node *root) {
this->root = root;
}
~tree();
void add(type e);
void add(type a[], size_t size);
void remove(type e);
void remove(type a[], size_t size);
bool check(type e) const;
void copyToArrIncOrder(type [], size_t &) const;
void copyToArrDecOrder(type [], size_t &) const;
void print() const {
if (this->root) this->root->inOrderInc(print_, this->root->Data());
}
size_t rootHeight() const {
return this->root ? this->root->Height() : 0;
}
node *Root() const { return this->root; }
bool isEmpty() const { return !this->root; }
void updateHeight() {
if (this->root) this->root->updateHeight();
}
void display() const {
if (this->root) this->root->display();
}
};
template <typename node, typename type>
void tree<node, type>::copyToArrIncOrder(type A[], size_t &s) const
{
if (this->root)
this->root->copyToArrIncOrder(A, s);
else
s = 0;
}
template <typename node, typename type>
void tree<node, type>::copyToArrDecOrder(type A[], size_t &s) const
{
if (this->root)
this->root->copyToArrDecOrder(A, s);
else
s = 0;
}
template <typename node, typename type>
void tree<node, type>::add(type e)
{
try
{
this->root->add(&this->root, e);
}
catch (const char *c)
{
std::cerr << "Error : Unable to add " << e << " in the tree\n";
std::cerr << "Recieved: " << c << "\n";
}
}
template <typename node, typename type>
void tree<node, type>::add(type a[], size_t size)
{
for (size_t i = 0; i < size; ++i)
try
{
this->root->add(&this->root, a[i]);
}
catch (const char *c)
{
std::cerr << "Error : Unable to add " << a[i] << " in the tree\n";
std::cerr << "Recieved: " << c << "\n";
}
}
template <typename node, typename type>
void tree<node, type>::remove(type e)
{
try
{
this->root->remove(&this->root, e);
}
catch (const char *c)
{
std::cerr << "Error : Unable to remove [ " << e << " ] from the tree\n";
std::cerr << "Recieved: " << c << "\n";
exit(1);
}
}
template <typename node, typename type>
void tree<node, type>::remove(type a[], size_t size)
{
for (size_t i = 0; i < size; ++i)
try
{
this->root->remove(&this->root, a[i]);
}
catch (const char *c)
{
std::cerr << "Error : Unable to remove [ " << a[i] << " ]\n";
std::cerr << "Recieved: " << c << "\n";
this->root->display();
exit(1);
}
}
template <typename node, typename type>
tree<node, type>::~tree()
{
if (this->root)
this->root->removeAll(&this->root);
}
template <typename node, typename type>
bool tree<node, type>::check(type e) const
{
node *t = this->root;
while (t)
if (t->data == e)
return true;
else
t = t->data > e ? t->left : t->right;
return false;
}
#endif