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.util.ArrayList; interface BinaryNodeInterface { interface BinaryNodelnterface { int getLeftChi = 0; boolean hasRightChild(); BinaryNodelnterface getRightChild(); BinaryNodelnterface getLeftChild(); char[] getData(); boolean hasLeftChild(); } public T getData(); public void setData(T newData); public BinaryNodelnterface getLeftChild(); public void setLeftChild(BinaryNodelnterface leftChild); public void setRightChild(BinaryNodelnterface rightChild); public boolean hasLeftChild(); public boolean hasRightChild(); public boolean isLeaf(); public int getNumberOfNodes(); public int getHeight(); public BinaryNodelnterface copy(); interface Stacklnterface { public boolean isEmpty(); public void push(BinaryNodelnterface node); public BinaryNodelnterface pop(); public BinaryNodelnterface top(); } interface Queuelnterface { public boolean isEmpty(); public void enqueue(BinaryNodeInterface node); 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); } public BinaryNodelnterface dequeue() { BinaryNodelnterface node = (BinaryNodelnterface) arrNode.remove(0); return node; } @Override public void enqueue(BinaryNodeInterface node) { // TODO Auto-generated method stub } } class LinkedStack implements Stacklnterface { // 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 implements BinaryNodelnterface { private T data; private BinaryNode left; private BinaryNode right; public BinaryNode() { this(null); } public BinaryNode(T dataPortion) { this(dataPortion, null, null); } public BinaryNode(T dataPortion, BinaryNode leftChild, BinaryNode rightChild) { data = dataPortion; left = leftChild; right = rightChild; } // Function used to set value of node. public void setData(T newData) { data = newData; } public BinaryNodelnterface getLeftChild() { return left; } public void setLeftChild(BinaryNodelnterface leftChild) { left = (BinaryNode) leftChild; } public boolean hasLeftChild() { return left != null; } public boolean isLeaf() { return (left == null) && (right == null); } public BinaryNodelnterface getRightChiId() { return right; } public void setRightChild(BinaryNodelnterface rightChild) { right = (BinaryNode) rightChild; } public boolean hasRightChild() { return right != null; } public BinaryNodelnterface copy() { return left; } public int getNumberOfNodes() { int a = getNumberOfNodes(this); return a; } private int getNumberOfNodes(BinaryNode 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 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(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 nodeStack = new LinkedStack>(); BinaryNodelnterface currentNode = this; BinaryNodelnterface 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> nodeQueue = new LinkedQueue>(); BinaryNodelnterface 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) 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 getRightChild() { // TODO Auto-generated method stub return null; } @Override public char[] getData() { // TODO Auto-generated method stub return null; } } }

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

Recommended Textbook for

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions

Question

What is the basic concept of an equal tangent sag/crest curve

Answered: 1 week ago

Question

3. Describe the communicative power of group affiliations

Answered: 1 week ago