Самый низкий общий предок двух листьев на дереве - PullRequest
1 голос
/ 30 марта 2020

У меня есть следующая древовидная структура:

struct Node {
   int data;
   Node* parent = nullptr;
}

Где каждый узел имеет не более одного родителя, но может иметь много детей. Я пытаюсь найти наименьшего общего предка двух узлов (node1 и node2), у которых нет дочерних элементов.

Это мой текущий код:

std::vector<Node*> ancestors1;
std::vector<Node*> ancestors2;
temp_node = node1->parent;
while(temp_node!=nullptr) {
    ancestors1.push_back(temp_node);
    temp_node = temp_node->parent;
}
temp_node = node2->parent;
while(temp_node!=nullptr) {
    ancestors2.push_back(temp_node);
    temp_node = temp_node->parent;
}
Node* common_ancestor = nullptr;
if (ancestors1.size() < ancestors2.size()) {
    ptrdiff_t t = ancestors1.end() - ancestors1.begin();
    std::vector<Node*>::iterator it1 = ancestors1.begin();
    std::vector<Node*>::iterator it2 = ancestors2.end() - t;
    while(it1!=ancestors1.end()) {
        if (*it1 == *it2) {
            common_ancestor = *it1;
        }
        ++it1;
    }
} else {
    ptrdiff_t t = ancestors2.end() - ancestors2.begin();
    std::vector<Node*>::iterator it2 = ancestors2.begin();
    std::vector<Node*>::iterator it1 = ancestors1.end() - t;
    while(it2!=ancestors2.end()) {
        if (*it1 == *it2) {
            common_ancestor = *it1;
        }
        ++it2;
    }
}
return common_ancestor

Этот код не всегда работаю, и я не знаю почему.

Ответы [ 3 ]

2 голосов
/ 30 марта 2020

Извините, я не смог устоять.

Помимо опечаток и ошибок, я считаю, что это может выглядеть еще проще:

#include <cassert>
#include <algorithm>
#include <iostream>
#include <vector>

struct Node {
  int data;
  Node *parent = nullptr;
};

Node* findCommonAncestor(Node *pNode1, Node *pNode2)
{
  // find paths of pNode1 and pNode2
  std::vector<Node*> path1, path2;
  for (; pNode1; pNode1 = pNode1->parent) path1.push_back(pNode1);
  for (; pNode2; pNode2 = pNode2->parent) path2.push_back(pNode2);
  // revert paths to make indexing simple
  std::reverse(path1.begin(), path1.end());
  std::reverse(path2.begin(), path2.end());
  // compare paths
  Node *pNode = nullptr; // ancestor
  size_t n = std::min(path1.size(), path2.size());
  for (size_t i = 0; i < n; ++i) {
    if (path1[i] == path2[i]) pNode = path1[i];
    else break;
  }
  // done
  return pNode;
}

int main()
{
  // sample tree:
  /*     1
   *     |
   *     2
   *    / \
   *   3   4
   *       |
   *       5
   */
  Node node1 = { 1, nullptr };
  Node node2 = { 2, &node1 };
  Node node3 = { 3, &node2 };
  Node node4 = { 4, &node2 };
  Node node5 = { 5, &node4 };
  Node *pNode = findCommonAncestor(&node3, &node5);
  if (pNode) {
    std::cout << "Lowest common ancestor: " << pNode->data << '\n';
  } else {
    std::cout << "No common ancestor found!\n";
  }
}

Вывод:

Lowest common ancestor: 2

Демонстрация в реальном времени на coliru

Примечание:

Хотя использование iterator s помогает сохранить общий код …

Я бы рассмотрел это как один из случаев, когда придерживаться простой старой индексации (иначе говоря, std::vector) индексация упрощает вещи.

0 голосов
/ 30 марта 2020

Вам не нужно больше места для решения этой проблемы в это время:

// measure depths
size_t depth1=0;
for (Node *n = node1; n; n=n->parent, ++depth1);
size_t depth2=0;
for (Node *n = node2; n; n=n->parent, ++depth2);

// move the deeper one up until they're the same depth
for (;depth1 > depth2; node1 = node1->parent, --depth1);
for (;depth2 > depth1; node2 = node2->parent, --depth2);

// move them both up until they match
while(node1 != node2) {
    node1 = node1->parent;
    node2 = node2->parent;
}

return node1;
0 голосов
/ 30 марта 2020

Я нашел проблему. В дополнение к необходимости перемещать оба итератора вместо одного (спасибо Jarod42 и v78), мне также нужно отключить l oop, как только я найду самого низкого общего предка (в противном случае он возвращает самого высокого общего предка) .

while(it1!=ancestors1.end()) {
        if (*it1 == *it2) {
            common_ancestor = *it1;
            break;
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...