lecture18

# lecture18 - COMP 250 Winter 2010 18 - tree traversal Feb...

This preview shows pages 1–2. Sign up to view the full content.

COMP 250 Winter 2010 18 - tree traversal Feb 17, 2010 Tree traversal Often we wish to examine (or visit) all the nodes of the tree. This is called tree traversal , or traversing a tree. There are two aspects to traversing a tree. One is that we need to follow references (pointers) from parent to child. The second is that we need to “visit” the node. By “visit”, I mean doing some computation, e.g. getting or setting a ±eld of an element referenced by that node. There are several di²erent ways in which we can traverse a tree. They di²er in the order in which the nodes are visited. Depth frst traversal The ±rst two traversals we look at are called “depth ±rst”. In these traversals, a node and all its descendents are visited before the next sibling is visited. There are two ways to do depth- ±rst-traversal of a tree, depending on whether you visit a node before its descendents or after its descendents. In a preo-rder traversal, you visit a node, and then visit all its children. In a

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/25/2011 for the course COMP 250 taught by Professor Blanchette during the Spring '08 term at McGill.

### Page1 / 2

lecture18 - COMP 250 Winter 2010 18 - tree traversal Feb...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online