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
12
votes
4 answers

jQuery: If the selected element $(this) has a parent with a classname of 'last'

I must be missing something quite important, I have been using .parent().parent().parent().. etc to traverse down the DOM and .next().next() to traverse up the DOM. I know this is wrong and that I need something more reliable, I need a selector that…
Iamsamstimpson
  • 1,359
  • 2
  • 17
  • 32
11
votes
1 answer

Jq: recursively delete all keys that match a given pattern

How to recursively delete all keys that match a given pattern? I have following jq config, but it doesn't seem to work: walk( if (type == "object" and (.[] | test('.*'))) then del(.) else . end)
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
10
votes
9 answers

Iterative depth-first tree traversal with pre- and post-visit at each node

Can anyone point me at pseudocode for iterative depth-first tree traversal, where it's possible to do actions on each node at both pre- and post- order? That is, an action before descent into a node's children, then an action after ascent from the…
xeolabs
  • 1,369
  • 1
  • 9
  • 16
10
votes
3 answers

Does creating new Processes help me for Traversing a big tree?

Let's think of it as a family tree, a father has kids, those kids have kids, those kids have kids, etc... So I have a recursive function that gets the father uses Recursion to get the children and for now just print them to debug output window...But…
Bohn
  • 26,091
  • 61
  • 167
  • 254
9
votes
3 answers

How to get parent multiple levels up the DOM tree without jQuery or modifying Element.prototype?

I am wondering if there is a way to select a specific element way up the DOM only using vanilla JS while not having to use parentNode multiple times. I understand you can do this with jQuery and modifying Element.prototype, but are there any other…
Nathan Boaldin
  • 185
  • 1
  • 4
  • 14
9
votes
1 answer

Implementation of Foldable in Haskell

For example, I have some data type. Let it be a binary tree: data Tree a = Leaf a | Branch (Tree a) (Tree a) For example, I implemented traversal of the tree: treeFoldt :: Tree t -> [t] treeFoldt = foldt1 (:) [] It works pretty good. But I want to…
David
  • 674
  • 6
  • 19
9
votes
2 answers

In-order tree traversal for non-binary trees

Does the term "inorder traversal" have a well-defined meaning for trees wider than binary trees, or are "pre-" and "post-" order the only type of DFS that makes sense? I mean with n>2 children per-node. I guess for n that is even it might mean going…
Baruch
  • 20,590
  • 28
  • 126
  • 201
9
votes
3 answers

Why is inorder and preorder traversal useful for creating an algorithm to decide if T2 is a subtree of T1

I'm looking at an interview book and the question is: You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. The authors mentions this as a…
8
votes
2 answers

The simplest way to generically traverse a tree in haskell

Suppose I used language-javascript library to build AST in Haskell. The AST has nodes of different types, and each node can have fields of those different types. And each type can have numerous constructors. (All the types instantiate Data, Eq and…
8
votes
8 answers

Level Order Traversal of a Binary Tree

void traverse(Node* root) { queue q; Node* temp_node= root; while(temp_node) { cout<value<left) q.push(temp_node->left); if(temp_node->right) …
brett
  • 5,379
  • 12
  • 43
  • 48
8
votes
3 answers

Time complexity of level order traversal

What is the time complexity of binary tree level order traversal ? Is it O(n) or O(log n)? void levelorder(Node *n) { queue < Node * >q; q.enqueue(n); while(!q.empty()) { Node * node = q.front(); DoSmthwith…
Fatima
  • 1,611
  • 6
  • 18
  • 21
7
votes
1 answer

Python - Tree traversal question

I have a hard time with tree traversal, and so avoid it like the plague... normally. I have a class that's sort-of (slightly simplified version here, but functionally the same) like: class Branch(object): def __init__(self, title, parent=None): …
orokusaki
  • 55,146
  • 59
  • 179
  • 257
7
votes
2 answers

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

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…
fuji
  • 1,173
  • 1
  • 10
  • 27
7
votes
4 answers

Python: reference variable in nested function's outer scope (not global)

I'm trying to recurse a tree and track the path of the traversal up to the point where I find an element that I'm looking for. However, I encounter two issues: While my current code returns the correct solution, it's a bit hacky. I have to push…
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
7
votes
6 answers

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

Can anyone give me a solution for traversing a binary tree in inorder without recursion and without using a stack?
Gautham
  • 3,418
  • 3
  • 30
  • 41
1 2
3
55 56