SlideShare a Scribd company logo
1 of 3
Intro to Java Programming 10th ed-
Implement the inorder, preorder , postorder method in BST using a stack. Write a test
program that
prompts the user to enter 10 integers, stores them in a BST, and invokes the inorder method,
preorder method, postorder method to display the elements.
inorder method to display the elements.
Solution
import static org.junit.Assert.assertEquals;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import org.junit.Test;
public class BST {
public TreeNode root = null;
public TreeNode get(int element){
if(root == null){
return null;
}
TreeNode runner = root;
while (true){
if(runner.data > element){
if(runner.leftNode == null){
return null;
}
runner = runner.leftNode;
} else if(runner.data < element) {
if(runner.rightNode == null){
return null;
}
runner = runner.rightNode;
} else {
return runner;
}
}
}
public void insert(int element){
if(root == null){
root = new TreeNode(element);
return;
}
TreeNode runner = root;
while (runner.data != element){
if(runner.data > element){
if(runner.leftNode == null){
runner.leftNode = new TreeNode(element);
return;
}
runner = runner.leftNode;
} else {
if(runner.rightNode == null){
runner.rightNode = new TreeNode(element);
return;
}
runner = runner.rightNode;
}
}
}
public static void postOrderTraversal(TreeNode root){
if(root == null){
return;
}
postOrderTraversal(root.leftNode);
postOrderTraversal(root.rightNode);
System.out.print(root.data + " ");
}
public static void preOrderTraversal(TreeNode root){
if(root == null){
return;
}
System.out.print(root.data + " ");
preOrderTraversal(root.leftNode);
preOrderTraversal(root.rightNode);
}
public static void inOrderTraversal(TreeNode root){
if(root == null){
return ;
}
inOrderTraversal(root.leftNode);
System.out.print(root.data + " ");
inOrderTraversal(root.rightNode);
}
private class TreeNode {
public int data;
public TreeNode leftNode;
public TreeNode rightNode;
public boolean visited;
TreeNode(int data){
this.data = data;
}
}
public void basicTest(){
BST tree = new BST();
int[] data = {9, 5, 3, 1, 4, 8, 15, 11, 21, 20, 29};
for (int i : data){
tree.insert(i);
}
assertEquals(tree.get(3).data, 3);
}
}

More Related Content

Similar to Intro to Java Programming 10th ed- Implement the inorder- preorder- po.docx

Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
ย 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
NicholasflqStewartl
ย 
ppopoff
ppopoffppopoff
ppopoff
Paul Popoff
ย 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
Darwin Durand
ย 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
ย 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
fantasiatheoutofthef
ย 
Soundreader.classpathSoundreader.project Soundre.docx
Soundreader.classpathSoundreader.project  Soundre.docxSoundreader.classpathSoundreader.project  Soundre.docx
Soundreader.classpathSoundreader.project Soundre.docx
whitneyleman54422
ย 
database propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdf
database propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdfdatabase propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdf
database propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdf
fashiionbeutycare
ย 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
ย 

Similar to Intro to Java Programming 10th ed- Implement the inorder- preorder- po.docx (20)

Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
ย 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
ย 
DI Frameworks - hidden pearls
DI Frameworks - hidden pearlsDI Frameworks - hidden pearls
DI Frameworks - hidden pearls
ย 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
ย 
Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017
ย 
ppopoff
ppopoffppopoff
ppopoff
ย 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
ย 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
ย 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
ย 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
ย 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
ย 
Soundreader.classpathSoundreader.project Soundre.docx
Soundreader.classpathSoundreader.project  Soundre.docxSoundreader.classpathSoundreader.project  Soundre.docx
Soundreader.classpathSoundreader.project Soundre.docx
ย 
How Reactive do we need to be
How Reactive do we need to beHow Reactive do we need to be
How Reactive do we need to be
ย 
database propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdf
database propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdfdatabase propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdf
database propertiesjdbc.url=jdbcderbyBigJavaDB;create=true # .pdf
ย 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
ย 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
ย 
Unit Testing Front End JavaScript
Unit Testing Front End JavaScriptUnit Testing Front End JavaScript
Unit Testing Front End JavaScript
ย 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
ย 
Java Programs
Java ProgramsJava Programs
Java Programs
ย 
Why react matters
Why react mattersWhy react matters
Why react matters
ย 

More from olsenlinnea427

Assignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docxAssignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docx
olsenlinnea427
ย 
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docxAssignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
olsenlinnea427
ย 

More from olsenlinnea427 (20)

Assume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docxAssume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docx
ย 
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docxAssume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
ย 
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docxAssume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
ย 
Assume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docxAssume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docx
ย 
Assume a list has the following element- write a function to interch.docx
Assume a list has the following element-   write a function to interch.docxAssume a list has the following element-   write a function to interch.docx
Assume a list has the following element- write a function to interch.docx
ย 
Assume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docxAssume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docx
ย 
Assignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docxAssignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docx
ย 
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docxAssignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
ย 
As part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docxAs part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docx
ย 
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docxAs hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
ย 
As human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docxAs human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docx
ย 
Aries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docxAries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docx
ย 
Apple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docxApple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docx
ย 
Anthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docxAnthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docx
ย 
answer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docxanswer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docx
ย 
Answer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docxAnswer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docx
ย 
Answer the following based on a Blockchain and Biometrics I- What t.docx
Answer the following based on a Blockchain and Biometrics    I- What t.docxAnswer the following based on a Blockchain and Biometrics    I- What t.docx
Answer the following based on a Blockchain and Biometrics I- What t.docx
ย 
Anglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docxAnglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docx
ย 
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docxAngela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
ย 
Andrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docxAndrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docx
ย 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
ย 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
ย 

Recently uploaded (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
ย 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
ย 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
ย 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
ย 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
ย 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
ย 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
ย 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
ย 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
ย 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
ย 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
ย 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
ย 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
ย 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
ย 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
ย 

Intro to Java Programming 10th ed- Implement the inorder- preorder- po.docx

  • 1. Intro to Java Programming 10th ed- Implement the inorder, preorder , postorder method in BST using a stack. Write a test program that prompts the user to enter 10 integers, stores them in a BST, and invokes the inorder method, preorder method, postorder method to display the elements. inorder method to display the elements. Solution import static org.junit.Assert.assertEquals; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import org.junit.Test; public class BST { public TreeNode root = null; public TreeNode get(int element){ if(root == null){ return null; } TreeNode runner = root; while (true){ if(runner.data > element){ if(runner.leftNode == null){ return null; } runner = runner.leftNode; } else if(runner.data < element) { if(runner.rightNode == null){ return null; }
  • 2. runner = runner.rightNode; } else { return runner; } } } public void insert(int element){ if(root == null){ root = new TreeNode(element); return; } TreeNode runner = root; while (runner.data != element){ if(runner.data > element){ if(runner.leftNode == null){ runner.leftNode = new TreeNode(element); return; } runner = runner.leftNode; } else { if(runner.rightNode == null){ runner.rightNode = new TreeNode(element); return; } runner = runner.rightNode; } } } public static void postOrderTraversal(TreeNode root){ if(root == null){ return; } postOrderTraversal(root.leftNode); postOrderTraversal(root.rightNode); System.out.print(root.data + " "); } public static void preOrderTraversal(TreeNode root){ if(root == null){ return; } System.out.print(root.data + " ");
  • 3. preOrderTraversal(root.leftNode); preOrderTraversal(root.rightNode); } public static void inOrderTraversal(TreeNode root){ if(root == null){ return ; } inOrderTraversal(root.leftNode); System.out.print(root.data + " "); inOrderTraversal(root.rightNode); } private class TreeNode { public int data; public TreeNode leftNode; public TreeNode rightNode; public boolean visited; TreeNode(int data){ this.data = data; } } public void basicTest(){ BST tree = new BST(); int[] data = {9, 5, 3, 1, 4, 8, 15, 11, 21, 20, 29}; for (int i : data){ tree.insert(i); } assertEquals(tree.get(3).data, 3); } }