Skip to curriculum
Jump to the editor

16.9. Find the matching node in an identical tree

Medium
Topics

This question is in

16. DOM: finding elements
15–25 min

Some code keeps two copies of the same tree: a live preview and an editable draft, the page and a clone made for printing, the real DOM and a copy rendered off screen. When the user clicks a node in one copy, you need the node at the same position in the other. Ids do not help (a clone repeats them, and most nodes have none), and content does not either (five identical list items look the same). The position does: the path of child indexes from the root down to the node.

Write findMatchingNode(rootA, rootB, nodeA) in matching-node.js.

Signature

JAVASCRIPT
function findMatchingNode(rootA, rootB, nodeA) // returns a Node or null
  • rootA, rootB: the roots of two trees with the same structure (for example, rootB is rootA.cloneNode(true)).
  • nodeA: a node inside rootA (an element, a text node or a comment), or rootA itself.
  • Returns the node of rootB at the same position: the same child index at every level. Returns null when nodeA is not inside rootA, or when rootB has no node at that position.

Requirements

  1. findMatchingNode(rootA, rootB, rootA) returns rootB.
  2. Elements at any depth, text nodes and comments are all found.
  3. Positions count every child node, including whitespace text nodes between elements and comments.
  4. Identical siblings are told apart by their position, never by their content, and ids are never used.
  5. The result is always a node of rootB (unless both roots are the same tree, when it is nodeA).
  6. A node that is not inside rootA (above it, outside it, or in rootB) gives null.
  7. If rootB is missing the node at that position, the result is null.
  8. Neither tree is changed.
  9. It works in or out of the page, with document fragments as roots, 3,000 levels deep and among 20,000 siblings.

Examples

JAVASCRIPT
const a = document.createElement("ul");
a.innerHTML = "<li>same</li><li>same</li><li>same <b>bold</b></li>";
const b = a.cloneNode(true);

findMatchingNode(a, b, a.children[1]);           // b.children[1]: the second <li> of b
findMatchingNode(a, b, a.querySelector("b").firstChild); // the text node "bold" in b
findMatchingNode(a, b, a);                       // b
JAVASCRIPT
findMatchingNode(a, b, document.body);           // null: not inside a
b.lastChild.remove();
findMatchingNode(a, b, a.lastChild);             // null: b has no third <li> now

Environment

  • Plain JavaScript in a browser page; the tests call the global findMatchingNode from matching-node.js.
  • Any DOM property is allowed. The point is to use positions: searching by id, by content or by selector gives wrong answers in the tests.

Hints

  1. How would you describe where a node is, using only numbers, so that the description works in another tree?
  2. Walking up from nodeA gives the levels from the bottom. In which order do you need them to walk down rootB?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…