Giter Site home page Giter Site logo

lijiebin / treeone Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 0.0 93 KB

A powerfully all-purpose hierarchical tree data structure desgin model as a best practice in development.

php java python development application mysql database sql system-design data-structures hierarchical-data tree-view catalog information-storage

treeone's Introduction

简体中文

TreeOne

A powerfully all-purpose hierarchical tree data structure design model as a best practice in development. (Demo default by MySQL)

image

Table Data Structure

CREATE TABLE `tree` (
  `id` int(11) NOT NULL,
  `name` varchar(30) NOT NULL, 
  `level` mediumint(4) NOT NULL DEFAULT '1', // Level number in its barnch
  `branch` mediumint(4) NOT NULL DEFAULT '1', // Means ancestry flag
  `path` varchar(50) NOT NULL DEFAULT '1' // Route to forefather
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  

Features

  • Very nifty structure
  • Strong flexibility
  • High Performance
  • Convenient for query
  • Almost cover all development circumstances

Sample Rows

id name level branch path
1 ----cate-1 1 1 1
3 --------cate-1-1 2 1 1.1
4 ------------cate-1-1-1 3 1 1.1.1
5 --------cate-1-2 2 1 1.2
6 ------------cate-1-2-1 3 1 1.2.1
2 ----cate-2 1 2 2
7 --------cate-2-1 2 2 2.1
9 ------------cate-2-1-1 3 2 2.1.1
10 ------------cate-2-1-2 3 2 2.1.2
8 --------cate-2-2 2 2 2.2

Query Examples

  • Render whole tree in one query
SELECT * FROM `tree` ORDER BY `branch`, `path`;
  • Get max deepth of tree
SELECT MAX(`level`) FROM `tree`;
  • Get branch root node
SELECT * FROM `tree` WHERE `branch` = 1 ORDER BY `path` ASC LIMIT 1;
  • Get one's parent
SELECT * FROM `tree` WHERE `branch` = 1 AND `path` = '1.1';
  • Get one's all children(same as one's all slibings)
SELECT * FROM `tree` WHERE `level` = 3 AND `branch` = 1 AND `path` > '1.1' AND `path` < '1.2';
  • Get one's all descendant
SELECT * FROM `tree` WHERE `branch` = 1 AND `path` > '1.1' AND `path` < '1.2' ORDER BY `path` ASC;
  • Get one's all ascendant (forms of "Bread-Crumbs")
SELECT * FROM `tree` WHERE `level` < 3 AND `path` IN ('1.2', '1') ORDER BY `path` ASC
  • Get one's immediately preceding sibling
SELECT * FROM `tree` WHERE `level` = 2 AND `branch` = 2 AND `path` < '2.2' ORDER BY `path` DESC LIMIT 1;
  • Get one's immediately succeeding sibling
SELECT * FROM `tree` WHERE `level` = 2 AND `branch` = 2 AND `path` > '2.2' ORDER BY `path` ASC LIMIT 1;

treeone's People

Contributors

lijiebin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.