Skip to main content
ICT
Lesson AB31 - Stacks and Queues
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

LAB ASSIGNMENT AB31.1 page 8 of 10

Inorder

Background:

Any recursive algorithm can be reduced to a linear sequence of events. It will be longer and more difficult to follow, but recursive solutions can be rewritten as iterative solutions if a stack is available for use. In this lab assignment, implement a non-recursive printInOrder method using a stack. After completing the lab, a greater appreciation for recursion and what it will accomplish for you should have been achieved.

You will work with the binary tree code that was implemented in Lesson AB30, Binary Trees. A non-recursive printInOrder method is summarized in pseudocode form below.

declare a Stack of TreeNode intitalized as empty
declare a temp TreeNode initialized to root

do
  while temp is not null
    push a copy of temp onto the stack
    set temp to temp’s left subtree

  if the stack is not empty
    pop the stack into temp
    print the contents of temp
    set temp to temp’s right subtree
until temp is null and the stack is empty

Assignment:

  1. Use the data file (file20.txt) from a previous binary tree lab to build the binary tree.

  2. Write the code for the non-recursive inorder method. Use (file20.txt) to test your program.

Instructions:

  1. Turn in your source code and a run output. The run output should consist of the inorder output of the binary tree.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.