Introduction to Java Programming 10th ed
**25.4 ( Implement preorder traversal without using recursion ) Implement the
preorder method in BST using a stack instead of recursion. Write a test program
that prompts the user to enter 10 integers, stores them in a BST, and
invokes the preorder method to display the elements.
Solution
package info.codeaddict.blog.tree.binary; import java.util.LinkedList; import java.util.Queue; /**
* @author codeaddict.info */ public class PreOrderWithoutRecursion implements Traversal {
@Override public void traverse(BinaryTree.Node root) { Queue queue = new LinkedList<>();
pushAllLeft(queue, root); while (!queue.isEmpty()) { BinaryTree.Node node = queue.poll();
System.out.println(node.value()); pushAllLeft(queue, node.right()); } } private void
pushAllLeft(Queue queue, BinaryTree.Node node) { while (node != null) { queue.add(node);
node = node.left(); } } }

More Related Content

PPTX
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
DOCX
Intro to Java Programming 10th ed- Implement the inorder- preorder- po.docx
PPTX
Vert.x - Reactive & Distributed [Devoxx version]
PPTX
Getting Started with Datatsax .Net Driver
ODP
Java Concurrency
PPT
比XML更好用的Java Annotation
PDF
AJUG April 2011 Cascading example
PDF
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
Intro to Java Programming 10th ed- Implement the inorder- preorder- po.docx
Vert.x - Reactive & Distributed [Devoxx version]
Getting Started with Datatsax .Net Driver
Java Concurrency
比XML更好用的Java Annotation
AJUG April 2011 Cascading example
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)

Similar to Introduction to Java Programming 10th ed --25-4 (Implement preorder t.docx (20)

PDF
Description (Part A) In this lab you will write a Queue implementati.pdf
PDF
Auto-GWT : Better GWT Programming with Xtend
ODP
Jersey Guice AOP
PPTX
Java concurrency
PPTX
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
PDF
Jdbc[1]
PDF
JDBC programming
PDF
Java7 New Features and Code Examples
PDF
package singlylinkedlist; public class Node { public String valu.pdf
PPTX
Java byte code in practice
PDF
Building Scalable Stateless Applications with RxJava
PDF
Reactive programming on Android
PDF
What is new in java 8 concurrency
PDF
Cocoa heads testing and viewcontrollers
PDF
Programming with ZooKeeper - A basic tutorial
PDF
Programming with ZooKeeper - A basic tutorial
PDF
Lecture10
PDF
Taming Core Data by Arek Holko, Macoscope
PPTX
Module-1 Updated Collection Framework.pptx
PPT
Sqlapi0.1
Description (Part A) In this lab you will write a Queue implementati.pdf
Auto-GWT : Better GWT Programming with Xtend
Jersey Guice AOP
Java concurrency
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
Jdbc[1]
JDBC programming
Java7 New Features and Code Examples
package singlylinkedlist; public class Node { public String valu.pdf
Java byte code in practice
Building Scalable Stateless Applications with RxJava
Reactive programming on Android
What is new in java 8 concurrency
Cocoa heads testing and viewcontrollers
Programming with ZooKeeper - A basic tutorial
Programming with ZooKeeper - A basic tutorial
Lecture10
Taming Core Data by Arek Holko, Macoscope
Module-1 Updated Collection Framework.pptx
Sqlapi0.1
Ad

More from diegor62 (20)

DOCX
Introductory Aocounting u 2014 1- Managerial information- accounting m.docx
DOCX
Indicate the functions of the agent process in a network operating sys.docx
DOCX
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
DOCX
internet domain names are linked to trademark issues- Technology makes.docx
DOCX
Internet network management framework as consisting of three major par.docx
DOCX
Internet Programming ORM This question uses the following two model cl.docx
DOCX
international ECO 305 course- how does culture embrace globalization a.docx
DOCX
International business is conducted outside of the home country- Natio.docx
DOCX
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
DOCX
Integrative Problems equire the integration of multiple concepts to fi.docx
DOCX
Insight Communications Promotes employees with good technical skills i.docx
DOCX
Infrared radiation is also called IR- Describe how it is different fro (1).docx
DOCX
Infrared radiation is also called IR- Describe how it is different fro.docx
DOCX
Initial Knowledge Check For each row in the table below- decide whethe.docx
DOCX
Is the ability of the human ear better or worse than the sonic capabil.docx
DOCX
Is my half equation and ionic equation from the answer key correct- I.docx
DOCX
Inorganic Chemistry - Wurtzite vs- Rock-Salt Structures Beryllium oxid.docx
DOCX
Is it time for IPI to reconsider its salesforce organization by produc.docx
DOCX
INITIALS- 22- Which of the following represents a combustion reaction-.docx
DOCX
Indonesian lenders are the most profitable business in the world- The.docx
Introductory Aocounting u 2014 1- Managerial information- accounting m.docx
Indicate the functions of the agent process in a network operating sys.docx
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
internet domain names are linked to trademark issues- Technology makes.docx
Internet network management framework as consisting of three major par.docx
Internet Programming ORM This question uses the following two model cl.docx
international ECO 305 course- how does culture embrace globalization a.docx
International business is conducted outside of the home country- Natio.docx
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
Integrative Problems equire the integration of multiple concepts to fi.docx
Insight Communications Promotes employees with good technical skills i.docx
Infrared radiation is also called IR- Describe how it is different fro (1).docx
Infrared radiation is also called IR- Describe how it is different fro.docx
Initial Knowledge Check For each row in the table below- decide whethe.docx
Is the ability of the human ear better or worse than the sonic capabil.docx
Is my half equation and ionic equation from the answer key correct- I.docx
Inorganic Chemistry - Wurtzite vs- Rock-Salt Structures Beryllium oxid.docx
Is it time for IPI to reconsider its salesforce organization by produc.docx
INITIALS- 22- Which of the following represents a combustion reaction-.docx
Indonesian lenders are the most profitable business in the world- The.docx
Ad

Recently uploaded (20)

PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
semiconductor packaging in vlsi design fab
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
Education and Perspectives of Education.pptx
PPTX
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
PDF
Climate and Adaptation MCQs class 7 from chatgpt
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
Empowerment Technology for Senior High School Guide
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
Module on health assessment of CHN. pptx
Journal of Dental Science - UDMY (2021).pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
What’s under the hood: Parsing standardized learning content for AI
semiconductor packaging in vlsi design fab
Complications of Minimal Access-Surgery.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Education and Perspectives of Education.pptx
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
Climate and Adaptation MCQs class 7 from chatgpt
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Empowerment Technology for Senior High School Guide
Computer Architecture Input Output Memory.pptx
Race Reva University – Shaping Future Leaders in Artificial Intelligence
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
B.Sc. DS Unit 2 Software Engineering.pptx
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Cambridge-Practice-Tests-for-IELTS-12.docx
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Module on health assessment of CHN. pptx

Introduction to Java Programming 10th ed --25-4 (Implement preorder t.docx

  • 1. Introduction to Java Programming 10th ed **25.4 ( Implement preorder traversal without using recursion ) Implement the preorder method in BST using a stack instead of recursion. Write a test program that prompts the user to enter 10 integers, stores them in a BST, and invokes the preorder method to display the elements. Solution package info.codeaddict.blog.tree.binary; import java.util.LinkedList; import java.util.Queue; /** * @author codeaddict.info */ public class PreOrderWithoutRecursion implements Traversal { @Override public void traverse(BinaryTree.Node root) { Queue queue = new LinkedList<>(); pushAllLeft(queue, root); while (!queue.isEmpty()) { BinaryTree.Node node = queue.poll(); System.out.println(node.value()); pushAllLeft(queue, node.right()); } } private void pushAllLeft(Queue queue, BinaryTree.Node node) { while (node != null) { queue.add(node); node = node.left(); } } }