SlideShare a Scribd company logo
Im suppose to make a preOrder transversal method so that it performs a preOrder transversal of
the tree.
For each node it traverses, it should call sb.append() to add a "[" the results of traversing the
subtree rooted at that node, and then a "]". You should add a space before the results of the left
and right child traversals, but only if the node exists.
This is what i have so far
public class BinarySearchTree> extends AbstractSet {
protected Entry root;
protected int size;
/**
* Initializes this BinarySearchTree object to be empty, to contain only
* elements of type E, to be ordered by the Comparable interface, and to
* contain no duplicate elements.
*/
public BinarySearchTree() {
root = null;
size = 0;
}
/**
* Initializes this BinarySearchTree object to contain a shallow copy of a
* specified BinarySearchTree object. The worstTime(n) is O(n), where n is the
* number of elements in the specified BinarySearchTree object.
*
* @param otherTree - the specified BinarySearchTree object that this
* BinarySearchTree object will be assigned a shallow copy of.
*/
public BinarySearchTree(BinarySearchTree otherTree) {
root = copy(otherTree.root, null);
size = otherTree.size;
}
/* Method to complete is here! */
public void preOrder(Entry ent, StringBuilder sb) {
// TODO: Complete me!
if(ent == null){
return;
}
if(ent.left != null){
sb.append("[");
preOrder(ent.left, sb);
}
if(ent.right != null){
sb.append("[");
preOrder(ent.right, sb);
}
sb.append("]");
}
But my preOrder method gives me these errors
The following hint(s) may help you locate some ways in which your solution may be improved:
--Pre-order traversal should process the root, then its left child (and exclude the non-existent
right child)! Expected [t [o]], but was []]
--Should just include the root's element when the tree only has 1 element! Expected [z], but was
]
--Pre-order traversal should process the root, ignore the non-existent left child, then process the
right child! Expected [w [z]]. but was []]
--Pre-order traversal should process the root, the left child, and then the right child! Expected [9
[0] [d]], but was [][]]
--Pre-order traversal should process the root, and then the left child. In processing the left child,
it should process that node first, and then process its children! Expected [d [9 [d]]], but was [[]]]
--Pre-order traversal should process the root, and then the right child. In processing the right
child, it should process that node first, and then process its children! Expected [v [w [z]]], but
was [[]]]
--Pre-order traversal should process a node and then recursively process each of its children!
Expected [v [d [9 [0] [d]] [j [g] [t [o]]]] [w [z]]], but was [[[][]][[][[]]]][[]]]
--Pre-order traversal should process a node and then recursively process each of its children!
Expected [v [d [9 [0] [d]] [j [g] [t]]]], but was [[[][]][[][]]]]
Solution
import java.util.Stack;
/* Class containing left and right child of current
node and key value*/
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
/* Class to print the inorder traversal */
class BinaryTree {
Node root;
void preorder() {
if (root == null) {
return;
}
//keep the nodes in the path that are waiting to be visited
Stack stack = new Stack();
Node node = root;
//first node to be visited will be the left one
while (node != null) {
stack.push(node);
node = node.left;
}
// traverse the tree
while (stack.size() > 0) {
// visit the top node
node = stack.pop();
System.out.print(node.data + " ");
if (node.right != null) {
node = node.right;
// the next node to be visited is the leftmost
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
}
public static void main(String args[]) {
/* creating a binary tree and entering
the nodes */
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.preorder();
}
}

More Related Content

Similar to Im suppose to make a preOrder transversal method so that it performs.pdf

ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
There is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdfThere is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdf
Dhanrajsolanki2091
 
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docx
ajoy21
 
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
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
archiesgallery
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
hardjasonoco14599
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
Shakil Ahmed
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
Zia Ush Shamszaman
 
A deque (pronounced deck) is an ordered set of items from which item.pdf
A deque (pronounced deck) is an ordered set of items from which item.pdfA deque (pronounced deck) is an ordered set of items from which item.pdf
A deque (pronounced deck) is an ordered set of items from which item.pdf
hardjasonoco14599
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
rock73
 
I need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdfI need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdf
arihantgiftgallery
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
illyasraja7
 
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
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
CourseHunt
 
Modify this code to do an Insert function for an AVL tree, instead o.pdf
Modify this code to do an Insert function for an AVL tree, instead o.pdfModify this code to do an Insert function for an AVL tree, instead o.pdf
Modify this code to do an Insert function for an AVL tree, instead o.pdf
fathimaoptical
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
charanjit1717
 
I have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdfI have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdf
allystraders
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
suchitrapoojari984
 

Similar to Im suppose to make a preOrder transversal method so that it performs.pdf (20)

ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
There is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdfThere is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdf
 
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docx
 
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
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
A deque (pronounced deck) is an ordered set of items from which item.pdf
A deque (pronounced deck) is an ordered set of items from which item.pdfA deque (pronounced deck) is an ordered set of items from which item.pdf
A deque (pronounced deck) is an ordered set of items from which item.pdf
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
I need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdfI need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdf
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
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
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Modify this code to do an Insert function for an AVL tree, instead o.pdf
Modify this code to do an Insert function for an AVL tree, instead o.pdfModify this code to do an Insert function for an AVL tree, instead o.pdf
Modify this code to do an Insert function for an AVL tree, instead o.pdf
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
 
Sorting
SortingSorting
Sorting
 
I have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdfI have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdf
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 

More from aniarihant

Give an explanation of the pedigree below. SolutionThe inherit.pdf
Give an explanation of the pedigree below.  SolutionThe inherit.pdfGive an explanation of the pedigree below.  SolutionThe inherit.pdf
Give an explanation of the pedigree below. SolutionThe inherit.pdf
aniarihant
 
elena ents you to impact into MAt ab SolutionThree fu.pdf
elena ents you to impact into MAt ab SolutionThree fu.pdfelena ents you to impact into MAt ab SolutionThree fu.pdf
elena ents you to impact into MAt ab SolutionThree fu.pdf
aniarihant
 
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdfEcosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
aniarihant
 
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdfCompare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
aniarihant
 
Assume that you have a garden and some pea plants have solid leaves a.pdf
Assume that you have a garden and some pea plants have solid leaves a.pdfAssume that you have a garden and some pea plants have solid leaves a.pdf
Assume that you have a garden and some pea plants have solid leaves a.pdf
aniarihant
 
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdfAccording to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
aniarihant
 
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdfA drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
aniarihant
 
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdfBill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
aniarihant
 
As a network administrator, what are some of the options you have fo.pdf
As a network administrator, what are some of the options you have fo.pdfAs a network administrator, what are some of the options you have fo.pdf
As a network administrator, what are some of the options you have fo.pdf
aniarihant
 
A concert has three pieces of music to be played before intermission.pdf
A concert has three pieces of music to be played before intermission.pdfA concert has three pieces of music to be played before intermission.pdf
A concert has three pieces of music to be played before intermission.pdf
aniarihant
 
5 of 50 students are female. If I randomly selects 10 students witho.pdf
5 of 50 students are female. If I randomly selects 10 students witho.pdf5 of 50 students are female. If I randomly selects 10 students witho.pdf
5 of 50 students are female. If I randomly selects 10 students witho.pdf
aniarihant
 
Which if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdfWhich if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdf
aniarihant
 
Write a assembly program for IA-32 to count up the number of even num.pdf
Write a assembly program for IA-32 to count up the number of even num.pdfWrite a assembly program for IA-32 to count up the number of even num.pdf
Write a assembly program for IA-32 to count up the number of even num.pdf
aniarihant
 
Which of the following is a weighted average of some elements having .pdf
Which of the following is a weighted average of some elements having .pdfWhich of the following is a weighted average of some elements having .pdf
Which of the following is a weighted average of some elements having .pdf
aniarihant
 
Which of the following statements isare true about the graph of the .pdf
Which of the following statements isare true about the graph of the .pdfWhich of the following statements isare true about the graph of the .pdf
Which of the following statements isare true about the graph of the .pdf
aniarihant
 
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
aniarihant
 
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
aniarihant
 
What does antibody titer meanSolution It is a method for t.pdf
What does antibody titer meanSolution  It is a method for t.pdfWhat does antibody titer meanSolution  It is a method for t.pdf
What does antibody titer meanSolution It is a method for t.pdf
aniarihant
 
What role do regular expressions typically play in language compiler.pdf
What role do regular expressions typically play in language compiler.pdfWhat role do regular expressions typically play in language compiler.pdf
What role do regular expressions typically play in language compiler.pdf
aniarihant
 
What is the role of ribosomes in an operon What is the role of.pdf
What is the role of ribosomes in an operon What is the role of.pdfWhat is the role of ribosomes in an operon What is the role of.pdf
What is the role of ribosomes in an operon What is the role of.pdf
aniarihant
 

More from aniarihant (20)

Give an explanation of the pedigree below. SolutionThe inherit.pdf
Give an explanation of the pedigree below.  SolutionThe inherit.pdfGive an explanation of the pedigree below.  SolutionThe inherit.pdf
Give an explanation of the pedigree below. SolutionThe inherit.pdf
 
elena ents you to impact into MAt ab SolutionThree fu.pdf
elena ents you to impact into MAt ab SolutionThree fu.pdfelena ents you to impact into MAt ab SolutionThree fu.pdf
elena ents you to impact into MAt ab SolutionThree fu.pdf
 
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdfEcosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
 
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdfCompare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
 
Assume that you have a garden and some pea plants have solid leaves a.pdf
Assume that you have a garden and some pea plants have solid leaves a.pdfAssume that you have a garden and some pea plants have solid leaves a.pdf
Assume that you have a garden and some pea plants have solid leaves a.pdf
 
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdfAccording to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
 
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdfA drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
 
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdfBill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
 
As a network administrator, what are some of the options you have fo.pdf
As a network administrator, what are some of the options you have fo.pdfAs a network administrator, what are some of the options you have fo.pdf
As a network administrator, what are some of the options you have fo.pdf
 
A concert has three pieces of music to be played before intermission.pdf
A concert has three pieces of music to be played before intermission.pdfA concert has three pieces of music to be played before intermission.pdf
A concert has three pieces of music to be played before intermission.pdf
 
5 of 50 students are female. If I randomly selects 10 students witho.pdf
5 of 50 students are female. If I randomly selects 10 students witho.pdf5 of 50 students are female. If I randomly selects 10 students witho.pdf
5 of 50 students are female. If I randomly selects 10 students witho.pdf
 
Which if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdfWhich if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdf
 
Write a assembly program for IA-32 to count up the number of even num.pdf
Write a assembly program for IA-32 to count up the number of even num.pdfWrite a assembly program for IA-32 to count up the number of even num.pdf
Write a assembly program for IA-32 to count up the number of even num.pdf
 
Which of the following is a weighted average of some elements having .pdf
Which of the following is a weighted average of some elements having .pdfWhich of the following is a weighted average of some elements having .pdf
Which of the following is a weighted average of some elements having .pdf
 
Which of the following statements isare true about the graph of the .pdf
Which of the following statements isare true about the graph of the .pdfWhich of the following statements isare true about the graph of the .pdf
Which of the following statements isare true about the graph of the .pdf
 
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
 
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
 
What does antibody titer meanSolution It is a method for t.pdf
What does antibody titer meanSolution  It is a method for t.pdfWhat does antibody titer meanSolution  It is a method for t.pdf
What does antibody titer meanSolution It is a method for t.pdf
 
What role do regular expressions typically play in language compiler.pdf
What role do regular expressions typically play in language compiler.pdfWhat role do regular expressions typically play in language compiler.pdf
What role do regular expressions typically play in language compiler.pdf
 
What is the role of ribosomes in an operon What is the role of.pdf
What is the role of ribosomes in an operon What is the role of.pdfWhat is the role of ribosomes in an operon What is the role of.pdf
What is the role of ribosomes in an operon What is the role of.pdf
 

Recently uploaded

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 

Im suppose to make a preOrder transversal method so that it performs.pdf

  • 1. Im suppose to make a preOrder transversal method so that it performs a preOrder transversal of the tree. For each node it traverses, it should call sb.append() to add a "[" the results of traversing the subtree rooted at that node, and then a "]". You should add a space before the results of the left and right child traversals, but only if the node exists. This is what i have so far public class BinarySearchTree> extends AbstractSet { protected Entry root; protected int size; /** * Initializes this BinarySearchTree object to be empty, to contain only * elements of type E, to be ordered by the Comparable interface, and to * contain no duplicate elements. */ public BinarySearchTree() { root = null; size = 0; } /** * Initializes this BinarySearchTree object to contain a shallow copy of a * specified BinarySearchTree object. The worstTime(n) is O(n), where n is the * number of elements in the specified BinarySearchTree object. * * @param otherTree - the specified BinarySearchTree object that this * BinarySearchTree object will be assigned a shallow copy of. */ public BinarySearchTree(BinarySearchTree otherTree) { root = copy(otherTree.root, null); size = otherTree.size; } /* Method to complete is here! */ public void preOrder(Entry ent, StringBuilder sb) { // TODO: Complete me! if(ent == null){ return;
  • 2. } if(ent.left != null){ sb.append("["); preOrder(ent.left, sb); } if(ent.right != null){ sb.append("["); preOrder(ent.right, sb); } sb.append("]"); } But my preOrder method gives me these errors The following hint(s) may help you locate some ways in which your solution may be improved: --Pre-order traversal should process the root, then its left child (and exclude the non-existent right child)! Expected [t [o]], but was []] --Should just include the root's element when the tree only has 1 element! Expected [z], but was ] --Pre-order traversal should process the root, ignore the non-existent left child, then process the right child! Expected [w [z]]. but was []] --Pre-order traversal should process the root, the left child, and then the right child! Expected [9 [0] [d]], but was [][]] --Pre-order traversal should process the root, and then the left child. In processing the left child, it should process that node first, and then process its children! Expected [d [9 [d]]], but was [[]]] --Pre-order traversal should process the root, and then the right child. In processing the right child, it should process that node first, and then process its children! Expected [v [w [z]]], but was [[]]] --Pre-order traversal should process a node and then recursively process each of its children! Expected [v [d [9 [0] [d]] [j [g] [t [o]]]] [w [z]]], but was [[[][]][[][[]]]][[]]] --Pre-order traversal should process a node and then recursively process each of its children! Expected [v [d [9 [0] [d]] [j [g] [t]]]], but was [[[][]][[][]]]] Solution
  • 3. import java.util.Stack; /* Class containing left and right child of current node and key value*/ class Node { int data; Node left, right; public Node(int item) { data = item; left = right = null; } } /* Class to print the inorder traversal */ class BinaryTree { Node root; void preorder() { if (root == null) { return; } //keep the nodes in the path that are waiting to be visited Stack stack = new Stack(); Node node = root; //first node to be visited will be the left one while (node != null) { stack.push(node); node = node.left; } // traverse the tree
  • 4. while (stack.size() > 0) { // visit the top node node = stack.pop(); System.out.print(node.data + " "); if (node.right != null) { node = node.right; // the next node to be visited is the leftmost while (node != null) { stack.push(node); node = node.left; } } } } public static void main(String args[]) { /* creating a binary tree and entering the nodes */ BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.preorder(); } }