tree-traversal

Level Order Traversal of a tree

梦想与她 提交于 2019-12-25 06:58:59
问题 In order to do level order(BFS) traversal of a generic tree I wrote the following display function for the code mentioned in the link below. The problem is that each level is printed twice. Can someone tell me why. Original Code without this function can be found in the link below in case someone need the entire implementation else just look at the displayBFS function below and tell me why are values repeating Level Order traversal of a generic tree(n-ary tree) in java Thanks! void displayBFS

Need a complex Xpath using sibling children, ancestors

▼魔方 西西 提交于 2019-12-23 20:05:54
问题 I need to find attribute values based on other values pulled from parent's/grand-parent's sibling's children. I think it's going to take 2 different expressions. So given the following XML (which is derived from a log file that can be thousands of lines long): <p:log xmlns:p="urn:NamespaceInfo"> <p:entries> <p:entry timestamp="2012-12-31T09:39:25"> <p:attributes> <p:attrib name="Position" value="1B2" /> <p:attrib name="Something" value="Something_else" /> </p:attributes> <p:msg> </p:msg> </p

Haskell Tree to List - preorder traversal

删除回忆录丶 提交于 2019-12-23 13:09:19
问题 Given the following tree structure in Haskell: data Tree = Leaf Int | Node Int Tree Tree deriving Show How can I get Haskell to return a list of the data in pre-order? e.g. given a tree: Node 1 (Leaf 2) (Leaf 3) return something like: preorder = [1,2,3] 回答1: You could aim to a more general solution and make your data type an instance of Foldable . There is a very similar example at hackage, but that implements a post-order visit. If you want to support pre-order visits you will have to write

Bin Tree Post Order Traversal, No recursion, no node flag

你离开我真会死。 提交于 2019-12-23 03:35:15
问题 Is there another way to do this? Just spent 2 hours trying to figure it out. I have a solution (see DumpPostOrder below) however, is there is a better or more efficient method? It feels like there may be. Rules are - no recursion, and the nodes cannot have a visited flag. Ie, you can only use left + right members. My approach was to destroy the tree in the process. By setting the children of each side to null you can mark the node as traversed once, but I'm also looking at each node with

construct and print elements of a Binary Tree (unbalanced binary tree), from left to right, and from bottom-up

允我心安 提交于 2019-12-23 02:36:30
问题 please let me know how to achieve the following: I have a binary tree, which is unbalanced, having both the left & right sub trees. I have to print the values of nodes of that unbalanced binary tree in the sequence (i) from left to right, (ii) from bottom to up, and (iii) also the data structure which will be used & its memory management or memory allocation. What initially I thought is that will go for level-order-traversal, en-queue the elements, and then print and de-queue the queue. Your

Understanding the logic in iterative Postorder traversal implementation on a Binary tree

别说谁变了你拦得住时间么 提交于 2019-12-22 14:51:15
问题 I was trying to understand how it is so intuitive to implement postorder traversal using 2 stacks. How did someone come up with it, is it just an observation or some particular way of thinking which helps one come up with such methods. If yes then please explain how to think in the right direction. 回答1: Let me explain how I stumbled on the solution: You start off with this simple observation: prima facie, the pre-order and post-order traversal differ only in their order of operations :

Understanding javascript DOM core ideas: nodeList vs HTMLElement objects

余生长醉 提交于 2019-12-21 22:16:36
问题 I have been working towards understanding the DOM very thoroughly. At the moment i'm making my way trough traversing the DOM tree and i seem to be finding some inconsistencies. On a nodeList i can use an index to iterate trough the list On a HTMLElement i can't use an index See this fiddle for an example: http://jsfiddle.net/AmhVk/4/ So the question is, why is it that the nodeList has an indexable list like element[0], element1 and the HTMLElement has not? Could someone explain this to me

Can I do inorder traversal of a binary tree without recursion and stack?

送分小仙女□ 提交于 2019-12-21 16:18:14
问题 Can anyone give me a solution for traversing a binary tree in inorder without recursion and without using a stack? 回答1: Second edit: I think this is right. Requires node.isRoot, node.isLeftChild, and node.parent, in addition to the usual node.left_child and node.right_child. state = "from_parent" current_node = root while (!done) switch (state) case "from_parent": if current_node.left_child.exists current_node = current_node.left_child state = "from_parent" else state = "return_from_left

How to construct a binary tree from just the level order traversal string

拜拜、爱过 提交于 2019-12-21 05:18:20
问题 Consider a binary tree with the following properties: An internal node (non-leaf node) has a value 1 if it has two children. A leaf node has a value 0 since it has no children. A level order traversal on the tree would generate a string of 1s and 0s (by printing the weird value at each node as they are visited). Now given this string construct the binary tree and perform a post order traversal on the tree. The post order string should be the output of the program. For example: Input String is

Modelling an arbitrary tree in C++ (with iterators)

三世轮回 提交于 2019-12-21 04:45:07
问题 I am looking for a way to model a tree with an arbitrary amount of childrens per nodes. This answer suggests using the Boost Graph Library for this task: What's a good and stable C++ tree implementation? The main operations that I need to perform are traversal functions (preorder, children, leafs) for the tree, as well as its subtrees. I will also need functions that gather data from the children upwards. Is BGL the right choice for this, and how would a preorder traversal of a simple tree be