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 155779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:12:57+00:00 2026-05-11T10:12:57+00:00

When Traversing a Tree/Graph what is the difference between Breadth First and Depth first?

  • 0

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

  • 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-11T10:12:58+00:00Added an answer on May 11, 2026 at 10:12 am

    These two terms differentiate between two different ways of walking a tree.

    It is probably easiest just to exhibit the difference. Consider the tree:

        A    / \   B   C  /   / \ D   E   F 

    A depth first traversal would visit the nodes in this order

    A, B, D, C, E, F 

    Notice that you go all the way down one leg before moving on.

    A breadth first traversal would visit the node in this order

    A, B, C, D, E, F 

    Here we work all the way across each level before going down.

    (Note that there is some ambiguity in the traversal orders, and I’ve cheated to maintain the ‘reading’ order at each level of the tree. In either case I could get to B before or after C, and likewise I could get to E before or after F. This may or may not matter, depends on you application…)


    Both kinds of traversal can be achieved with the pseudocode:

    Store the root node in Container While (there are nodes in Container)    N = Get the 'next' node from Container    Store all the children of N in Container    Do some work on N 

    The difference between the two traversal orders lies in the choice of Container.

    • For depth first use a stack. (The recursive implementation uses the call-stack…)
    • For breadth-first use a queue.

    The recursive implementation looks like

    ProcessNode(Node)    Work on the payload Node    Foreach child of Node       ProcessNode(child)    /* Alternate time to work on the payload Node (see below) */ 

    The recursion ends when you reach a node that has no children, so it is guaranteed to end for finite, acyclic graphs.


    At this point, I’ve still cheated a little. With a little cleverness you can also work-on the nodes in this order:

    D, B, E, F, C, A 

    which is a variation of depth-first, where I don’t do the work at each node until I’m walking back up the tree. I have however visited the higher nodes on the way down to find their children.

    This traversal is fairly natural in the recursive implementation (use the ‘Alternate time’ line above instead of the first ‘Work’ line), and not too hard if you use a explicit stack, but I’ll leave it as an exercise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should remove the double quotes around the regex: "regex":… May 12, 2026 at 3:48 pm
  • Editorial Team
    Editorial Team added an answer Depends on the language. There are, and have been, all… May 12, 2026 at 3:48 pm
  • Editorial Team
    Editorial Team added an answer In two-sided relationships, one side "owns" the relationship, meaning that… May 12, 2026 at 3:48 pm

Related Questions

I am writing some tools for our build system to enforce some strict calling
I am writing a small class for driving integration testing of a win form
I have an interesting (at least to me) problem: I can't manage to find
The docs on this are pretty shoddy. There are a number of events you

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.