traversal

Traverse tree without recursion and stack in C

天大地大妈咪最大 提交于 2019-12-20 12:30:11
问题 How to traverse each node of a tree efficiently without recursion in C (no C++)? Suppose I have the following node structure of that tree: struct Node { struct Node* next; /* sibling node linked list */ struct Node* parent; /* parent of current node */ struct Node* child; /* first child node */ } It's not homework. I prefer depth first. I prefer no additional data struct needed (such as stack). I prefer the most efficient way in term of speed (not space). You can change or add the member of

Find the element before and after a specific element

巧了我就是萌 提交于 2019-12-20 10:30:01
问题 I have a list with links I use with tabs. It looks like this: <ul> <li><a href="#">First tab</a></li> <li><a href="#">Second tab</a></li> <li class="active"><a href="#">Active tab</a></li> <li><a href="#">Fourth tab</a></li> <li><a href="#">Fifth tab</a></li> </ul> How can I find the list element before and after the active tab? (In this case, the second and fourth tab). Tried using find, with no success :( 回答1: $("li.active").next(); and $("li.active").prev(); 来源: https://stackoverflow.com

C# Graph Traversal

一曲冷凌霜 提交于 2019-12-20 09:47:15
问题 This algorithm does a great job of traversing the nodes in a graph. Dictionary<Node, bool> visited = new Dictionary<Node, bool>(); Queue<Node> worklist = new Queue<Node>(); visited.Add(this, false); worklist.Enqueue(this); while (worklist.Count != 0) { Node node = worklist.Dequeue(); foreach (Node neighbor in node.Neighbors) { if (!visited.ContainsKey(neighbor)) { visited.Add(neighbor, false); worklist.Enqueue(neighbor); } } } I can use this to find a target node in the graph. The worklist

How to get hostname from IP address from file similar to /etc/hosts

*爱你&永不变心* 提交于 2019-12-20 06:19:47
问题 I have a file which maps IP Address to hostname. Its format is similar to hosts file and contains a list of ipaddress to hostname mapping. eg. 10.200.99.1 master1 10.200.99.2 master2 10.200.99.3 master3 10.200.99.4 slave1 10.200.99.5 slave2 10.200.99.6 slave3 ... ... ... I would like to obtain hostname from a given ipaddress using bash script. How can i do so? 回答1: You can try that : sh script.sh listofip #!/bin/bash echo "IP ?" echo -n "(Value and press Enter) :" read ip while read line do

How to traverse SimpleXML to edit text nodes?

匆匆过客 提交于 2019-12-20 06:19:28
问题 I need to implement the following algorithm with SimpleXML: put a XML fragment string into a SimpleXML object; traverse all the nodes, selecting text nodes; edit the text node (example convert to upper case); return the xml as string. PROBLEMS: How to load a XML with named entities (ex.   ). To traverse XML to get only text nodes... With $sx->xpath('//text()'); I can not edit the nodes, how to select text nodes to edition? 回答1: You can override the text content of a node returned by a

<HTMLDivElement> has no method 'siblings'

你离开我真会死。 提交于 2019-12-19 19:44:06
问题 Im trying to change a sibling of a div element and this is the statement i used $('.edit').click(function(){ this.siblings('.innerInfo').html("success"); }); It keeps throwing the <HTMLDivElement> has no method 'siblings' exception, and i really cant figure out why. I've initiated jQuery and ive started the script on document.ready thanks for your help! 回答1: Use $(this) instead of this . 回答2: You're should reference $(this) like: $('.edit').click(function(){ $(this).siblings('.innerInfo')

Prolog - Finding adjacent elements in a list

喜你入骨 提交于 2019-12-19 12:24:55
问题 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.,

Prolog - Finding adjacent elements in a list

不羁的心 提交于 2019-12-19 12:24:02
问题 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.,

In Pyramid, how can I use a different renderer based on contents of context?

可紊 提交于 2019-12-19 02:31:20
问题 I have 3 different product page layouts that I would like to display dependent on the information available about the products. Using traversal I have a class called ProductFinder that grabs all the information. For example the user goes to domain/green/small and ProductFinder will list all products from my DB that are green and small. This list is self.products in the ProductFinder class. In my __init__.py I have added the line: config.add_view('app.views.products', name='') In products.py I

NLTK: How do I traverse a noun phrase to return list of strings?

烈酒焚心 提交于 2019-12-19 02:04:12
问题 In NLTK, how do I traverse a parsed sentence to return a list of noun phrase strings? I have two goals: (1) Create the list of Noun Phrases instead of printing them using the 'traverse()' method. I presently use StringIO to record the output of the existing traverse() method. That is not an acceptable solution. (2) De-parse the Noun Phrase string so: '(NP Michael/NNP Jackson/NNP)' becomes 'Michael Jackson'. Is there a method in NLTK to de-parse? The NLTK documentation recommends using