Questions tagged [tree-traversal]

The process of visiting each node in a tree based on certain criterion.

The process of visiting each node in a tree based on certain criterion.

Use this tag for programming questions related to traversal of all types of trees.

See also:

832 questions
6
votes
3 answers

Fixing my implementation of "inorder tree traversal" algorithm with a Stack

Part of it is that I have to implement a non-recursive method of a inorder traversal of a binary tree. I am kind of stuck. Here is what I have so far: public void inorder(BinaryTree v) { Stack stack = new Stack(); …
tenkii
  • 449
  • 2
  • 10
  • 23
5
votes
1 answer

Traverse Array of objects to generate d3 Sankey Chart data

This input (tree-like structure) has to be formatted to a particular format to draw a d3 sankey diagram chart. let unformattedJson = [ { "key": "a1", "value": 30, "buckets": [ { "key": "a2", "value": 10 }, …
ideeps
  • 379
  • 4
  • 15
5
votes
1 answer

Using Abstract base class type to traverse entire JAXB Object tree

The system hardware I write software for is physically connect via hardware in a tree structure. The data model in our application is a Tree. For our new rewrite, we're using JAXB to create the data model. We have three types of Devices, and they…
Matt Brown
  • 435
  • 4
  • 17
5
votes
4 answers

Idiomatic Python: Propagating yields or flattening sequences?

I'm writing a breadth depth-first tree traversal function, and what I want to do is this: def traverse(node): yield node for n in node.children: yield_all traverse(n) # << if Python had a yield_all statement The idea is to end up…
perimosocordiae
  • 17,287
  • 14
  • 60
  • 76
5
votes
2 answers

How to detect circular reference in a Tree?

I have a Node class as follows: public class Node{ Object data; List children; } I need to traverse this tree in post order and I am using Guava TreeTraverser for for the same. TreeTraverser treeTraverser = new…
vatsal mevada
  • 5,148
  • 7
  • 39
  • 68
5
votes
5 answers

Inward spiral tree traversal

I came across an interesting problem: Given a binary tree print it in inward spiral order i.e first print level 1, then level n, then level 2, then n-1 and so on. For Ex: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Should Output: 1 15 14 13 12 11 10 9 8…
Atri
  • 5,511
  • 5
  • 30
  • 40
5
votes
4 answers

Find all subtrees in a BST whose keys lie in a given range

I was given this question during a recent interview: Given a BST whose nodes contains an Integer as value, find all subtrees whose nodes fall between integers X (min) and Y (max), where X
Quest Monger
  • 8,252
  • 11
  • 37
  • 43
5
votes
3 answers

Strategy to implement tree traversing algorithm in parallel?

I have implemented an iterative algorithm, where each iteration involves a pre-order tree traversal (sometimes called downwards accumulation) followed by a post-order tree traversal (upwards accumulation). Each visit to each node involves…
5
votes
3 answers

Building a BST from a depth-first preorder list in Haskell more idiomatically

This submission to Programming Praxis gives an O(n) function that "undoes" a preorder traversal of a binary search tree, converting a list back into a tree. Supplying the missing data declaration: data Tree a = Leaf | Branch {value::a, left::Tree…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
5
votes
2 answers

Visualisation of Tree Hierarchy in HTML

I am looking of inspiration for doing interaction design on a hierachy/tree structure. (products with a number of subproducts, rules that apply for selecting subproducts). I want to have a tree where there is a visible connection between sub-nodes…
5
votes
2 answers

Recursive Postorder Traversal to List in Python?

Hello all—I'm a programming newcomer, and have the following very simple code here: def postorder(T): if T != None: postorder(T.left) postorder(T.right) print T.data, All I want is instead of printing the traversal I…
Aaron Ramsey
  • 63
  • 1
  • 1
  • 7
5
votes
3 answers

Depth first search using Queue

How do I do a depth first search using a Queue in c#? The following is my datastructure: public class Node { public string Name{get;set} public IEnumerable Children{get;set;} } Now I have a collection of Node object each with children,…
Mike
  • 3,204
  • 8
  • 47
  • 74
4
votes
1 answer

Huffman Code encoding traversal

I am trying to do the encoding of a huffman tree. My tree is correct. I just need to figure out how to fix my recursive function to create the table properly. Thanks for any help I can receive. struct Code { char letter; string…
user1266174
  • 111
  • 2
  • 11
4
votes
3 answers

Haskell Tree to List - preorder traversal

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 =…
Gravy
  • 12,264
  • 26
  • 124
  • 193
4
votes
2 answers

Best Algorithm to get a Post-order Tree Traversal

I have a Tree containing a Large Number of nodes, and I Trying to get The Best post-order Traversal Algorithm. NOTE: The Algorithm Shouldn't consider Recursion Due to large number of Nodes may cause StackOverFlow Exception. The Algorithm…
Aboelnour
  • 1,423
  • 3
  • 17
  • 39