lecture-14

# Traverse to the right subtree of d and so on cpsc 223

This preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: y, so nothing to visit –  We’re done going left and right … CPSC 223  ­ ­ Fall 2010 21 Depth-First, Left-to-Right Tree Traversal previous subtree current subtree A D B G C E NULL NULL NULL NULL NULL H F NULL NULL NULL NULL 12. Finished traversing the right subtree of B –  Now go back to the previous subtree CPSC 223  ­ ­ Fall 2010 22 11 10/19/10 Depth-First, Left-to-Right Tree Traversal previous subtree D current subtree B A G C E NULL NULL NULL NULL NULL H F NULL NULL NULL NULL 14. Traverse to the right subtree of D And so on … CPSC 223  ­ ­ Fall 2010 23 Traversing binary trees D The basic (recursive) algorithm is: B A void traverse(const Node* subTreeRoot) { G C E H F if(subTreeRoot == NULL) Note const argument return; traverse(subtreeRoot->leftChild); … we’re assuming we are traverse(subtreeRoot->rightChild); } CPSC 223  ­ ­ Fall 2010 not modifying the tree … otherwise we wouldn’t use const here 24 12 10/19/10 Traversing binary trees OUTPUT: D B A C G E F H 1 When should we “visit” a node? •  In a “preorder” traversal 2 D B G –  Visit the node ﬁrst 5 6 A C 3 –  Then...
View Full Document

## This document was uploaded on 03/18/2014 for the course CPSC 223 at Gonzaga.

Ask a homework question - tutors are online