Nested
nested sets model demo
Install / Use
/learn @houz42/NestedREADME
Nested Set Model
Nested Set Model (or Modified Preorder Tree) represents nested sets (trees or hierarchies) in relational databases. See wikipedia.
Model in short
The nested model:
- assigns two numbers for each node as left and right, that
- left of each node is less than all its children's left, and
- right of each node is greater than all its children's right.
Numbers could be assigned according to a preorder tree traversal, which visits each node twice, and assigns numbers at both visits.
- Then querying all descendants of a node could be efficiency as:
SELECT child.id, child.node, child.lft, child.rgt
FROM `nested` parent, `nested` child
WHERE child.lft BETWEEN parent.lft AND parent.rgt
AND parent.id=@node_id
- And querying the path from root to any node could be:
SELECT parent.id, parent.node, parent.lft, parent.rgt
FROM `nested` parent, `nested` child
WHERE child.lft BETWEEN parent.lft AND parent.rgt
AND child.id=@node_id
ORDER BY parent.lft
- Another column
depthis used to record depth of the node, to query immediate children of a node efficiently, and the querying could be:
SELECT child.id, child.node, child.lft, child.rgt
FROM `nested` parent, `nested` child
WHERE child.lft BETWEEN parent.lft AND parent.rgt
AND child.depth=parent.depth+1
AND parent.id=@node_id
The nested model is also suitable for trees with more than one root, forests.
Test clothing categories
Test data used in nested_test.go are collected from previous wikipedia article.
- Clothing
- Men's
- Suits
- Slacks
- Jackets
- Women's
- Dresses
- Evening Gowns
- Sun Dresses
- Skirts
- Blouses
Traversal and number nodes as:
![]()
Demo
Chinese division data
Store Chinese division data with nested sets:
- build a division tree from raw data,
- assign left and right number for divisions by preorder tree traversal,
- generate sql inserting queries
Data collected from 中国行政区划数据. Initial inserting SQL in division.sql are generated with build.go:
$ cd division && go run build.go # generates data inserting sql
T** product categories data
Store product category info and structure with nested sets:
- run
spider/spider.pyto query data from t**, - build sql inserting data
data/categoryInfo.sqlanddata/categoryTree.sqlas inbuild_test.go
Use as dependency
- create new table as in
createtable.sqlwith your table name; - initialize table as in
division/build.go, or - call
Add...()continually as inTestInserting(); - call
SetTableName()in yourinit();
Related Skills
oracle
352.2kBest practices for using the oracle CLI (prompt + file bundling, engines, sessions, and file attachment patterns).
xurl
352.2kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
prose
352.2kOpenProse VM skill pack. Activate on any `prose` command, .prose files, or OpenProse mentions; orchestrates multi-agent workflows.
Command Development
111.1kThis skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
