traversal

Write a non-recursive traversal of a Binary Search Tree using constant space and O(n) run time

那年仲夏 提交于 2019-11-28 03:04:17
This is not homework, this is an interview question. The catch here is that the algorithm should be constant space. I'm pretty clueless on how to do this without a stack, I'd post what I've written using a stack, but it's not relevant anyway. Here's what I've tried: I attempted to do a pre-order traversal and I got to the left-most node, but I'm stuck there. I don't know how to "recurse" back up without a stack/parent pointer. Any help would be appreciated. (I'm tagging it as Java since that's what I'm comfortable using, but it's pretty language agnostic as is apparent.) iluxa I didn't think

jQuery: Getting the two last list items?

喜你入骨 提交于 2019-11-28 02:31:56
问题 I want to apply a special class to the two last list items in an unordered list with jQuery. Like this: <ul> <li>Lorem</li> <li>ipsum</li> <li>dolor</li> <li class="special">sit</li> <li class="special">amet</li> </ul> How to? Should I use :eq somehow? Thanks in advance Pontus 回答1: Another approach, using andSelf and prev: $('ul li:last-child').prev('li').andSelf().addClass("special"); 回答2: You can use function slice. It is very flexible. $('ul li').slice(-2).addClass("special"); 回答3: var

How can I listen to a TAB key pressed/typed in Java?

て烟熏妆下的殇ゞ 提交于 2019-11-28 02:28:23
问题 private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) { //cant capture my TAB? System.out.print(evt.getKeyChar()); } What is the simplest way in an java gui to capture the tab key without doing using the focus listening technique? 回答1: VK_TAB is the tab constant. However: No Tab key-pressed or key-released events are received by the key event listener. This is because the focus subsystem consumes focus traversal keys, such as Tab and Shift Tab. See: http://docs.oracle.com/javase

How to parse or query complex JSON in JavaScript

谁说胖子不能爱 提交于 2019-11-28 02:07:39
问题 Is it possible to perform complex queries over a JSON object? I am open to JavaScript or jQuery solutions, the easier the better. I'm envisioning some kind of functional programming language similar to LINQ or SQL. I Prefer no other third party libraries or add-ons. UPDATE From the looks of early answers, an add-on is going to be necessary. In that case, I prefer an add-on that requires no installation process. Something that deploys with the software publish (like jQuery) is fine (e.g. sets

Prolog, find minimum in a list

会有一股神秘感。 提交于 2019-11-28 01:50:25
in short: How to find min value in a list? (thanks for the advise kaarel) long story: I have created a weighted graph in amzi prolog and given 2 nodes, I am able to retrieve a list of paths. However, I need to find the minimum value in this path but am unable to traverse the list to do this. May I please seek your advise on how to determine the minimum value in the list? my code currently looks like this: arc(1,2). arc(2,3). arc(3,4). arc(3,5). arc(3,6). arc(2,5). arc(5,6). arc(2,6). path(X,Z,A) :- (arc(X,Y),path(Y,Z,A1),A is A1+1;arc(X,Z), A is 1). thus, ' keying findall(Z,path(2,6,Z),L).' in

Skip recursion in jQuery.find() for a selector?

橙三吉。 提交于 2019-11-28 00:58:45
问题 TL;DR: How do I get an action like find(), but block traversal (not full stop, just skip) for a certain selector? ANSWERS: $(Any).find(Selector).not( $(Any).find(Mask).find(Selector) ) There were many truly great answers, I wish I could some how distribute the bounty points more, maybe I should make some 50 pt bounties in response to some of these ;p I choose Karl-André Gagnon's because this answer managed to make findExclude unrequired in one, slightly long, line. While this uses three find

Enumerating all paths in a tree

两盒软妹~` 提交于 2019-11-28 00:07:31
I was wondering how to best implement a tree data structure to be able to enumerate paths of all levels. Let me explain it with the following example: A / \ B C | /\ D E F I want to be able to generate the following: A B C D E F A-B A-C B-D C-E C-F A-B-D A-C-E A-C-F As of now, I am executing a depth-first-search for different depths on a data structure built using a dictionary and recording unique nodes that are seen but I was wondering if there is a better way to do this kind of a traversal. Any suggestions? hugomg Whenever you find a problem on trees, just use recursion :D def paths(tree):

Find the next element that is not immediate?

℡╲_俬逩灬. 提交于 2019-11-27 23:24:09
问题 I want to find the first span element after class counter, in code something like this: <div class="counter"></div> <p></p> <span></span> It seems like the next() function only finds the immediate next element, so something like this: $(".counter").next("span") Won't work. The way I have been using is a bit lengthy and I was wondering if there was a shorter way, it is this: $(".counter").nextAll("span").eq(0) I think the closest() method in jQuery 3 will do the trick, but I am using 1.2.6 --

Using jQuery to gather all text nodes from a wrapped set, separated by spaces

烈酒焚心 提交于 2019-11-27 21:25:27
问题 I'm looking for a way to gather all of the text in a jQuery wrapped set, but I need to create spaces between sibling nodes that have no text nodes between them. For example, consider this HTML: <div> <ul> <li>List item #1.</li><li>List item #2.</li><li>List item #3.</li> </ul> </div> If I simply use jQuery's text() method to gather the text content of the <div> , like such: var $div = $('div'), text = $div.text().trim(); alert(text); that produces the following text: List item #1.List item #2

Ray - Octree intersection algorithms

北城以北 提交于 2019-11-27 19:56:58
问题 I'm looking for a good ray-octree intersection algorithm, which gives me the leafs the ray passes through in an iterative way. I'm planning on implementing it on the CPU, since I do not want to dive into CUDA just yet :) At the moment, my Voxel raycaster just does 3D DDA (Amanatides/Woo version) on a non-hierarchic array of XxYxZ voxels. You can imagine that this is pretty costly when there's a lot of empty space, as demonstrated in the following picture (brighter red = more work :) ): I've