tree-traversal

Level Order Traversal of a Binary Tree

时光毁灭记忆、已成空白 提交于 2019-11-29 06:51:26
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 code to me. Is there a neat implementation than this or how can I make this code better? void traverse

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

一曲冷凌霜 提交于 2019-11-29 00:38:53
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 more like this: Array ( Array ( [ID] => 1 [parentcat_ID] => 0 [children] => Array ( Array ( [ID] => 6

Catamorphism and tree-traversing in Haskell

心已入冬 提交于 2019-11-28 18:24:51
I am impatient, looking forward to understanding catamorphism related to this SO question :) I have only practiced the beginning of Real World Haskell tutorial. So, Maybe I'm gonna ask for way too much right now, if it was the case, just tell me the concepts I should learn. Below, I quote the wikipedia code sample for catamorphism . I would like to know your opinion about foldTree below, a way of traversing a Tree, compared to this other SO question and answer, also dealing with traversing a Tree n-ary tree traversal . (independantly from being binary or not, I think the catamorphism below can

Pre-order to post-order traversal

不想你离开。 提交于 2019-11-28 17:33:34
问题 If the pre-order traversal of a binary search tree is 6, 2, 1, 4, 3, 7, 10, 9, 11, how to get the post-order traversal? 回答1: You are given the pre-order traversal of the tree, which is constructed by doing: output, traverse left, traverse right. As the post-order traversal comes from a BST, you can deduce the in-order traversal (traverse left, output, traverse right) from the post-order traversal by sorting the numbers. In your example, the in-order traversal is 1, 2, 3, 4, 6, 7, 9, 10, 11.

Traversing a n-ary tree without using recurrsion

时间秒杀一切 提交于 2019-11-28 05:26:44
How can I traverse an n -ary tree without using recursion? Recursive way: traverse(Node node) { if(node == null) return; for(Node child : node.getChilds()) { traverse(child); } } You can do this without recursion and without a stack. But you need to add two extra pointers to the node: The parent node. So you can come back to the parent if you are finished. The current child node so you know which one to take next. For each node, you handle all the kids. If a kid is handled, you check if there is a next kid and handle that (updating the current). If all kids are handled, go back to the parent.

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

Level Order traversal of a generic tree(n-ary tree) in java

早过忘川 提交于 2019-11-28 02:29:16
(In case you want to avoid the lengthy explanation, all I am looking for is a level order traversal for a generic-tree(n-ary tree) in java. The code supplied works and needs the level order display function. Looked around for an hour but couldnt find reference to generic n-ary trees. Would appreciate if soemone can help me build the LevelOrderDisplay function on top of my code as it will help me understand the queue error that I am getting. Thanks! ) I have been trying to implement a tree representation of Autosys job schedules at work. As each job(process) can have one or or more dependent

How to convert a tree structure to a Stream of nodes in java

和自甴很熟 提交于 2019-11-27 16:31:57
问题 I want to convert a tree in a Java8 stream of nodes Here is a tree of nodes storing data wich can be selected public class SelectTree<D> { private D data; private boolean selected = false; private SelectTree<D> parent; private final List<SelectTree<D>> children = new ArrayList<>(); public SelectTree(D data, SelectTree<D> parent) { this.data = data; if (parent != null) { this.parent = parent; this.parent.getChildren().add(this); } } public D getData() { return data; } public void setData(D

Catamorphism and tree-traversing in Haskell

梦想的初衷 提交于 2019-11-27 11:19:29
问题 I am impatient, looking forward to understanding catamorphism related to this SO question :) I have only practiced the beginning of Real World Haskell tutorial. So, Maybe I'm gonna ask for way too much right now, if it was the case, just tell me the concepts I should learn. Below, I quote the wikipedia code sample for catamorphism. I would like to know your opinion about foldTree below, a way of traversing a Tree, compared to this other SO question and answer, also dealing with traversing a

checking subtrees using preorder and inorder strings

时光毁灭记忆、已成空白 提交于 2019-11-27 06:07:14
问题 A book I'm reading claims that one way to check whether a binary tree B is a subtree of a binary tree A is to build the inorder and preorder strings (strings that represent the inorder and preorder traversal of each tree) of both trees, and check whether inorder_B is a substring of inorder_A and preorder_B is a substring of preorder_A . Note that it claims that you have to check substring match on both the inorder and preorder strings. Is it really necessary to check for a substring match on