hierarchy

Count all child nodes of hierarchical data in a table

隐身守侯 提交于 2019-11-28 00:22:51
I want to count number of all child nodes under any level of tree structure maintained in a table using adjacency model (parent-child key). Table structure and data looks like this: id - item- parentid 1 - A - 2 - B - 1 3 - C - 1 4 - D - 2 5 - E - 2 6 - F - 3 7 - G - 3 8 - H - 5 9 - I - 5 10 - J - 9 11 - K - 4 For example B has following child and grand child structure: B E H I J F K Now if you want to count "All child nodes of B" my answer should be 6. Any pure SQL Query based solution would be of great help. Or mysql/php will also work. Thanks! The way you store your data will not allow for

Creating a JSON Tree from a string hierarchy

不羁岁月 提交于 2019-11-27 21:51:50
问题 Given these 4 variables, var el1 = {name:'ronaldo', team: 'europe/spain/realmadrid'} var el2 = {name:'messi', team: 'europe/spain/barcelona'} var el3 = {name:'gerald', team: 'europe/england/liverpool'} var el4 = {name:'unknown english', team: 'europe/england'} I need to produce this JSON tree hierarchy, { "text":"europe", "leaf":false, "children":[ { "text":"spain", "leaf":false, "children":[ { "text":"realmadrid", "leaf":false, "children":[ { "text":"ronaldo", "leaf":true } ] }, { "text":

Convert delimited string into hierarchical JSON with JQuery

独自空忆成欢 提交于 2019-11-27 19:08:35
I have an array of strings that describe the parent/child relationship by being delimited with dashes. So, if Bob's boss was Jim and Jim's boss was Fred, Bob's entry in the array would be "Fred-Jim-Bob" and Jim's entry would be "Fred-Jim." I don't have the ability to change the way the data is coming in so I was looking for help as far as the best way of turning these values into JSON similar to this: { "name": "Fred", "children": { "name": "Jim", "children": { "name": "Bob" } } } Any help would be greatly appreciated. Thanks. var input = ["Fred-Jim-Bob", "Fred-Jim", "Fred-Thomas-Rob", "Fred"]

Angular 2 - How does ng-bootstrap provide the NgbRadioGroup and NgbButtonLabel to their NgbRadio directive?

試著忘記壹切 提交于 2019-11-27 16:48:19
问题 Here is the label code: import {Directive} from '@angular/core'; @Directive({ selector: '[ngbButtonLabel]', host: {'[class.btn]': 'true', '[class.active]': 'active', '[class.disabled]': 'disabled', '[class.focus]': 'focused'} }) export class NgbButtonLabel { active: boolean; disabled: boolean; focused: boolean; } and here is the radio button code: import {Directive, forwardRef, Input, Renderer2, ElementRef, OnDestroy} from '@angular/core'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from

Interesting tree/hierarchical data structure problem

烈酒焚心 提交于 2019-11-27 16:37:31
问题 Colleges have different ways of organizing their departments. Some schools go School -> Term -> Department . Others have steps in between, with the longest being School -> Sub_Campus -> Program -> Term -> Division -> Department . School , Term , and Department are the only ones that always exist in a school's "tree" of departments. The order of these categories never changes, with the second example I gave you being the longest. Every step down is a 1:N relationship. Now, I'm not sure how to

Tree drawing orientation

北战南征 提交于 2019-11-27 13:17:21
I've made a slightly modified tree using the tree layout. I needed to orient the tree right-to-left instead of the regular left-to-right orientation that's the default. What is the right and proper d3:ish way to do this? I ended up doing this by simply inverting the x coordinate after creating the layout but I feel that this is a hack. Surely there is something more elegant? I thought about doing an SVG rotation around the center but then I'd have to rotate the labels to get the text right way around. That didn't feel right either. mbostock The tree layout computes node positions in an

Construct hierarchy tree from flat list with parent field?

亡梦爱人 提交于 2019-11-27 13:17:06
I have a list of "page" objects with a parent field. This parent field references another object in the list. I would like to create a tree hierarchy from this list based on this field. Here is what my original list looks like: [ { id: 1, title: 'home', parent: null }, { id: 2, title: 'about', parent: null }, { id: 3, title: 'team', parent: 2 }, { id: 4, title: 'company', parent: 2 } ] I would like to convert it into a tree structure like this: [ { id: 1, title: 'home', parent: null }, { id: 2, title: 'about', parent: null, children: [ { id: 3, title: 'team', parent: 2 }, { id: 4, title:

Is it good practice to often use instanceof?

北城以北 提交于 2019-11-27 13:15:41
问题 The scenario. I'm writting game-related code. In that game a Player (its also a class) has a list of Item . There are other types of items that inherit from Item , for example ContainerItem , DurableItem or WeaponItem . Obviously it is very conveniant for me to just have List<Item> . But when I get the players items, the only way for me to distinguish between what type of item is by using the instanceof keyword. I'm sure I've read that reliaing on it is bad practice. Is it ok to use it in

Flat PHP Array to Hierarchy Tree

二次信任 提交于 2019-11-27 12:54:37
I have an array with the following keys id parent_id name A sample array: array(7) { [0]=> array(3) { ["id"]=> string(1) "4" ["parent_id"]=> string(1) "0" ["name"]=> string(16) "Top Level Page 4" } [1]=> array(3) { ["id"]=> string(1) "5" ["parent_id"]=> string(1) "1" ["name"]=> string(19) "Second Level Page 1" } [2]=> array(3) { ["id"]=> string(1) "6" ["parent_id"]=> string(1) "2" ["name"]=> string(19) "Second Level Page 2" } [3]=> array(3) { ["id"]=> string(1) "7" ["parent_id"]=> string(1) "5" ["name"]=> string(18) "Third Level Page 1" } [4]=> array(3) { ["id"]=> string(1) "3" ["parent_id"]=>

Create nested list from PHP array for dropdown select field

て烟熏妆下的殇ゞ 提交于 2019-11-27 12:34:08
问题 I am struggling with an array I want to turn into a nested < select > I need: <select> <option value="1">Top1</option> <option value="2">Top2</option> <option value="9">Top3</option> <option value="7"> - - Top3.1</option> <option value="5"> - - Top3.2</option> <option value="12">- - - - Top3.2.1</option> <option value="6">Top4</option> <option value="4">Top5</option> <option value="8"> - - Top5.1</option> <option value="3"> - - Top5.2</option> I can't work with optgroup, because everything is