SlideShare a Scribd company logo
1 of 8
Download to read offline
Please help write BinaryTree.java Thank you!
Create a class BinaryTree. A class that implements the ADT binary tree.
import java.util.Iterator;
import java.util.NoSuchElementException;
import StackAndQueuePackage.*;
public class BinaryTree <T> implements BinaryTreeInterface<T>
{
private BinaryNode<T> root;
public BinaryTree()
{
root = null;
} // end default constructor
public BinaryTree(T rootData)
{
root = new BinaryNode<>(rootData);
} // end constructor
public BinaryTree(T rootData, BinaryTree<T> leftTree,
BinaryTree<T> rightTree)
{
privateSetTree(rootData, leftTree, rightTree);
} // end constructor
public void setTree(T rootData)
{
root = new BinaryNode<>(rootData);
} // end setTree
public void setTree(T rootData, BinaryTreeInterface<T> leftTree,
BinaryTreeInterface<T> rightTree)
{
privateSetTree(rootData, (BinaryTree<T>)leftTree,
(BinaryTree<T>)rightTree);
} // end setTree
private void privateSetTree(T rootData, BinaryTree<T> leftTree,
BinaryTree<T> rightTree)
{
root = new BinaryNode<>(rootData);
if ((leftTree != null) && !leftTree.isEmpty())
root.setLeftChild(leftTree.root);
if ((rightTree != null) && !rightTree.isEmpty())
{
if (rightTree != leftTree)
root.setRightChild(rightTree.root);
else
root.setRightChild(rightTree.root.copy());
} // end if
if ((leftTree != null) && (leftTree != this))
leftTree.clear();
if ((rightTree != null) && (rightTree != this))
rightTree.clear();
} // end privateSetTree
//...
private class PreorderIterator implements Iterator<T>
{
private StackInterface<BinaryNode<T>> nodeStack;
public PreorderIterator()
{
nodeStack = new LinkedStack<>();
if (root != null)
nodeStack.push(root);
} // end default constructor
public boolean hasNext()
{
return !nodeStack.isEmpty();
} // end hasNext
public T next()
{
BinaryNode<T> nextNode;
if (hasNext())
{
nextNode = nodeStack.pop();
BinaryNode<T> leftChild = nextNode.getLeftChild();
BinaryNode<T> rightChild = nextNode.getRightChild();
// Push into stack in reverse order of recursive calls
if (rightChild != null)
nodeStack.push(rightChild);
if (leftChild != null)
nodeStack.push(leftChild);
}
else
{
throw new NoSuchElementException();
}
return nextNode.getData();
} // end next
public void remove()
{
throw new UnsupportedOperationException();
} // end remove
} // end PreorderIterator
public void iterativePreorderTraverse ()
{
StackInterface<BinaryNode<T>> nodeStack = new LinkedStack<>();
if (root != null)
nodeStack.push(root);
BinaryNode<T> nextNode;
while (!nodeStack.isEmpty())
{
nextNode = nodeStack.pop();
BinaryNode<T> leftChild = nextNode.getLeftChild();
BinaryNode<T> rightChild = nextNode.getRightChild();
// Push into stack in reverse order of recursive calls
if (rightChild != null)
nodeStack.push(rightChild);
if (leftChild != null)
nodeStack.push(leftChild);
System.out.print(nextNode.getData() + " ");
} // end while
} // end iterativePreorderTraverse
private class InorderIterator implements Iterator<T>
{
private StackInterface<BinaryNode<T>> nodeStack;
private BinaryNode<T> currentNode;
public InorderIterator()
{
nodeStack = new LinkedStack<>();
currentNode = root;
} // end default constructor
public boolean hasNext()
{
return !nodeStack.isEmpty() || (currentNode != null);
} // end hasNext
public T next()
{
BinaryNode<T> nextNode = null;
// Find leftmost node with no left child
while (currentNode != null)
{
nodeStack.push(currentNode);
currentNode = currentNode.getLeftChild();
} // end while
// Get leftmost node, then move to its right subtree
if (!nodeStack.isEmpty())
{
nextNode = nodeStack.pop();
assert nextNode != null; // Since nodeStack was not empty
// before the pop
currentNode = nextNode.getRightChild();
}
else
throw new NoSuchElementException();
return nextNode.getData();
} // end next
public void remove()
{
throw new UnsupportedOperationException();
} // end remove
} // end InorderIterator
public void iterativeInorderTraverse ()
{
StackInterface<BinaryNode<T>> nodeStack = new LinkedStack<>();
BinaryNode<T> currentNode = root;
while (!nodeStack.isEmpty() || (currentNode != null))
{
// Find leftmost node with no left child
while (currentNode != null)
{
nodeStack.push(currentNode);
currentNode = currentNode.getLeftChild();
} // end while
// Visit leftmost node, then traverse its right subtree
if (!nodeStack.isEmpty())
{
BinaryNode<T> nextNode = nodeStack.pop();
assert nextNode != null; // Since nodeStack was not empty
// before the pop
System.out.print(nextNode.getData() + " ");
currentNode = nextNode.getRightChild();
} // end if
} // end while
} // end iterativeInorderTraverse
private class PostorderIterator implements Iterator<T>
{
private StackInterface<BinaryNode<T>> nodeStack;
private BinaryNode<T> currentNode;
public PostorderIterator()
{
nodeStack = new LinkedStack<>();
currentNode = root;
} // end default constructor
public boolean hasNext()
{
return !nodeStack.isEmpty() || (currentNode != null);
} // end hasNext
public T next()
{
BinaryNode<T> leftChild, rightChild, nextNode = null;
// Find leftmost leaf
while (currentNode != null)
{
nodeStack.push(currentNode);
leftChild = currentNode.getLeftChild();
if (leftChild == null)
currentNode = currentNode.getRightChild();
else
currentNode = leftChild;
} // end while
// Stack is not empty either because we just pushed a node, or
// it wasn't empty to begin with since hasNext() is true.
// But Iterator specifies an exception for next() in case
// hasNext() is false.
if (!nodeStack.isEmpty())
{
nextNode = nodeStack.pop();
// nextNode != null since stack was not empty before pop
BinaryNode<T> parent = null;
if (!nodeStack.isEmpty())
{
parent = nodeStack.peek();
if (nextNode == parent.getLeftChild())
currentNode = parent.getRightChild();
else
currentNode = null;
}
else
currentNode = null;
}
else
{
throw new NoSuchElementException();
} // end if
return nextNode.getData();
} // end next
public void remove()
{
throw new UnsupportedOperationException();
} // end remove
} // end PostorderIterator
private class LevelOrderIterator implements Iterator<T>
{
private QueueInterface<BinaryNode<T>> nodeQueue;
public LevelOrderIterator()
{
nodeQueue = new LinkedQueue<>();
if (root != null)
nodeQueue.enqueue(root);
} // end default constructor
public boolean hasNext()
{
return !nodeQueue.isEmpty();
} // end hasNext
public T next()
{
BinaryNode<T> nextNode;
if (hasNext())
{
nextNode = nodeQueue.dequeue();
BinaryNode<T> leftChild = nextNode.getLeftChild();
BinaryNode<T> rightChild = nextNode.getRightChild();
// Add to queue in order of recursive calls
if (leftChild != null)
nodeQueue.enqueue(leftChild);
if (rightChild != null)
nodeQueue.enqueue(rightChild);
}
else
{
throw new NoSuchElementException();
}
return nextNode.getData();
} // end next
public void remove()
{
throw new UnsupportedOperationException();
} // end remove
} // end LevelOrderIterator
} // end BinaryTree
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdf

More Related Content

Similar to Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf

Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
federaleyecare
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
harihelectronicspune
 
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
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
aksahnan
 
using the code below create a method called getCollisionCount that w.pdf
using the code below create a method called getCollisionCount that w.pdfusing the code below create a method called getCollisionCount that w.pdf
using the code below create a method called getCollisionCount that w.pdf
amirthagiftsmadurai
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
sivakumar19831
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
shanki7
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
anukoolelectronics
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
f3apparelsonline
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
MARRY7
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdf
ADITIEYEWEAR
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
mail931892
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf
arjuncp10
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdf
SANDEEPARIHANT
 

Similar to Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf (20)

Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.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
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
using the code below create a method called getCollisionCount that w.pdf
using the code below create a method called getCollisionCount that w.pdfusing the code below create a method called getCollisionCount that w.pdf
using the code below create a method called getCollisionCount that w.pdf
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
 
Linked lists
Linked listsLinked lists
Linked lists
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdf
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdf
 

More from info750646

please need help on those- You can use Excel- Word- or handwritten 1-.pdf
please need help on those- You can use Excel- Word- or handwritten 1-.pdfplease need help on those- You can use Excel- Word- or handwritten 1-.pdf
please need help on those- You can use Excel- Word- or handwritten 1-.pdf
info750646
 
Please make sure to cite your sourees- if you are turning in a paper c.pdf
Please make sure to cite your sourees- if you are turning in a paper c.pdfPlease make sure to cite your sourees- if you are turning in a paper c.pdf
Please make sure to cite your sourees- if you are turning in a paper c.pdf
info750646
 
Please help- I know it's alot but i could really use the help- I appre.pdf
Please help- I know it's alot but i could really use the help- I appre.pdfPlease help- I know it's alot but i could really use the help- I appre.pdf
Please help- I know it's alot but i could really use the help- I appre.pdf
info750646
 
Please help write the Caesar cipher- It must follow the these requirem.pdf
Please help write the Caesar cipher- It must follow the these requirem.pdfPlease help write the Caesar cipher- It must follow the these requirem.pdf
Please help write the Caesar cipher- It must follow the these requirem.pdf
info750646
 

More from info750646 (20)

Please provide an example of findings after reviewing and analyzing fi.pdf
Please provide an example of findings after reviewing and analyzing fi.pdfPlease provide an example of findings after reviewing and analyzing fi.pdf
Please provide an example of findings after reviewing and analyzing fi.pdf
 
Please help--- The schematic diagram below shows a progressive defor.pdf
Please help---   The schematic diagram below shows a progressive defor.pdfPlease help---   The schematic diagram below shows a progressive defor.pdf
Please help--- The schematic diagram below shows a progressive defor.pdf
 
Please show all of your work and explain in detail- I do not understan.pdf
Please show all of your work and explain in detail- I do not understan.pdfPlease show all of your work and explain in detail- I do not understan.pdf
Please show all of your work and explain in detail- I do not understan.pdf
 
Please note that multiple options may be correct- but there also may b.pdf
Please note that multiple options may be correct- but there also may b.pdfPlease note that multiple options may be correct- but there also may b.pdf
Please note that multiple options may be correct- but there also may b.pdf
 
please need help on those- You can use Excel- Word- or handwritten 1-.pdf
please need help on those- You can use Excel- Word- or handwritten 1-.pdfplease need help on those- You can use Excel- Word- or handwritten 1-.pdf
please need help on those- You can use Excel- Word- or handwritten 1-.pdf
 
Please match the following sensory receptors with their functions- A-.pdf
Please match the following sensory receptors with their functions- A-.pdfPlease match the following sensory receptors with their functions- A-.pdf
Please match the following sensory receptors with their functions- A-.pdf
 
Please match the following threats with its description- Programming r.pdf
Please match the following threats with its description- Programming r.pdfPlease match the following threats with its description- Programming r.pdf
Please match the following threats with its description- Programming r.pdf
 
Please make sure to cite your sourees- if you are turning in a paper c.pdf
Please make sure to cite your sourees- if you are turning in a paper c.pdfPlease make sure to cite your sourees- if you are turning in a paper c.pdf
Please make sure to cite your sourees- if you are turning in a paper c.pdf
 
please locate two different health messages (e-g- a pharmaceutical or.pdf
please locate two different health messages (e-g- a pharmaceutical or.pdfplease locate two different health messages (e-g- a pharmaceutical or.pdf
please locate two different health messages (e-g- a pharmaceutical or.pdf
 
Please label the following structures A- Tunica albuginea B- Seminifer.pdf
Please label the following structures A- Tunica albuginea B- Seminifer.pdfPlease label the following structures A- Tunica albuginea B- Seminifer.pdf
Please label the following structures A- Tunica albuginea B- Seminifer.pdf
 
Please help- What is the monetary base and how does it relate to the F.pdf
Please help- What is the monetary base and how does it relate to the F.pdfPlease help- What is the monetary base and how does it relate to the F.pdf
Please help- What is the monetary base and how does it relate to the F.pdf
 
Please help- This is from vertebrate zoology- I know the first questio.pdf
Please help- This is from vertebrate zoology- I know the first questio.pdfPlease help- This is from vertebrate zoology- I know the first questio.pdf
Please help- This is from vertebrate zoology- I know the first questio.pdf
 
Please help- I know it's alot but i could really use the help- I appre.pdf
Please help- I know it's alot but i could really use the help- I appre.pdfPlease help- I know it's alot but i could really use the help- I appre.pdf
Please help- I know it's alot but i could really use the help- I appre.pdf
 
Please help- I don't know what I'm doing wrong return - input tion.pdf
Please help- I don't know what I'm doing wrong    return - input tion.pdfPlease help- I don't know what I'm doing wrong    return - input tion.pdf
Please help- I don't know what I'm doing wrong return - input tion.pdf
 
please help- a- Give an NFA recognizing the language (01U001U010) b- C.pdf
please help- a- Give an NFA recognizing the language (01U001U010) b- C.pdfplease help- a- Give an NFA recognizing the language (01U001U010) b- C.pdf
please help- a- Give an NFA recognizing the language (01U001U010) b- C.pdf
 
Please help!!! A- A couple has a baby that is monosomic for the X.pdf
Please help!!!  A-    A couple has a baby that is monosomic for the X.pdfPlease help!!!  A-    A couple has a baby that is monosomic for the X.pdf
Please help!!! A- A couple has a baby that is monosomic for the X.pdf
 
please help! thank you!!! Question 14 (2 points) is a characteristic o.pdf
please help! thank you!!! Question 14 (2 points) is a characteristic o.pdfplease help! thank you!!! Question 14 (2 points) is a characteristic o.pdf
please help! thank you!!! Question 14 (2 points) is a characteristic o.pdf
 
Please help write the Caesar cipher- It must follow the these requirem.pdf
Please help write the Caesar cipher- It must follow the these requirem.pdfPlease help write the Caesar cipher- It must follow the these requirem.pdf
Please help write the Caesar cipher- It must follow the these requirem.pdf
 
please help with this problem thank you- M-L-K the maximally simplifie.pdf
please help with this problem thank you- M-L-K the maximally simplifie.pdfplease help with this problem thank you- M-L-K the maximally simplifie.pdf
please help with this problem thank you- M-L-K the maximally simplifie.pdf
 
Please Help with this Required information -The following information.pdf
Please Help with this  Required information -The following information.pdfPlease Help with this  Required information -The following information.pdf
Please Help with this Required information -The following information.pdf
 

Recently uploaded

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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)
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
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
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
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
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 

Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf

  • 1. Please help write BinaryTree.java Thank you! Create a class BinaryTree. A class that implements the ADT binary tree. import java.util.Iterator; import java.util.NoSuchElementException; import StackAndQueuePackage.*; public class BinaryTree <T> implements BinaryTreeInterface<T> { private BinaryNode<T> root; public BinaryTree() { root = null; } // end default constructor public BinaryTree(T rootData) { root = new BinaryNode<>(rootData); } // end constructor public BinaryTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree) { privateSetTree(rootData, leftTree, rightTree); } // end constructor public void setTree(T rootData) { root = new BinaryNode<>(rootData); } // end setTree public void setTree(T rootData, BinaryTreeInterface<T> leftTree, BinaryTreeInterface<T> rightTree) { privateSetTree(rootData, (BinaryTree<T>)leftTree, (BinaryTree<T>)rightTree); } // end setTree private void privateSetTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree) { root = new BinaryNode<>(rootData);
  • 2. if ((leftTree != null) && !leftTree.isEmpty()) root.setLeftChild(leftTree.root); if ((rightTree != null) && !rightTree.isEmpty()) { if (rightTree != leftTree) root.setRightChild(rightTree.root); else root.setRightChild(rightTree.root.copy()); } // end if if ((leftTree != null) && (leftTree != this)) leftTree.clear(); if ((rightTree != null) && (rightTree != this)) rightTree.clear(); } // end privateSetTree //... private class PreorderIterator implements Iterator<T> { private StackInterface<BinaryNode<T>> nodeStack; public PreorderIterator() { nodeStack = new LinkedStack<>(); if (root != null) nodeStack.push(root); } // end default constructor public boolean hasNext() { return !nodeStack.isEmpty(); } // end hasNext public T next() { BinaryNode<T> nextNode; if (hasNext()) { nextNode = nodeStack.pop(); BinaryNode<T> leftChild = nextNode.getLeftChild();
  • 3. BinaryNode<T> rightChild = nextNode.getRightChild(); // Push into stack in reverse order of recursive calls if (rightChild != null) nodeStack.push(rightChild); if (leftChild != null) nodeStack.push(leftChild); } else { throw new NoSuchElementException(); } return nextNode.getData(); } // end next public void remove() { throw new UnsupportedOperationException(); } // end remove } // end PreorderIterator public void iterativePreorderTraverse () { StackInterface<BinaryNode<T>> nodeStack = new LinkedStack<>(); if (root != null) nodeStack.push(root); BinaryNode<T> nextNode; while (!nodeStack.isEmpty()) { nextNode = nodeStack.pop(); BinaryNode<T> leftChild = nextNode.getLeftChild(); BinaryNode<T> rightChild = nextNode.getRightChild(); // Push into stack in reverse order of recursive calls if (rightChild != null) nodeStack.push(rightChild); if (leftChild != null) nodeStack.push(leftChild); System.out.print(nextNode.getData() + " "); } // end while } // end iterativePreorderTraverse
  • 4. private class InorderIterator implements Iterator<T> { private StackInterface<BinaryNode<T>> nodeStack; private BinaryNode<T> currentNode; public InorderIterator() { nodeStack = new LinkedStack<>(); currentNode = root; } // end default constructor public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); } // end hasNext public T next() { BinaryNode<T> nextNode = null; // Find leftmost node with no left child while (currentNode != null) { nodeStack.push(currentNode); currentNode = currentNode.getLeftChild(); } // end while // Get leftmost node, then move to its right subtree if (!nodeStack.isEmpty()) { nextNode = nodeStack.pop(); assert nextNode != null; // Since nodeStack was not empty // before the pop currentNode = nextNode.getRightChild(); } else throw new NoSuchElementException(); return nextNode.getData(); } // end next public void remove() { throw new UnsupportedOperationException(); } // end remove } // end InorderIterator
  • 5. public void iterativeInorderTraverse () { StackInterface<BinaryNode<T>> nodeStack = new LinkedStack<>(); BinaryNode<T> currentNode = root; while (!nodeStack.isEmpty() || (currentNode != null)) { // Find leftmost node with no left child while (currentNode != null) { nodeStack.push(currentNode); currentNode = currentNode.getLeftChild(); } // end while // Visit leftmost node, then traverse its right subtree if (!nodeStack.isEmpty()) { BinaryNode<T> nextNode = nodeStack.pop(); assert nextNode != null; // Since nodeStack was not empty // before the pop System.out.print(nextNode.getData() + " "); currentNode = nextNode.getRightChild(); } // end if } // end while } // end iterativeInorderTraverse private class PostorderIterator implements Iterator<T> { private StackInterface<BinaryNode<T>> nodeStack; private BinaryNode<T> currentNode; public PostorderIterator() { nodeStack = new LinkedStack<>(); currentNode = root; } // end default constructor public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); } // end hasNext public T next() { BinaryNode<T> leftChild, rightChild, nextNode = null; // Find leftmost leaf
  • 6. while (currentNode != null) { nodeStack.push(currentNode); leftChild = currentNode.getLeftChild(); if (leftChild == null) currentNode = currentNode.getRightChild(); else currentNode = leftChild; } // end while // Stack is not empty either because we just pushed a node, or // it wasn't empty to begin with since hasNext() is true. // But Iterator specifies an exception for next() in case // hasNext() is false. if (!nodeStack.isEmpty()) { nextNode = nodeStack.pop(); // nextNode != null since stack was not empty before pop BinaryNode<T> parent = null; if (!nodeStack.isEmpty()) { parent = nodeStack.peek(); if (nextNode == parent.getLeftChild()) currentNode = parent.getRightChild(); else currentNode = null; } else currentNode = null; } else { throw new NoSuchElementException(); } // end if return nextNode.getData(); } // end next public void remove() { throw new UnsupportedOperationException(); } // end remove } // end PostorderIterator private class LevelOrderIterator implements Iterator<T>
  • 7. { private QueueInterface<BinaryNode<T>> nodeQueue; public LevelOrderIterator() { nodeQueue = new LinkedQueue<>(); if (root != null) nodeQueue.enqueue(root); } // end default constructor public boolean hasNext() { return !nodeQueue.isEmpty(); } // end hasNext public T next() { BinaryNode<T> nextNode; if (hasNext()) { nextNode = nodeQueue.dequeue(); BinaryNode<T> leftChild = nextNode.getLeftChild(); BinaryNode<T> rightChild = nextNode.getRightChild(); // Add to queue in order of recursive calls if (leftChild != null) nodeQueue.enqueue(leftChild); if (rightChild != null) nodeQueue.enqueue(rightChild); } else { throw new NoSuchElementException(); } return nextNode.getData(); } // end next public void remove() { throw new UnsupportedOperationException(); } // end remove } // end LevelOrderIterator } // end BinaryTree