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

Breadth First Vs Depth First

When Traversing a Tree/Graph what is the difference between Breadth First and Depth first? Any coding or pseudocode examples would be great.
180
votes
8 answers

Explain Morris inorder tree traversal without using stacks or recursion

Can someone please help me understand the following Morris inorder tree traversal algorithm without using stacks or recursion ? I was trying to understand how it works, but its just escaping me. 1. Initialize current as root 2. While current is…
brainydexter
  • 19,826
  • 28
  • 77
  • 115
156
votes
5 answers

How can I find the closest previous sibling with class using jQuery?

Here's the rough HTML I get to work with:
  • // this is the single element I need to select
  • daulex
    • 1,615
    • 2
    • 12
    • 10
    120
    votes
    20 answers

    How can I get selector from jQuery object

    $("*").click(function(){ $(this); // how can I get selector from $(this) ? }); Is there an easy way to get selector from $(this)? There is a way to select an element by its selector, but what about getting the selector from element?
    Fidilip
    • 1,209
    • 2
    • 9
    • 3
    94
    votes
    1 answer

    Python: Maximum recursion depth exceeded

    I have the following recursion code, at each node I call sql query to get the nodes belong to the parent node. here is the error: Exception RuntimeError: 'maximum recursion depth exceeded' in
    add-semi-colons
    • 18,094
    • 55
    • 145
    • 232
    50
    votes
    10 answers

    Write a non-recursive traversal of a Binary Search Tree using constant space and O(n) run time

    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…
    user183037
    • 2,549
    • 4
    • 31
    • 42
    35
    votes
    16 answers

    Printing BFS (Binary Tree) in Level Order with Specific Formatting

    To begin with, this question is not a dup of this one, but builds on it. Taking the tree in that question as an example, 1 / \ 2 3 / / \ 4 5 6 How would you modify your program to print it so, 1 2 3 4 5 6 rather than the…
    viksit
    • 7,542
    • 9
    • 42
    • 54
    34
    votes
    15 answers

    Help me understand Inorder Traversal without using recursion

    I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion. This is what I've tried so…
    Srikanth
    • 11,780
    • 23
    • 72
    • 92
    30
    votes
    3 answers

    Traversing a n-ary tree without using recurrsion

    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); } }
    ako
    • 2,511
    • 3
    • 18
    • 11
    28
    votes
    5 answers

    Can we use Morris traversal for postorder?

    I have visited many websites but couldnt find any algorithm for Morris postOrder traversal. I know that we can use Morris algorithm for preOrder and inOrder.It will be of great help if someone points me to the postOrder Morris algorithm, if it…
    kingshuk
    • 472
    • 1
    • 4
    • 10
    26
    votes
    2 answers

    Uniqueness of Inorder, Preorder, and Postorder traversal with null elements

    We all know that different binary trees can have the same inorder, preorder, or postorder traversal. But if we were to include null elements into a preorder traversal, then result of the traversal would be unique as long as the trees are unique.…
    bli00
    • 2,215
    • 2
    • 19
    • 46
    26
    votes
    2 answers

    Traversing a tree of objects in c#

    I have a tree that consists of several objects, where each object has a name (string), id (int) and possibly an array of children that are of the same type. How do I go through the entire tree and print out all of the ids and names? I'm new to…
    BeefTurkey
    • 900
    • 4
    • 12
    • 13
    22
    votes
    14 answers

    Inorder tree traversal: Which definition is correct?

    I have the following text from an academic course I took a while ago about inorder traversal (they also call it pancaking) of a binary tree (not BST): Inorder tree traversal Draw a line around the outside of the tree. Start to the left of the…
    Chris S
    • 64,770
    • 52
    • 221
    • 239
    22
    votes
    11 answers

    Pre-order to post-order traversal

    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?
    Bobj-C
    • 5,276
    • 9
    • 47
    • 83
    21
    votes
    2 answers

    What is the time complexity of tree traversal?

    What is the time complexity of tree traversal, I'm sure it must be obvious but my poor brain can not work it out right now.
    new299
    • 804
    • 1
    • 7
    • 17
    1
    2 3
    55 56