lecture-14

# cpsc 223 fall 2010 6 3 101910 traversing binary

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: int) •  We usually move top-to-bottom (“depth ﬁrst”) and left-to-right •  Traversal used in various tasks (insert, remove, display, etc.) CPSC 223  ­ ­ Fall 2010 6 3 10/19/10 Traversing binary trees D B A G C E H F Tree traversals are usually implemented using recursion –  Don’t need to know number of nodes, height, etc. –  It’s possible to traverse without using recursion (but tricky) But, when traveling a speciﬁc path … –  Can use recursion or iteration (loops) –  E.g., lookup and retrieve (typically simple loops) CPSC 223  ­ ­ Fall 2010 7 Traversing binary trees Root of T D B G Le< subtree of T A C E H Right subtree of T F Given a tree T, we have the following two cases: –  T is empty (we have nothing to traverse) –  T is not empty, and so we: 1.  Visit the root of T 2.  Visit the left subtree of T 3.  Visit the right subtree of T CPSC 223  ­ ­ Fall 2010 The basic structure of each type of tree traversal we’ll look at (preorder, inorder, postorder) 8 4 10/19/10 Depth-First, Left-to-Right Tree Traversal D B A G C E NULL NULL NULL NULL NULL H F NULL NULL NULL NULL Lets do a (preorder) traversal of this tree … –  T...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online