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
3
votes
1 answer

Need a complex Xpath using sibling children, ancestors

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…
N1tr0
  • 485
  • 2
  • 6
  • 24
3
votes
4 answers

Given the preorder traversal of a heap, construct the heap

Saw this question somewhere in the internet and tried to solve it. I could solve it for cases where the heap is a strictly binary tree (by repeatedly partitioning the preorder traversal) but could not figure out an algorithm when the heap is only a…
Raj
  • 4,342
  • 9
  • 40
  • 45
3
votes
1 answer

Quickly detecting identical nodes which are siblings to an ancestor

I am trying to find a fast algorithm to identify couples of nodes (A, B) that contains the same data and that are positioned on a tree in such way that a node A has as an ancestor the node B OR B is the sibling of an ancestor of A. Take for…
mac
  • 42,153
  • 26
  • 121
  • 131
3
votes
1 answer

Graph traversal for node search

In the given below graph, the child parts are traversed recursively. Each child must report its immediate parent. The problem is child[3] must report both of its immediate parent (i.e. child[2] and child[4]) at the same time in the same…
user32262
  • 8,660
  • 21
  • 64
  • 77
2
votes
3 answers

Numbering Regex Submatches

Is there a canonical ordering of submatch expressions in a regular expression? For example: What is the order of the submatches in "(([0-9]{3}).([0-9]{3}).([0-9]{3}).([0-9]{3}))\s+([A-Z]+)" ? a.…
user16753
  • 21
  • 1
2
votes
4 answers

level-order, tree traversal - How to keep track of the level?

How to keep track of the level while traversing a binary tree in level order or breadth-first order? The nodes in the binary tree have left and right references only. I want to be able to distinguish between each row of nodes. Here is my method for…
XP1
  • 6,910
  • 8
  • 54
  • 61
2
votes
2 answers

Python: Solving "Generate Parentheses" with Backtracking --> Confused About stack.pop()

I am trying to understand the backtracking code below: class Solution: def generateParenthesis(self, n: int) -> List[str]: stack = [] res = [] def backtrack(openN, closedN): if openN == closedN == n: …
PineNuts0
  • 4,740
  • 21
  • 67
  • 112
2
votes
1 answer

Is it a good idea to have functions whose sole purpose it is to call another function?

I wrote code for a Tree traversal. In the Binary_tree class, I wrote three methods: __Inorder, __Postorder, and __Preorder, for tree traversal; and I wrote three other methods to call them and pass in self.root as a parameter so that I don't have to…
2
votes
1 answer

Why does tree traversal result in undefined behaviour when not using pointer to keep track of current node?

I'm experimenting with compression algorithms and encountered an issue whereby my decoding algorithm would give the expected result where the encoded input was short, but after it reached a certain level of complexity it would start returning…
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
2
votes
3 answers

Traversing an html nested list using javascript

I have an HTML list (
    ,
  • , etc.) of pages, with multiple items at various depths. I am trying to write some code to traverse this list one element at a time. So a "next " button would return the ID of the following
  • element, which could be…
Hatcham
  • 330
  • 3
  • 10
2
votes
0 answers

printf for interval heap shows unexpected result with certain input

Sorry if my English is bad, I speak Korean as mother tongue. I've just learned about interval heap, and implementing interval heap in C. My code should receive integers from input file, and if the value of read integer is positive, insert the value…
k9wan
  • 45
  • 3
2
votes
1 answer

Traverse object and return matching key / values

I have a series of objects containing product information that I need to use to filter the available product options. (i.e. Customer selects radio button with value option id 25, I need to filter the other two options based on what's available for…
Momo
  • 23
  • 3
2
votes
1 answer

Solve binary expression tree for given variable

For the expression: res = ((v + 10) * (i - 2)) / 3 My program builds the following binary tree: (=) ├─ (res) └─ (/) ├─ (3) └─ (*) ├─ (+) │ ├─ (v) │ └─ (10) └─ (-) ├─ (i) …
user7401478
  • 1,372
  • 1
  • 8
  • 25
2
votes
2 answers

How to traverse BTree in-order without recursion in iterative style?

I need B-Tree LNR traversal (in-order). I've found an algorithm for B-Tree traversal here. How I can implement it without recursion in iterative way? I've found this question but there is no answer and the code in the question is so unclear and…
NikBond
  • 743
  • 1
  • 5
  • 20
2
votes
1 answer

Find all unique nodes of a n-ary tree in all paths that reach a certain depth without recursion

I'm trying to write an algorithm in Python to get a unique list of all nodes in a tree where the path reaches a certain depth. Each child has an unknown number of children prior to traversal The children can be accessed via an iterable (e.g. for…
user276833
  • 77
  • 6