traversal

Recursive tree traversal with mysql through PHP

試著忘記壹切 提交于 2019-12-01 18:19:33
I am creating a questionnaire for a client that requires the questions to be organized by 3 layers of levels. I've successfully created the U.I. however I've been trying for the last 3 hours to pull data from a database in such a way that everything loads in the right place. The database is organized like so by the client so I have no control over it: id description parentId 1 Level 1 0 2 Level 2 0 3 Level 1a 1 4 Level 1b 1 5 Level 1a1 3 I have found a similar question to mine on the site but when I attempted it's solution I got the following on repeat infinetly: Code: function makeList($par

Recursive tree traversal with mysql through PHP

十年热恋 提交于 2019-12-01 18:08:58
问题 I am creating a questionnaire for a client that requires the questions to be organized by 3 layers of levels. I've successfully created the U.I. however I've been trying for the last 3 hours to pull data from a database in such a way that everything loads in the right place. The database is organized like so by the client so I have no control over it: id description parentId 1 Level 1 0 2 Level 2 0 3 Level 1a 1 4 Level 1b 1 5 Level 1a1 3 I have found a similar question to mine on the site but

Prolog - Finding adjacent elements in a list

隐身守侯 提交于 2019-12-01 15:39:49
I'm trying to define a predicate adjacent(X, Y, Zs) that is true if X and Y are adjacent in a list. My code is currently this: adjacent(_, _, []). adjacent(X, Y, [X, Y|Tail]) :- adjacent(X,Y, Tail). It works for the basic case of adjacent(c, d, [a, b, c, d, e]) , but due to the base case, every other case returns true as well, and I'm stuck on that. The other problem is that if X is not equal to the first part of the list's head, then it skips past both X and Y and goes to the next 'X'; e.g., if c isn't equal to a , then it skips past both a and b and checks if c is equal to c . This is

How can I check if all values in an array have a certain value?

拟墨画扇 提交于 2019-12-01 14:52:12
In Java, given an array of values (say integers), is there a way to efficiently check to see if they all have a certain value? For example, with an array of integers such as: int[] numbers = {2, 2, 2, 2, 2, 2, 2}; and you need to perform an action only if all of them are 2, is there a more effective way than doing this: if (numbers[1] == 2 && numbers[2] == 2 && numbers[3] == 2 && …) I know there are ways to do this in C++ but what about Java? Or, if using java 8, you can do something like: if(Arrays.stream(numbers).allMatch(x -> x == 2)) { // do something } Basically, pretty much everything

Traverse json data using jquery

◇◆丶佛笑我妖孽 提交于 2019-12-01 08:12:51
问题 I have following json data [ { id: "79", title: "Web+Infographics", path: "web-infographics" }, { id: "80", title: "Miscellaneous", path: "miscellaneous" }, { id: "81", title: "Entertainment", path: "entertainment" } ] and i want to get the id, title and path out of it using jquery how can i do that? Thanks in advance. 回答1: Quite simple, use jQuery.each : $.each(data, function (index, item) { console.log(item); }); But, you don't really need jQuery for this simple task, give the native Array

Networkx graph searches: dfs_successors vs. dfs_predecessors

流过昼夜 提交于 2019-12-01 06:23:58
Consider the following graph structure (borrowed from this question ): G = networkx.DiGraph() G.add_edges_from([('n', 'n1'), ('n', 'n2'), ('n', 'n3')]) G.add_edges_from([('n4', 'n41'), ('n1', 'n11'), ('n1', 'n12'), ('n1', 'n13')]) G.add_edges_from([('n2', 'n21'), ('n2', 'n22')]) G.add_edges_from([('n13', 'n131'), ('n22', 'n221')]) which yields: n---->n1--->n11 | |--->n12 | |--->n13 | |--->n131 |--->n2 | |---->n21 | |---->n22 | |--->n221 |--->n3 I can perform a depth-first search for successors starting at node n and get: > dfs_successors(G, 'n') {'n': ['n1', 'n2', 'n3'], 'n1': ['n12', 'n13',

How does this inorder traversal algorithm work?

早过忘川 提交于 2019-12-01 05:06:03
问题 I don't have much experience with recursion, so I'm having a hard time determining exactly how this algorithm works: public static void inorder(Node<?> n) { if (n != null) { inorder(n.getLeft()); System.out.print(n.data + " "); inorder(n.getRight()); } } I know that it visits the left and right child nodes of every node in the tree, but I just can't get my head around why exactly it works. 回答1: I'll try to give it a shot. Imagine a tree a b c d e f g Each letter represents a Node object. What

Networkx graph searches: dfs_successors vs. dfs_predecessors

☆樱花仙子☆ 提交于 2019-12-01 05:03:08
问题 Consider the following graph structure (borrowed from this question): G = networkx.DiGraph() G.add_edges_from([('n', 'n1'), ('n', 'n2'), ('n', 'n3')]) G.add_edges_from([('n4', 'n41'), ('n1', 'n11'), ('n1', 'n12'), ('n1', 'n13')]) G.add_edges_from([('n2', 'n21'), ('n2', 'n22')]) G.add_edges_from([('n13', 'n131'), ('n22', 'n221')]) which yields: n---->n1--->n11 | |--->n12 | |--->n13 | |--->n131 |--->n2 | |---->n21 | |---->n22 | |--->n221 |--->n3 I can perform a depth-first search for successors

Bash: how to traverse directory structure and execute commands?

流过昼夜 提交于 2019-12-01 03:53:19
I have split a large text file into a number of sets of smaller ones for performance testing that i'm doing. There are a number of directories like this: /home/brianly/output-02 (contains 2 files myfile.chunk.00 and myfile.chunk.01) /home/brianly/output-04 (contains 4 files...) /home/brianly/output-06 (contains 6 files...) It's important to note that there is an increasing number of files in each directory. What I need to do is run an executable against each of the text files in the output directories. The command looks something like this against a single file: ./myexecutable -i /home/brianly

Traverse FILE line by line using fscanf

感情迁移 提交于 2019-11-30 23:29:59
Ok so i have a text file database.txt. Each line is a user with the format below "John Smith"| 4| 80.00| "123 Lollipop Lane"| "New Jersey"| "08080" When I try do the following: database = fopen(fileName,"r"); while(fscanf(database,"%[^\n]", buffer) != EOF){ printf("ATTEMPT TO: Insert user %s\n\n", buffer); if (insert_Customer(clients, create_Customer(buffer)) < 0){ printf("\nERROR: Failed to insert and create customer %s\n", buffer); } else printf("\nSUCCESS: Inserted Customer %s\n\n", buffer); } It runs fine for the first line, sending in the ENTIRE line to the create_Customer(char * buffer)