Fix: check for presence of existing Trie node before adding

This commit is contained in:
Richard Wong 2024-03-17 02:54:05 +09:00
parent 078007cc6d
commit 69cdc85016
Signed by: richard
GPG Key ID: 5BD36BA2E9EE33D0
2 changed files with 19 additions and 8 deletions

View File

@ -50,8 +50,8 @@ vector<int> repeated_mutation(const ogdf::Graph &G, int k_max, trie* trie_store)
// compute_mps(G, dist(gen), temp_order, new_removed_size); // compute_mps(G, dist(gen), temp_order, new_removed_size);
// we set mutate_start_point to 0 to mutate everything every round // we set mutate_start_point to 0 to mutate everything every round
// temp_order = generate_mutated_post_order(G, temp_order, 0); temp_order = generate_mutated_post_order(G, temp_order, 1);
temp_order = generate_guided_post_order(G, temp_order); // temp_order = generate_guided_post_order(G, temp_order);
temp_order = generate_guided_post_order(G, temp_order); temp_order = generate_guided_post_order(G, temp_order);
// test if something is in the trie_store // test if something is in the trie_store
// returns false when no order is found in trie_store // returns false when no order is found in trie_store

View File

@ -29,11 +29,22 @@ void trie::add_new_post_order(vector<int> post_order, int current_position) {
if (current_index >= 0) { if (current_index >= 0) {
int node_id = post_order[current_index]; int node_id = post_order[current_index];
// only if it doesn't exist yet
// we want to check for existing node_id in the _next_node_list
// Assuming 'it' is an iterator pointing to an element in 'vec'
auto it = std::find_if(_next_node_list.begin(), _next_node_list.end(), [node_id](trie* obj) {
return obj->_node_id == node_id;
});
// only add if not found
if (it == _next_node_list.end()) {
// std::cout << "added trie node for: " << node_id << std::endl; // std::cout << "added trie node for: " << node_id << std::endl;
trie* new_trie_node = new trie(node_id); trie* new_trie_node = new trie(node_id);
new_trie_node->add_new_post_order(post_order, current_position+1); new_trie_node->add_new_post_order(post_order, current_position+1);
_next_node_list.push_back(new_trie_node); _next_node_list.push_back(new_trie_node);
// if found, then just continue
} else {
(*it)->add_new_post_order(post_order, current_position+1);
}
} }
} }
@ -51,7 +62,7 @@ bool trie::check_post_order(vector<int> post_order, int current_position) {
}); });
// found // found
if (it != _next_node_list.end()) { if (it != _next_node_list.end()) {
std::cout << (*it)->_node_id << std::endl; // std::cout << (*it)->_node_id << std::endl;
return_value = (*it)->check_post_order(post_order, current_position+1); return_value = (*it)->check_post_order(post_order, current_position+1);
// not found // not found
} else { } else {