function repairTree() { $tbl_mdb_names = claro_sql_get_main_tbl(); $tbl_category = $tbl_mdb_names['category']; // get list of all node $sql = " SELECT code, code_P, treePos, name, nb_childs\n FROM `" . $tbl_category . "`\n ORDER BY `treePos`"; $catList = claro_sql_query_fetch_all($sql); $newTreePos = 1; $listSize = count($catList); // foreach node check code_parent, treepos and nbchilds foreach ($catList as $cat) { $newCatList[$cat['code']] = $cat; $parentCatData = get_cat_data(get_cat_id_from_code($cat['code_P'])); if ($cat['treePos'] < $parentCatData['treePos']) { $newCatList[$cat['code']]['newCode_P'] = ' root '; $newCatList[$cat['code']]['newTreePos'] = $listSize--; } else { if (!is_null($cat['code_P']) && !get_cat_id_from_code($cat['code_P'])) { $newCatList[$cat['code']]['newCode_P'] = ' root '; $newCatList[$cat['code']]['newTreePos'] = $listSize--; } else { $newCatList[$cat['code']]['newTreePos'] = $newTreePos++; $newCatList[$cat['code']]['newNb_childs'] = countChild($cat['code']); } } } reset($newCatList); $node_moved = false; // rescan node list and update data if difference was detected. foreach ($newCatList as $cat) { if (isset($cat['newCode_P']) && $cat['code_P'] != $cat['newCode_P']) { $sql = "UPDATE `" . $tbl_category . "` " . ($cat['newCode_P'] == ' root ' ? " SET code_P = null " : " SET code_P = " . (int) $cat['newCode_P']) . " WHERE code = '" . claro_sql_escape($cat['code']) . "'"; $node_moved = true; // repair ownance but brok countchild claro_sql_query($sql); } if (isset($cat['newNb_childs']) && $cat['nb_childs'] != $cat['newNb_childs']) { $sql = "UPDATE `" . $tbl_category . "` " . " SET nb_childs = " . (int) $cat['newNb_childs'] . " WHERE code = '" . claro_sql_escape($cat['code']) . "'"; claro_sql_query($sql); } if ($cat['treePos'] != $cat['newTreePos']) { $sql = "UPDATE `" . $tbl_category . "` " . " SET treePos = " . (int) $cat['newTreePos'] . " WHERE code = '" . claro_sql_escape($cat['code']) . "'"; claro_sql_query($sql); } } if ($node_moved) { return claro_failure::set_failure('node_moved'); } else { return true; } }
$cur = $first; while ($cur->next != $first) { echo "<br/>小孩编号:" . $cur->no; $cur = $cur->next; } echo "<br/>小孩编号:" . $cur->no; } function countChild($first, $m, $k) { $cur = $first; for ($i = 0; $i < $m - 1; $i++) { $cur = $cur->next; } $j = 0; while ($cur != $cur->next) { if ($j == $k - 2) { echo "<br/>出列编号:" . $cur->next->no; $cur->next = $cur->next->next; $cur = $cur->next; $j = 0; } else { $cur = $cur->next; $j++; } } echo "<br/>最后出列编号:" . $cur->no; } addChild(25, $first); showChild($first); countChild($first, 1, 5); //第二个小孩开始数,数到三出列