tree

Generate all leaf-to-root paths in a dictionary tree in Python

时光怂恿深爱的人放手 提交于 2021-02-07 18:09:48
问题 I have a dictionary-tree in an "non-standard" form like so: tree = {'0': {'A': {'B': {'C': {}}}}, {'D': {'E': {}}, {'F': {}}}} Leaf nodes are defined as dictionary key-value pairs where the values is an empty dictionary. I would like to extract all the leaf-to-root paths as lists of lists like so: paths_ = [['C', 'B', 'A', '0'], ['E', 'D', '0'], ['F', 'D', '0']] The paths can be reversed too if that is helpful. paths_ = [['0', 'A', 'B', 'C'], ['0', 'D', 'E'], ['0', 'D', 'F']] I know I have to

Generate all leaf-to-root paths in a dictionary tree in Python

梦想的初衷 提交于 2021-02-07 18:07:16
问题 I have a dictionary-tree in an "non-standard" form like so: tree = {'0': {'A': {'B': {'C': {}}}}, {'D': {'E': {}}, {'F': {}}}} Leaf nodes are defined as dictionary key-value pairs where the values is an empty dictionary. I would like to extract all the leaf-to-root paths as lists of lists like so: paths_ = [['C', 'B', 'A', '0'], ['E', 'D', '0'], ['F', 'D', '0']] The paths can be reversed too if that is helpful. paths_ = [['0', 'A', 'B', 'C'], ['0', 'D', 'E'], ['0', 'D', 'F']] I know I have to

Finding Diameter of a Tree

戏子无情 提交于 2021-02-07 10:51:41
问题 I have written code for finding the diameter of binary tree. But I am unable to figure out where is it going wrong . The two functions that I have written and their definition are as follows :- int btree::diameteroftree(node* leaf) { if (leaf==NULL) return 0; int lheight = hieghtoftree(leaf->left); int rheight = hieghtoftree(leaf->right); int ldiameter = diameteroftree(leaf->left); int rdiameter = diameteroftree(leaf->right); return max(lheight + rheight + 1,max(ldiameter,rdiameter)); } int

Specific Graph and need to more Creative solution

巧了我就是萌 提交于 2021-02-07 08:55:37
问题 Directed Graph (|V|=a, |E|=b) is given. each vertexes has specific weight. we want for each vertex (1..a) find a vertex with maximum weight that can be reachable from that vertex. Update 1: one nice answer is prepare by @Paul in O(b + a log a). but I search for O(a + b) algorithms, if any? Is there any different efficient or fastest any other ways for doing it? 回答1: Yes, it's possible to modify Tarjan's SCC algorithm to solve this problem in linear time. Tarjan's algorithm uses two node

Extjs 4.1 - Listerning in CellEditing plugin not working at second time

梦想的初衷 提交于 2021-02-07 04:37:17
问题 i define a treeGrid with plugin CellEditing like Ext.define('MyExample', { extend: 'Ext.tree.Panel', id: 'example', alias: 'example', .... plugins: [ Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1, listeners: { beforeedit: function(plugin, edit){ alert('don't run second time'); } } }) ], ... And i have a button when i click this button will call below window (this window has treeGrid above) Ext.create('Ext.window.Window', { title: 'Phân xử lý', modal:true, height: 500 width: 500

What does x += x & -x mean?

谁都会走 提交于 2021-02-07 03:28:38
问题 I found that many people use x += x & -x , x -= x & -x to solve the interval tree problem. Can you explain what that equation means? void update(int m, int x) { m++; while (m < N) { t[m] = t[m] + x; m += m & -m; } } int query(int m) { int result= 0; m++; while (m > 0) { result = result + t[m]; m -= m & -m; } return result; } 回答1: Note: This answer (like the method itself) assumes signed integers are represented in two's complement form. The expression x & -x is a quick - but admittedly arcane

Retrieving full hierarchy sorted by a column under PostgreSQL's Ltree module

随声附和 提交于 2021-02-06 09:12:35
问题 I'm using PostgreSQL's Ltree module for storing hierarchical data. I'm looking to retrieve the full hierarchy sorted by a particular column. Consider the following table: votes | path | ... -------+-------+----- 1 | 1 | ... 2 | 1.1 | ... 4 | 1.2 | ... 1 | 1.2.1 | ... 3 | 2 | ... 1 | 2.1 | ... 2 | 2.1.1 | ... 4 | 2.1.2 | ... ... | ... | ... In my current implementation, I'd query the database with SELECT * FROM comments ORDER BY path , which would return the whole tree: Node 1 -- Node 1.1 --

Retrieving full hierarchy sorted by a column under PostgreSQL's Ltree module

余生颓废 提交于 2021-02-06 09:12:34
问题 I'm using PostgreSQL's Ltree module for storing hierarchical data. I'm looking to retrieve the full hierarchy sorted by a particular column. Consider the following table: votes | path | ... -------+-------+----- 1 | 1 | ... 2 | 1.1 | ... 4 | 1.2 | ... 1 | 1.2.1 | ... 3 | 2 | ... 1 | 2.1 | ... 2 | 2.1.1 | ... 4 | 2.1.2 | ... ... | ... | ... In my current implementation, I'd query the database with SELECT * FROM comments ORDER BY path , which would return the whole tree: Node 1 -- Node 1.1 --

Retrieving full hierarchy sorted by a column under PostgreSQL's Ltree module

你。 提交于 2021-02-06 09:09:33
问题 I'm using PostgreSQL's Ltree module for storing hierarchical data. I'm looking to retrieve the full hierarchy sorted by a particular column. Consider the following table: votes | path | ... -------+-------+----- 1 | 1 | ... 2 | 1.1 | ... 4 | 1.2 | ... 1 | 1.2.1 | ... 3 | 2 | ... 1 | 2.1 | ... 2 | 2.1.1 | ... 4 | 2.1.2 | ... ... | ... | ... In my current implementation, I'd query the database with SELECT * FROM comments ORDER BY path , which would return the whole tree: Node 1 -- Node 1.1 --

Retrieving full hierarchy sorted by a column under PostgreSQL's Ltree module

℡╲_俬逩灬. 提交于 2021-02-06 09:07:25
问题 I'm using PostgreSQL's Ltree module for storing hierarchical data. I'm looking to retrieve the full hierarchy sorted by a particular column. Consider the following table: votes | path | ... -------+-------+----- 1 | 1 | ... 2 | 1.1 | ... 4 | 1.2 | ... 1 | 1.2.1 | ... 3 | 2 | ... 1 | 2.1 | ... 2 | 2.1.1 | ... 4 | 2.1.2 | ... ... | ... | ... In my current implementation, I'd query the database with SELECT * FROM comments ORDER BY path , which would return the whole tree: Node 1 -- Node 1.1 --