Exercise 2: Tracing movePage's Own Reorganization — Possible Solution ==================================================================== THE STARTING TREE ------------------------------ Before Step 7, java sits under general-purpose-languages, with fundamentals under java, and chapter-1 under fundamentals - the real five-level chain built in Step 3. WHAT THE RECURSIVE CTE FINDS ------------------------------ Per Chapter 10, calling movePage(javaPageId, archivePageId) runs the recursive CTE seeded at java's own id. The first recursive step finds fundamentals (java's direct child, depth 1); the second step finds chapter-1 (fundamentals' own child, and therefore java's grandchild, depth 2). The query returns both rows, ordered by depth ascending: fundamentals first, chapter-1 second. WHAT GETS UPDATED, AND IN WHAT ORDER ------------------------------ First, java itself is updated directly with its own new fullPath, computed from its new parent (archive) and its own unchanged slug: archive/java. This new path is stored in the recalculated Map under java's own id. Then the loop processes the two descendant rows in the CTE's own depth order: fundamentals first, using recalculated.get(java's id) - now archive/java - to build archive/java/fundamentals, storing that under fundamentals' own id in the Map. Then chapter-1, using recalculated.get(fundamentals' id) - now archive/java/fundamentals - to build archive/java/fundamentals/chapter-1. WHY THE ORDER MATTERS ------------------------------ If chapter-1 were processed before fundamentals, recalculated.get( fundamentals' id) would still hold fundamentals' own OLD path (or be missing entirely), producing a wrong or broken result for chapter-1. Processing strictly by depth - shallower descendants before deeper ones - guarantees every row's own parent has already been recalculated by the time that row is processed, since a node's depth in this CTE is always exactly one greater than its own parent's depth. WHY THIS WORKS AS AN ANSWER ------------------------------ It correctly identifies both real descendants rather than just one, correctly traces the exact new path values through the recalculated Map step by step, and correctly explains why depth- ascending order isn't arbitrary - it's the specific guarantee the algorithm depends on to be correct.