Jump to content

Archived

This topic is now archived and is closed to further replies.

Nuv

Binary tree traversals

Recommended Posts

I am trying to display binary tree  level wise. Breadth-first tree traversal can be used in it.Can anyone point me towards its logic or point me towards a good tutorial.

Share this post


Link to post
Share on other sites

Breadth-first means looking at children before looking at grandchildren.

 

One simple way to do it is with a queue.

queue = array(root node)
while the queue is not empty { // not a foreach!
    node = the first item in the queue
    display the node
    for each child node {
        add the child node to the end of the queue
    }
}

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.