traversal

Traversing an object getting the key and all parents keys

大兔子大兔子 提交于 2019-12-04 19:46:12
traverse tree(json), fulfill getKeys(data, str) function using JS. get the key and all parents keys. const data = { key1: 'str1', key2: { key3: 'str3', key4: 'str4', key5: { key6: 'str6', key7: 'str7', key8: 'str8', }, } } for example: getKeys(data, 'str1'); return: 'key1' getKeys(data, 'str3'); return: 'key2, key3' getKeys(data, 'str6'); return: 'key2, key5, key6' I think it can be done be recursion, but how? this is my solution, but failed let s = []; function getKeys(data, str, key='') { if (key !== '') { s.push(key); } for (item in data) { if (typeof data[item] === 'object') { getKeys(data

Use threads when traversing a tree

孤街醉人 提交于 2019-12-04 19:29:23
I will like to speed the process of traversing a tree. Here is an example of a node: class Node { public List<Node> Children { get; set; } public int SompeProperty { get; set; } public String SomeOtherProperty { get; set; } } the way I traverse the try is like: static void TraverseTree(Node ParentNode) { if (ParentNode.Children == null) return; foreach (var child in ParentNode.Children) { TraverseTree(child); } } the ParentNode.Children method takes about 1 millisecond because a Node represents a file or a directory. I just used this example of a node to illustrate better my point. so if you

How to traverse keys of a Hashtable in alphabetical order?

若如初见. 提交于 2019-12-04 18:05:15
What is the easiest way to traverse a hashtable's keys in ascending alphabetical order? This is fairly dependent upon what the type of the key is. But lets assume for a minute that they are strings. You could use the following LINQ query Hashtable table = GetHashTable(); var keys = table.Keys.Cast<String>().OrderBy(x => x); For more complex structures the LINQ query is only slightly different. Lets assume you had the following definition for a key struct Name { public string First; public string Last; // Equality code omitted } The LINQ code would be the following Hashtable table =

Scala: how to traverse stream/iterator collecting results into several different collections

∥☆過路亽.° 提交于 2019-12-04 15:10:25
I'm going through log file that is too big to fit into memory and collecting 2 type of expressions, what is better functional alternative to my iterative snippet below? def streamData(file: File, errorPat: Regex, loginPat: Regex): List[(String, String)]={ val lines : Iterator[String] = io.Source.fromFile(file).getLines() val logins: mutable.Map[String, String] = new mutable.HashMap[String, String]() val errors: mutable.ListBuffer[(String, String)] = mutable.ListBuffer.empty for (line <- lines){ line match { case errorPat(date,ip)=> errors.append((ip,date)) case loginPat(date,user,ip,id) =

jquery find next element with class

若如初见. 提交于 2019-12-04 14:59:30
问题 I'm trying to find the next element with a class of "error" and hitting a wall. In looking at the demo on jQuery's site, this should work, but doesn't. $("button[disabled]").next().text("this button is disabled"); <div> <button disabled="disabled">First</button> <span>no overwrite</span> <span class="error"></span> </div> <div> <button>Second</button> <span></span> </div> <div> <button disabled="disabled">Third</button> <span>no overwrite</span> <span class="error"></span> </div> I'm trying

Reconstructing binary tree from inorder and preorder traversals

江枫思渺然 提交于 2019-12-04 14:36:28
I have written the following code for constructing a tree from its inorder and preorder traversals. It looks correct to me but the final tree it results in does not have the same inorder output as the one it was built from. Can anyone help me find the flaw in this function? public btree makeTree(int[] preorder, int[] inorder, int left,int right) { if(left > right) return null; if(preIndex >= preorder.length) return null; btree tree = new btree(preorder[preIndex]); preIndex++; int i=0; for(i=left; i<= right;i++) { if(inorder[i]==tree.value) break; } tree.left = makeTree(preorder, inorder,left,

c++: pass function as parameter to another function

此生再无相见时 提交于 2019-12-04 09:54:17
i am currently implementing a binary tree in c++ and i want to traverse it with a function called in_order(). is there any way to pass a function as an argument, so that i can do things like below (without having to write the code to traverse the list more than once)? struct tree_node; // and so on class tree; // and so on void print_node () { // some stuff here } // some other functions tree mytree(); // insert some nodes mytree.in_order(print_node); mytree.in_order(push_node_to_stack); mytree.in_order(something_else); Yes, you can do this in a number of ways. Here are two common

Applying a Logarithm to Navigate a Tree

谁说胖子不能爱 提交于 2019-12-04 07:08:18
I had once known of a way to use logarithms to move from one leaf of a tree to the next "in-order" leaf of a tree. I think it involved taking a position value (rank?) of the "current" leaf and using it as a seed for a fresh traversal from the root down to the new target leaf - all the way using a log function test to determine whether to follow the right or left node down to the leaf. I no longer recall how to exercise that technique. Can anyone re-introduce me? I also don't recall if the technique required the tree to be balanced, or if it worked on n-trees or only binary trees. Any info

PHP - Code to traverse a directory and get all the files(images)

陌路散爱 提交于 2019-12-04 06:17:23
问题 i want to write a page that will traverse a specified directory.... and get all the files in that directory... in my case the directory will only contain images and display the images with their links... something like this How to Do it p.s. the directory will not be user input.. it will be same directory always... 回答1: /** * function get files * @param $path string = path to fine files in * @param $accept array = array of extensions to accept * @param currentLevel = 0, stopLevel = 0 *

Select element by and classname in javascript

我们两清 提交于 2019-12-04 05:19:36
问题 I have an onChange event that needs to dynamically select an ID and then add a preset classname to pass to a function. onChange = "show(document.getElementById(this.value). {select a class here? } );" The equivalent in jquery $('#'+this.value+'.myclassname').function(); So how do I select an ID+classname in javascript? I know I'm being dense. 回答1: You need to use a classname? Ids should be unique. var element = document.getElementById('myId'); Or say element is a parent and you want a child