tree-traversal

converting a binary search tree to doubly linked list

ⅰ亾dé卋堺 提交于 2019-12-20 10:55:56
问题 This question was asked in a recent coding interview. Q : Given a binary tree, write a program to convert it to a doubly linked list. The nodes in the doubly linked list are arranged in a sequence formed by a zig-zag level order traversal My approach i could always do the zig-zag level order traversal of the tree and store it in an array an then make a double linked list. but the question demands for a in-place solution. can anyone help in explaining the recursive approach should be used? 回答1

Printing a tree lazily in Newick format

北城以北 提交于 2019-12-19 04:14:32
问题 I wish to print a binary tree in Newick format, showing each node's distance to its parent. At the moment I haven't had an issue with the following code, which uses regular recursion, but a tree too deep may produce a stack overflow. (defn tree->newick [tree] (let [{:keys [id children to-parent]} tree dist (double to-parent)] ; to-parent may be a rational (if children (str "(" (tree->newick (first children)) "," (tree->newick (second children)) "):" dist) (str (name id) ":" dist)))) (def

Help me understand Inorder Traversal without using recursion

烂漫一生 提交于 2019-12-18 09:59:07
问题 I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion. This is what I've tried so far: def traverseInorder(node): lifo = Lifo() lifo.push(node) while True: if node is None: break if node.left is not None: lifo.push(node.left) node = node.left continue prev = node while True: if node is None: break print node.value prev = node node =

Level Order Traversal of a Binary Tree

≡放荡痞女 提交于 2019-12-18 04:47:09
问题 void traverse(Node* root) { queue<Node*> q; Node* temp_node= root; while(temp_node) { cout<<temp_node->value<<endl; if(temp_node->left) q.push(temp_node->left); if(temp_node->right) q.push(temp_node->right); if(!q.empty()) { temp_node = q.front(); q.pop(); } else temp_node = NULL; } } The above posted code is my level order traversal code. This code works fine for me but One thing I dont like is I am explicitly initializing temp_node = NULL or I use break. But it does not seem to be a good

PHP Traversing Function to turn single array into nested array with children - based on parent id

99封情书 提交于 2019-12-17 18:33:50
问题 I have an array similar to this: Array ( Array ( [ID] => 1 [parentcat_ID] => 0 ), Array ( [ID] => 2 [parentcat_ID] => 0 ), Array ( [ID] => 6 [parentcat_ID] => 1 ), Array ( [ID] => 7 [parentcat_ID] => 1 ), Array ( [ID] => 8 [parentcat_ID] => 6 ), Array ( [ID] => 9 [parentcat_ID] => 1 ), Array ( [ID] => 13 [parentcat_ID] => 7 ), Array ( [ID] => 14 [parentcat_ID] => 8 ) ) But I need a function to recursively put each item into a 'children' array inside the relevant parent array. So it would look

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

别等时光非礼了梦想. 提交于 2019-12-17 17:24:48
问题 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

jquery find closest previous sibling with class

烂漫一生 提交于 2019-12-17 02:30:40
问题 here's the rough html I get to work with: <li class="par_cat"></li> <li class="sub_cat"></li> <li class="sub_cat"></li> <li class="par_cat"></li> // this is the single element I need to select <li class="sub_cat"></li> <li class="sub_cat"></li> <li class="sub_cat current_sub"></li> // this is where I need to start searching <li class="par_cat"></li> <li class="sub_cat"></li> <li class="par_cat"></li> I need to traverse from the .current_sub , find the closest previous .par_cat and do stuff to

Post order traversal of a formula

时光毁灭记忆、已成空白 提交于 2019-12-14 03:56:28
问题 In data structures, I get converting in order and pre-order formula conversions into trees. However, I'm not so good with post-order. For the given formula x y z + a b - c * / - I came up with - / \ * / (divide) / \ / \ x + - c / \ /\ y z a b For the most part, this seems to fit, except the * in the left subtree is the joker in the deck. In post order traversal, the last character is the top node of the tree, everything else branches down. Now I take the / and * operators to mean that they

Non-recursive breadth-first traversal without a queue

牧云@^-^@ 提交于 2019-12-13 15:32:53
问题 In a generic tree represented by nodes having pointers to parent, siblings, and firs/last children, as in: class Tnode { def data Tnode parent = null Tnode first_child = null, last_child = null Tnode prev_sibling = null, next_sibling = null Tnode(data=null) { this.data = data } } Is it possible to do an iterative (non-recursive) breadth-first (level-order) traversal without using any additional helper structures such as a queue. So basically: we can use single node references for backtracking

Is there any kind of “line visitor” in Eclipse JDT Java Parser? If there is not, does someone knows a good workaround?

女生的网名这么多〃 提交于 2019-12-13 14:05:56
问题 I wanna visit the nodes in an AST from a Java file and extract some information related to these nodes. But, I wanna pass by the AST guided by the lines in the source code file. I know there is information about the lines associated with each node, but the problem is that the default way to access the nodes is through specific visitors. So: 1. to avoid redundant visits to the nodes, 2. do not generates overhead while trying to enumerate all the possible node types (or visitors), and 3. to