Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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.utiLArrayList; public interface BinaryNodeInterface { interface BinaryNodelnterface{

int getLeftChi = 0; } //Declare function to get value of node public T getData(); //Declare function to set value of node public void setData(T newData); //Declare function to get left child of current node public BinaryNodelnterface getLeftChild(); //Declare function to aet riaht child of current node public void setLeftChild(BinaryNodelnterface leftChild);

//Declare function to set right child of current node public void setRightChild(BinaryNodelnterface rightChild);

//Declare function to know current node has left child public boolean hasLeftChildO;

//Declare function to know current node has right child public boolean hasRightChild0;

//Declare function to know current node is a leaf node public boolean isLeaf();

//Declare function to know number of nodes

public int getNumberOfNodesO;

//Declare function to know height of tree public int getHeighto;

//Declare function to copy node

public BinaryNodelnterface copy();

interface Stacklnterface {

//Declare function to check stack is empty or not- public boolean isEmpty();

//Declare function to insert node into stack public void push(BinaryNodelnterface node); //Declare function to remove node from stack public BinaryNodelnterface pop();

//Declare function to point top element of stack public BinaryNodelnterface top();

}

interface Queuelnterface

{

//Declare function to check queue is empty or not. public boolean isEmpty();

//Declare function to insert node into queue public void enqueue(BinaryNodeInterface node); //Declare function to remove node from queue

public BinaryNodelnterface dequeue();

}

class LinkedQueue implements Queuelnterface

{

//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);

}

"Dene function to remove node from queue

public BinaryNodelnterface dequeue()

{ BinaryNodelnterface node =

(BinaryNodelnterface)arrNode.remove(0);

return node;

} }

class LinkedStack implements Stacklnterfaoe

{

//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);

} "Dene function to remove node from stack

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 implements BinaryNodelnterface

{

private T data;

private BinaryNode left;

private BinaryNode right;

public BinaryNodel)

{

this(null);

}

public BinaryNode(T dataPortion)

{ this(dataPortion,nulI,null);

}

public BinaryNode(T dataPortion,BinaryNode IeftChild,BinaryNode rightChild)

{

data = dataPortion;

left = leftChild;

right = rightChild;

}

//Function used to get value of node. public T getData()

{

return data;

}

//Function used to set value of node.

public void setDatafl(newData)

{

data = newData;

}

public BinaryNodelnterface getLeftChildo {

return left;

}

//Dene function to set left child of current node public void setLeftChild(BinaryNodelnterfaoe

leftChild)

{ left = (BinaryNode)IeftChild;

}

/lDene function to know current node has left child public boolean hasLeftChild()

{

return left != null;

}

public boolean isLeafo

{

return (left == null) && (right == null);

}

public BinaryNodelnterface getRightChiId()

{

return right;

}

public void setRightChild(BinaryNodelnterface rightChild)

{

right = (BinaryNode)rightChild;

}

public boolean hasRightChildO

{

return right != null;

}

public BinaryNodelnterface copy() {

return left;

}

public int getNumberOfNodesi)

{

int a = getNumberOfNodeshis);

return a;

}

piivate int getNumberOfNodes(BinaryNode node) {

ll declare and initialize variable number to 0

int number = 0;

Check if the node is null or not and if not, then call the recursive function with the child nodes as parameters

if (node != null)

number = 1 + getNumberONIOdesmodeJeft) + getNumberOfNodes(node.right);

/I return the calculated total

return number;

}

public int getHeighto

{

int leheight = 0;

int rightheight = 0;

if (left != null)

leftheight = le.getl-leight();

if (right != null)

n'ghtheight = right.getHeight0;

"calculate the height by adding 1 to the greater llheight among the left and the right node return 1 + Mathmaxlleftheight, n'ghtheight);

}

public String preorderTraversalo

{

Stacklnterfaoe> nodeStack =

new LinkedStack>(); BinaryNodelnterface currentNode = this; String result =

if (currentNode != null)

{

nodeStack.push(currentNode);

}

while(!nodeStack.isEmpty())

{

BinaryNodelnterface nextNode = nodeStack.pop();

assert nextNode != null;

result = result.concat(" ");

result = result.concat(Stnng.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 postorderTraversalo

{

Stacklnterface> nodeStack =

new LinkedStack>();

BinaryNodelnterface currentNode = this; BinaryNodelnterface prevNode = null; String result=";

if (currentNode != null)

{

nodeStack.push(currentNode);

}

while(!nodeStack.isEmpty())

{

if((prevNode == null)||(prevNode.getLeftChildO = currentNode)|| (prevNode.getRightChild()==currentNode))

{

boolean currentNodehasLeChildO; if (currentNodehasLeChildO) {

currentNode = currentNode.getLeftChi|d(); nodeStack.push(currentNode);

}

else if (currentNode.hasRightChiId())

{

currentNode =

currentNode.getRightChild0; nodeStack.push(currentNode);

}

else

{

nodeStack.pop();

result = result.concat(" ");

result = result.concat(Stnng.valueOf (currentNode.getData()));

prevNode= currentNode;

currentNode=nodeStack.top();

} }

else if (currentNode.getLeftChiId() = prevNode)

{ if (currentNodehasRightChildO)

{

currentNode = currentNode.getRightChild0;

nodeStack.push(currentNode);

prevNode = null; }

nodeStack.pop();

result = result.concat(" ");

result = result.concat(Stnng.valueOf (currentNode.getData())); prevNode= currentNode;

currentNode=nodeStack.top();

} }

Else if the current node's right child is already visite out

else if (currentNode.getRightChild() == prevNode)

{

nodeStack.pop();

result = result.concat(" ");

"concatenate the node in string variable result = result.concat(Stnng.valueOf (currentNode.getData()));

prevNode= currentNode;

if (!nodeStack.isEmpty())

currentNode=nodeStack.top();

} }

return result; }

1

public String levelorderTraversalO

{

Ilcreate an object of Queuelnterface Queuelnterface> nodeQueue = new LinkedQueue>();

Create an object of BinaryNodelnterface that will hold the current node and initia of the tree.

BinaryNodelnterface currentNode = this; String result=";

Ilcreate an object arr of type ArrayList java.util.ArrayList arr = new iava.util.ArrayList();

If root is not null then add to the queue and then remove it and add the current I

if (currentNode != null)

{

/linsert node into queue nodeQueue.enqueue(currentNode); /lremove node from queue nodeQueue.dequeue(); arr.add(currentNode);

result = result.concat(" ");

result = result.concat(Stnng.valueOf (currentNode.getData()));

}

/I if queue is empty and array is not empty

if ((nodeQueue.isEmpty() == true) && (!arr.isEmpty()))

{

/l|oop continue iterate till array is not empty while(!arr.isEmpty())

{

/lfor each node in the array add their /lchild nodes to queue

for(int i=0;i

{

Store ith index element into current node currentNode= (BinaryNodelnterface)arr.get(i); /luse if statement to check current node

/lhas left child if yes insert it into

if (wrrentNode.hasLeChild())

{

nodeQueue.enqueue (currentNode.getLeftChiId());

}

/luse if statement to check current node /lhas right child if yes insert it into queue

if (currentNodehasRightChildO) {

nodeQueue.enqueue

(currentNode.getRightChild());

} }

arr.clear(); /l loop continue iterate till the queue is

/lnot empty while(!nodeQueue.isEmpty())

{ dequeue each node and add to the array so that their child nodes can be added t

currentNode = nodeQueue.dequeue(); arr.add(currentNode); result = result.concat(" ");

result = result.concat(Stnng.valueOf

(currentNode.getData()));

} } }

return result;

/lmain method

public class lteratorlmplementation

{ public static void main(String|] args)

{

{

"Create 7 nodes of binary tree 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 nd'l = new BinaryNode('b'); BinaryNode nd8 = new BinaryNode('e'); Illink nodes to make a binary tree nd3.setRightChild(nd2); nd6.setLeftChild(nd3); nd7.setLeftChild(nd5);

nd7.setRightChild(nd4); nd8.setLeftChild(nd7); nd8.setRightChild(nd6);

/ICall getNumberofNodes() function to know /Inumber of nodes in tree System.out.println("Number of nodes in tree:"+nd8.getNumberOfNodesO);

/ICall preorderTraversal() function to display /lpreorder traversal of tree System.out.println("Preorder traversal: "+nd8.preorderTraversalO);

/ICall postorderTraversalo function to display /lpreorder traversal of tree

System.out.println("Postorder traversal:

"+nd8.IevelorderTraversal());

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

recognise typical interviewer errors and explain how to avoid them

Answered: 1 week ago

Question

identify and evaluate a range of recruitment and selection methods

Answered: 1 week ago

Question

understand the role of competencies and a competency framework

Answered: 1 week ago