Skip to content

Commit 46c3988

Browse files
Add find_common_ancestor() method to a_star class for ancestor retrieval
1 parent f9b64be commit 46c3988

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

include/a_star.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ namespace utils
141141
virtual void current_node(const node<Tp> &c_node) {}
142142
#endif
143143

144+
protected:
145+
const node<Tp> &find_common_ancestor(const node &a, const node &b) const
146+
{
147+
std::unordered_set<const node<Tp> *> ancestors;
148+
auto c_a = &a;
149+
while (c_a)
150+
{
151+
ancestors.insert(c_a);
152+
c_a = c_a->get_parent() ? c_a->get_parent().get() : nullptr;
153+
}
154+
auto c_b = &b;
155+
while (c_b)
156+
{
157+
if (ancestors.count(c_b))
158+
return *c_b;
159+
c_b = c_b->get_parent() ? c_b->get_parent().get() : nullptr;
160+
}
161+
throw std::runtime_error("No common ancestor found");
162+
}
163+
144164
private:
145165
std::priority_queue<std::shared_ptr<node<Tp>>, std::vector<std::shared_ptr<node<Tp>>>, node_cmp> open_list;
146166
std::unordered_set<std::shared_ptr<const node<Tp>>> closed_list;

0 commit comments

Comments
 (0)