Question & Answer: Question: Using the examples in Figures 24-6 and 24-7 to suggest algorithms, implement iterator classes for preorder,…..

can any one please edit this code am getting lot of errors please

Question: Using the examples in Figures 24-6 and 24-7 to suggest algorithms, implement iterator classes for preorder,
postorder, and level-order traversals of a binary tree.Begin by writing iterative versions of the traversals similar to the method interativeInorderTraverse given in segment 24.13

code:

package iteratorimplementation;

import java.util.ArrayList;
interface BinaryNodeInterface<T> {
interface BinaryNodelnterface<T> {
int getLeftChi = 0;
boolean hasRightChild();
BinaryNodelnterface<T> getRightChild();
BinaryNodelnterface<T> getLeftChild();
char[] getData();
boolean hasLeftChild();
}
public T getData();
public void setData(T newData);
public BinaryNodelnterface<T> getLeftChild();
public void setLeftChild(BinaryNodelnterface<T> leftChild);
public void setRightChild(BinaryNodelnterface<T> rightChild);
public boolean hasLeftChild();
public boolean hasRightChild();
public boolean isLeaf();
public int getNumberOfNodes();
public int getHeight();
public BinaryNodelnterface<T> copy();
interface Stacklnterface<BinaryNodelnterfaoe> {
public boolean isEmpty();
public void push(BinaryNodelnterface node);
public BinaryNodelnterface pop();
public BinaryNodelnterface top();
}
interface Queuelnterface<BinaryNodelnterface> {
public boolean isEmpty();
public void enqueue(BinaryNodeInterface node);
public BinaryNodelnterface dequeue();
}
class LinkedQueue<BinaryNodelnterface> implements
Queuelnterface<BinaryNodelnterface> {
// create an object of arraylist
ArrayList arrNode = new ArrayList();
public boolean isEmpty() {
// use if statement to check size of array
if (arrNode.size() == 0)
return true;
else
return false;
}
public void enqueue(BinaryNodelnterface node) {
arrNode.add(node);
}
public BinaryNodelnterface dequeue() {
BinaryNodelnterface node = (BinaryNodelnterface) arrNode.remove(0);
return node;
}
@Override
public void enqueue(BinaryNodeInterface node) {
// TODO Auto-generated method stub
}
}
class LinkedStack<BinaryNodelnterfaoe> implements
Stacklnterface<BinaryNodelnterface> {
// Create an ArrayList
ArrayList arrNode = new ArrayList();
// Define function to check stack is empty or not.
public boolean isEmpty() {
// use if statement to check size of array
if (arrNode.size() == 0)
return true;
return false;
}
public void push(BinaryNodelnterface node) {
arrNode.add(node);
}
public BinaryNodelnterface pop() {
BinaryNodelnterface node = (BinaryNodelnterface) arrNode
.remove(arrNode.size() – 1);
return node;
}
public BinaryNodelnterface top() {
BinaryNodelnterface node = (BinaryNodelnterface) arrNode
.get(arrNode.size() – 1);
return node;
}
}
class BinaryNode<T> implements BinaryNodelnterface<T> {
private T data;
private BinaryNode<T> left;
private BinaryNode<T> right;
public BinaryNode() {
this(null);
}
public BinaryNode(T dataPortion) {
this(dataPortion, null, null);
}
public BinaryNode(T dataPortion, BinaryNode<T> leftChild,
BinaryNode<T> rightChild) {
data = dataPortion;
left = leftChild;
right = rightChild;
}
// Function used to set value of node.
public void setData(T newData) {
data = newData;
}
public BinaryNodelnterface<T> getLeftChild() {
return left;
}
public void setLeftChild(BinaryNodelnterface<T> leftChild) {
left = (BinaryNode<T>) leftChild;
}
public boolean hasLeftChild() {
return left != null;
}
public boolean isLeaf() {
return (left == null) && (right == null);
}
public BinaryNodelnterface<T> getRightChiId() {
return right;
}
public void setRightChild(BinaryNodelnterface<T> rightChild)
{
right = (BinaryNode<T>) rightChild;
}
public boolean hasRightChild() {
return right != null;
}
public BinaryNodelnterface<T> copy() {
return left;
}
public int getNumberOfNodes() {
int a = getNumberOfNodes(this);
return a;
}
private int getNumberOfNodes(BinaryNode<T> node) {
int number = 0;
if (node != null) {
number = 1 + getNumberOfNodes(node.left)
+ getNumberOfNodes(node.right);
return number;
}
return number;
}
public int getHeight() {
int leftheight = 0;
int rightheight = 0;
if (left != null)
leftheight = left.getHeight();
if (right != null)
rightheight = right.getHeight();
return 1 + Math.max(leftheight + rightheight, rightheight);
}
public String preorderTraversal() {
Stacklnterface<BinaryNodelnterface> nodeStack = new LinkedStack<BinaryNodelnterface<T>>();
BinaryNodelnterface<T> currentNode = this;
String result = “”;
if (currentNode != null) {
nodeStack.push(currentNode);
}
while (!nodeStack.isEmpty())
{
BinaryNodelnterface<T> nextNode = nodeStack.pop();
assert nextNode != null;
result = result.concat(” “);
result = result.concat(String.valueOf(nextNode.getData()));
if (nextNode.hasRightChild()) {
currentNode = nextNode.getRightChild();
nodeStack.push(currentNode);
}
if (nextNode.hasLeftChild()) {
currentNode = nextNode.getLeftChild();
nodeStack.push(currentNode);
}
}
return result;
}
public String postorderTraversal() {
Stacklnterface<BinaryNodelnterface> nodeStack = new LinkedStack<BinaryNodelnterface<T>>();
BinaryNodelnterface<T> currentNode = this;
BinaryNodelnterface<T> prevNode = null;
String result = “”;
if (currentNode != null) {
nodeStack.push(currentNode);
}
while (!nodeStack.isEmpty()) {
if ((prevNode == null)
|| (prevNode.getLeftChild() == currentNode)
|| (prevNode.getRightChild() == currentNode)) {
boolean currentNodehasLe?ChildO;
if (currentNode.hasLeftChild()) {
currentNode = currentNode.getLeftChild();
nodeStack.push(currentNode);
}
else if (currentNode.hasRightChild()) {
currentNode = currentNode.getRightChild();
nodeStack.push(currentNode);
}
else {
nodeStack.pop();
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode
.getData()));
prevNode = currentNode;
currentNode = nodeStack.top();
}
} else if (currentNode.getLeftChild() == prevNode) {
if (currentNode.hasRightChild()) {
currentNode = currentNode.getRightChild();
nodeStack.push(currentNode);
prevNode = null;
}
nodeStack.pop();
result = result.concat(” “);
result = result
.concat(String.valueOf(currentNode.getData()));
prevNode = currentNode;
currentNode = nodeStack.top();
}
}
if (currentNode.getRightChild() == prevNode) {
nodeStack.pop();
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode.getData()));
prevNode = currentNode;
if (!nodeStack.isEmpty()) {
currentNode = nodeStack.top();
}
}
return result;
}
public String levelorderTraversal() {
Queuelnterface<BinaryNodelnterface<T>> nodeQueue = new LinkedQueue<BinaryNodelnterface<T>>();
BinaryNodelnterface<T> currentNode = this;
String result = “”;
java.util.ArrayList arr = new ArrayList();
if (currentNode != null)
{
nodeQueue.enqueue((BinaryNodeInterface) currentNode);
nodeQueue.dequeue();
arr.add(currentNode);
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode.getData()));
}
if ((nodeQueue.isEmpty() == true) && (!arr.isEmpty())) {
while (!arr.isEmpty()) {
for (int i = 0; i < arr.size(); i++) {
currentNode = (BinaryNodelnterface<T>) arr.get(i);
if (currentNode.hasLeftChild()) {
nodeQueue.enqueue((BinaryNodeInterface) currentNode.getLeftChild());
}
if (currentNode.hasRightChild()) {
nodeQueue.enqueue((BinaryNodeInterface) currentNode.getRightChild());
}
}
arr.clear();
while (!nodeQueue.isEmpty()) {
currentNode = nodeQueue.dequeue();
arr.add(currentNode);
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode
.getData()));
}
}
}
return result;
}
public class lteratorlmplementation {
public void main(String[] args) {
BinaryNode nd2 = new BinaryNode(“g”);
BinaryNode nd3 = new BinaryNode(‘f’);
BinaryNode nd4 = new BinaryNode(‘e’);
BinaryNode nd5 = new BinaryNode(‘d’);
BinaryNode nd6 = new BinaryNode(‘c’);
BinaryNode nd7 = new BinaryNode(‘b’);
BinaryNode nd8 = new BinaryNode(‘e’);
nd3.setRightChild(nd2);
nd6.setLeftChild(nd3);
nd7.setLeftChild(nd5);
nd7.setRightChild(nd4);
nd8.setLeftChild(nd7);
nd8.setRightChild(nd6);
System.out.println(“Number of nodes intree:”
+ nd8.getNumberOfNodes());
System.out.println(“Preorder traversal:”
+ nd8.preorderTraversal());
System.out.println(“Postorder traversal: ”
+ nd8.levelorderTraversal());
}
}
@Override
public BinaryNodelnterface<T> getRightChild() {
// TODO Auto-generated method stub
return null;
}
@Override
public char[] getData() {
// TODO Auto-generated method stub
return null;
}
}
}

Expert Answer

 

Code:

import java.util.ArrayList;

interface BinaryNodeInterface<T> {
/*
interface BinaryNodelnterface<T> {

int getLeftChi = 0;

boolean hasRightChild();

BinaryNodelnterface<T> getRightChild();

BinaryNodelnterface<T> getLeftChild();

char[] getData();

boolean hasLeftChild();
}
*/

public T getData();

public void setData(T newData);

public BinaryNodeInterface<T> getLeftChild();

public BinaryNodeInterface<T> getRightChild();

public void setLeftChild(BinaryNodeInterface<T> leftChild);

public void setRightChild(BinaryNodeInterface<T> rightChild);

public boolean hasLeftChild();

public boolean hasRightChild();

public boolean isLeaf();

public int getNumberOfNodes();

public int getHeight();

public BinaryNodeInterface<T> copy();
}

interface Stacklnterface<BinaryNodelnterfaoe> {

public boolean isEmpty();

public void push(BinaryNodeInterface node);

public BinaryNodeInterface pop();

public BinaryNodeInterface top();
}

interface Queuelnterface<BinaryNodeInterface> {

public boolean isEmpty();

public void enqueue(BinaryNodeInterface node);

public BinaryNodeInterface dequeue();
}

class LinkedQueue<BinaryNodeInterface> implements
Queuelnterface<BinaryNodeInterface> {
// create an object of arraylist

ArrayList arrNode = new ArrayList();

@Override
public boolean isEmpty() {
// use if statement to check size of array
return arrNode.isEmpty();
}

@Override
public void enqueue(BinaryNodeInterface node) {
arrNode.add(node);
}

@Override
public BinaryNodeInterface dequeue() {
BinaryNodeInterface node = (BinaryNodeInterface) arrNode.remove(0);
return node;
}

}

class LinkedStack<BinaryNodelnterfaoe> implements
Stacklnterface<BinaryNodeInterface> {
// Create an ArrayList

ArrayList arrNode = new ArrayList();
// Define function to check stack is empty or not.

@Override
public boolean isEmpty() {
// use if statement to check size of array
if (arrNode.isEmpty()) {
return true;
}
return false;
}

@Override
public void push(BinaryNodeInterface node) {
arrNode.add(node);
}

@Override
public BinaryNodeInterface pop() {
BinaryNodeInterface node = (BinaryNodeInterface) arrNode
.remove(arrNode.size() – 1);
return node;
}

@Override
public BinaryNodeInterface top() {
BinaryNodeInterface node = (BinaryNodeInterface) arrNode
.get(arrNode.size() – 1);
return node;
}
}

class BinaryNode<T> implements BinaryNodeInterface<T> {

private T data;
private BinaryNode<T> left;
private BinaryNode<T> right;

public BinaryNode() {
this(null);
}

public BinaryNode(T dataPortion) {
this(dataPortion, null, null);
}

public BinaryNode(T dataPortion, BinaryNode<T> leftChild,
BinaryNode<T> rightChild) {
data = dataPortion;
left = leftChild;
right = rightChild;
}
// Function used to set value of node.

@Override
public void setData(T newData) {
data = newData;
}

@Override
public BinaryNodeInterface<T> getLeftChild() {
return left;
}

@Override
public void setLeftChild(BinaryNodeInterface<T> leftChild) {
left = (BinaryNode<T>) leftChild;
}

@Override
public boolean hasLeftChild() {
return left != null;
}

@Override
public boolean isLeaf() {
return (left == null) && (right == null);
}

public BinaryNodeInterface<T> getRightChiId() {
return right;
}

@Override
public void setRightChild(BinaryNodeInterface<T> rightChild) {
right = (BinaryNode<T>) rightChild;
}

@Override
public boolean hasRightChild() {
return right != null;
}

@Override
public BinaryNodeInterface<T> copy() {
return left;
}

@Override
public int getNumberOfNodes() {
int a = getNumberOfNodes(this);
return a;
}

private int getNumberOfNodes(BinaryNode<T> node) {
int number = 0;
if (node != null) {
number = 1 + getNumberOfNodes(node.left)
+ getNumberOfNodes(node.right);
return number;
}
return number;
}

@Override
public int getHeight() {
int leftheight = 0;
int rightheight = 0;
if (left != null) {
leftheight = left.getHeight();
}
if (right != null) {
rightheight = right.getHeight();
}
return 1 + Math.max(leftheight + rightheight, rightheight);
}

public String preorderTraversal() {
Stacklnterface<BinaryNodeInterface> nodeStack = new LinkedStack<>();
BinaryNodeInterface<T> currentNode = this;
String result = “”;
if (currentNode != null) {
nodeStack.push(currentNode);
}
while (!nodeStack.isEmpty()) {
BinaryNodeInterface<T> nextNode = nodeStack.pop();
assert nextNode != null;
result = result.concat(” “);
result = result.concat(String.valueOf(nextNode.getData()));
if (nextNode.hasRightChild()) {
currentNode = nextNode.getRightChild();
nodeStack.push(currentNode);
}
if (nextNode.hasLeftChild()) {
currentNode = nextNode.getLeftChild();
nodeStack.push(currentNode);
}
}
return result;
}

public String postorderTraversal() {
Stacklnterface<BinaryNodeInterface> nodeStack = new LinkedStack<>();
BinaryNodeInterface<T> currentNode = this;
BinaryNodeInterface<T> prevNode = null;
String result = “”;
if (currentNode != null) {
nodeStack.push(currentNode);
}
while (!nodeStack.isEmpty()) {
if ((prevNode == null)
|| (prevNode.getLeftChild() == currentNode)
|| (prevNode.getRightChild() == currentNode)) {
if (currentNode.hasLeftChild()) {
currentNode = currentNode.getLeftChild();
nodeStack.push(currentNode);
} else if (currentNode.hasRightChild()) {
currentNode = currentNode.getRightChild();
nodeStack.push(currentNode);
} else {
nodeStack.pop();
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode
.getData()));
prevNode = currentNode;
currentNode = nodeStack.top();
}
} else if (currentNode.getLeftChild() == prevNode) {
if (currentNode.hasRightChild()) {
currentNode = currentNode.getRightChild();
nodeStack.push(currentNode);
}
nodeStack.pop();
result = result.concat(” “);
result = result
.concat(String.valueOf(currentNode.getData()));
prevNode = currentNode;
currentNode = nodeStack.top();
}
}
if (currentNode.getRightChild() == prevNode) {
nodeStack.pop();
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode.getData()));
prevNode = currentNode;
if (!nodeStack.isEmpty()) {
currentNode = nodeStack.top();
}
}
return result;
}

public String levelorderTraversal() {
Queuelnterface<BinaryNodeInterface<T>> nodeQueue = new LinkedQueue<>();
BinaryNodeInterface<T> currentNode = this;
String result = “”;
java.util.ArrayList arr = new ArrayList();
if (currentNode != null) {
nodeQueue.enqueue((BinaryNodeInterface) currentNode);
nodeQueue.dequeue();
arr.add(currentNode);
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode.getData()));
}
if ((nodeQueue.isEmpty() == true) && (!arr.isEmpty())) {
while (!arr.isEmpty()) {
for (int i = 0; i < arr.size(); i++) {
currentNode = (BinaryNodeInterface<T>) arr.get(i);
if (currentNode.hasLeftChild()) {
nodeQueue.enqueue((BinaryNodeInterface) currentNode.getLeftChild());
}
if (currentNode.hasRightChild()) {
nodeQueue.enqueue((BinaryNodeInterface) currentNode.getRightChild());
}
}
arr.clear();
while (!nodeQueue.isEmpty()) {
currentNode = nodeQueue.dequeue();
arr.add(currentNode);
result = result.concat(” “);
result = result.concat(String.valueOf(currentNode
.getData()));
}
}
}
return result;
}

@Override
public T getData() {
return data;
}

@Override
public BinaryNodeInterface<T> getRightChild() {
return right;
}
}

public class lteratorlmplementation {

public static void main(String[] args) {
BinaryNode nd2 = new BinaryNode(“g”);
BinaryNode nd3 = new BinaryNode(‘f’);
BinaryNode nd4 = new BinaryNode(‘e’);
BinaryNode nd5 = new BinaryNode(‘d’);
BinaryNode nd6 = new BinaryNode(‘c’);
BinaryNode nd7 = new BinaryNode(‘b’);
BinaryNode nd8 = new BinaryNode(‘e’);
nd3.setRightChild(nd2);
nd6.setLeftChild(nd3);
nd7.setLeftChild(nd5);
nd7.setRightChild(nd4);
nd8.setLeftChild(nd7);
nd8.setRightChild(nd6);
System.out.println(“Number of nodes intree:”
+ nd8.getNumberOfNodes());
System.out.println(“Preorder traversal:”
+ nd8.preorderTraversal());
System.out.println(“Postorder traversal: ”
+ nd8.levelorderTraversal());
}
}

Output:

Question & Answer: Question: Using the examples in Figures 24-6 and 24-7 to suggest algorithms, implement iterator classes for preorder,..... 1

Still stressed from student homework?
Get quality assistance from academic writers!