-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.h
More file actions
114 lines (99 loc) · 3.2 KB
/
Set.h
File metadata and controls
114 lines (99 loc) · 3.2 KB
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
#include <iostream>
template <typename T>
class Set {
private:
struct Node {
T data;
Node* left;
Node* right;
Node(T value) : data(value), left(nullptr), right(nullptr) {}
};
Node* root;
// Helper function to insert a new element in the tree
Node* insert(Node* node, const T& value) {
if (node == nullptr) return new Node(value);
if (value < node->data)
node->left = insert(node->left, value);
else if (value > node->data)
node->right = insert(node->right, value);
// If value already exists, do nothing (since Set doesn't allow duplicates)
return node;
}
// Helper function to search for an element in the tree
bool contains(Node* node, const T& value) const {
if (node == nullptr) return false;
if (value == node->data) return true;
if (value < node->data) return contains(node->left, value);
return contains(node->right, value);
}
// Helper function to find the minimum node in a subtree
Node* findMin(Node* node) {
while (node && node->left != nullptr) {
node = node->left;
}
return node;
}
// Helper function to delete an element from the tree
Node* remove(Node* node, const T& value) {
if (node == nullptr) return node;
if (value < node->data) {
node->left = remove(node->left, value);
} else if (value > node->data) {
node->right = remove(node->right, value);
} else {
// Node with only one child or no child
if (node->left == nullptr) {
Node* temp = node->right;
delete node;
return temp;
} else if (node->right == nullptr) {
Node* temp = node->left;
delete node;
return temp;
}
// Node with two children: Get the in-order successor
Node* temp = findMin(node->right);
node->data = temp->data;
node->right = remove(node->right, temp->data);
}
return node;
}
// Helper function to print elements (in-order traversal)
void inorderPrint(Node* node) const {
if (node == nullptr) return;
inorderPrint(node->left);
std::cout << node->data << " ";
inorderPrint(node->right);
}
// Helper function to deallocate nodes in destructor
void clear(Node* node) {
if (node == nullptr) return;
clear(node->left);
clear(node->right);
delete node;
}
public:
// Constructor
Set() : root(nullptr) {}
// Destructor
~Set() {
clear(root);
}
// Public interface to insert an element
void insert(const T& value) {
root = insert(root, value);
}
// Public interface to check if an element exists
bool contains(const T& value) const {
return contains(root, value);
}
// Public interface to remove an element
void remove(const T& value) {
root = remove(root, value);
}
// Public interface to print all elements
void print() const {
inorderPrint(root);
std::cout << std::endl;
}
};