Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 180993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:41:00+00:00 2026-05-11T14:41:00+00:00

It’s been quite a while since I took data structures and algorithms in college,

  • 0

It’s been quite a while since I took data structures and algorithms in college, so I was surprised recently by a suggestion that recursion may not be the way ™ to do tree traversal. For some reason iterative, queue based traversal has not been a technique that I’ve ever used.

What, if any, are the advantages of iterative vs. recursive traversal? In what situations might I use one rather than the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:41:01+00:00Added an answer on May 11, 2026 at 2:41 pm

    If you are doing a breadth first search the natural implementation is to push nodes into a queue, not to use recursion.

    If you are doing a depth first search then recursion is the most natural way to code the traversal. However, unless your compiler optimizes tail recursion into iteration, your recursive implementation will be slower than an iterative algorithm, and will die with a stack overflow on a deep enough tree.

    Some quick Python to illustrate the difference:

    #A tree is a tuple of an int and a tree.  t = (1, (2,(4, (6), (7, (9)) )), (3, (5, (8)) )) def bfs(t):     to_visit = [t]     while len(to_visit) > 0:         c = to_visit[0]         if type(c) is int:             print c         else:              print c[0]             to_visit.append(c[1])             if len(c) > 2: to_visit.append(c[2])         to_visit = to_visit[1:]  def dfs(t):     if type(t) is int:         print t         return      print t[0]     dfs(t[1])     if len(t) > 2: dfs(t[2])   bfs(t) dfs(t) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 116k
  • Answers 116k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It depends. If Session state mode is defined as "inproc"… May 11, 2026 at 10:39 pm
  • Editorial Team
    Editorial Team added an answer There is no means of doing this automatically built into… May 11, 2026 at 10:39 pm
  • Editorial Team
    Editorial Team added an answer Interfaces define contracts. In the example you provide, the ??… May 11, 2026 at 10:39 pm

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
It seems to me obfuscation is an idea that falls somewhere in the security

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.