Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Tree traversal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Breadth-first search=== Also, listed below is pseudocode for a simple [[Queue (data structure)|queue]] based level-order traversal, and will require space proportional to the maximum number of nodes at a given depth. This can be as much as half the total number of nodes. A more space-efficient approach for this type of traversal can be implemented using an [[iterative deepening depth-first search]]. '''procedure''' levelorder(node) queue β '''empty queue''' queue.enqueue(node) '''while''' '''not''' queue.isEmpty() node β queue.dequeue() visit(node) '''if''' node.left β '''null''' queue.enqueue(node.left) '''if''' node.right β '''null''' queue.enqueue(node.right) If the tree is represented by an array (first index is 0), it is sufficient iterating through all elements: '''procedure''' levelorder(array) '''for''' i '''from''' 0 '''to''' array.size visit(array[i])
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)