What would the code be for the pseudocode list below. I have created a Stack Met
ID: 3529246 • Letter: W
Question
What would the code be for the pseudocode list below. I have created a Stack Method that this is suppose to work off of, in which the Stack Method works off of my LinkedList file. I am trying to code a program that will count the contiguous blocks of the same color in the image. I have to use a stack and a queue to count the blocks in a rubiks cube image in which i have already created. I have also created a Pixel class as well that is suppose to be implemented. I have came up with the following but i am unsure if this is right
Depth First Recursion
Explanation / Answer
public void dfs() { // DFS uses Stack data structure Stack stack = new Stack(); stack.push(this.rootNode); rootNode.visited=true; printNode(rootNode); while(!stack.isEmpty()) { Node node = (Node)s.peek(); Node child = getUnvisitedChildNode(n); if(child != null) { child.visited = true; printNode(child); s.push(child); } else { s.pop(); } } // Clear visited property of nodes clearNodes(); } } Class Node { Char data; Public Node(char c) { this.data=c; }BFSDFS.java Java Class Main { public void bfs() { // BFS uses Queue data structure Queue queue = new LinkedList(); queue.add(this.rootNode); printNode(this.rootNode); rootNode.visited = true; while(!queue.isEmpty()) { Node node = (Node)queue.remove(); Node child=null; while((child=getUnvisitedChildNode(node))!=null) { child.visited=true; printNode(child); queue.add(child); } } // Clear visited property of nodes clearNodes(); } public void dfs() { // DFS uses Stack data structure Stack stack = new Stack(); stack.push(this.rootNode); rootNode.visited=true; printNode(rootNode); while(!stack.isEmpty()) { Node node = (Node)s.peek(); Node child = getUnvisitedChildNode(n); if(child != null) { child.visited = true; printNode(child); s.push(child); } else { s.pop(); } } // Clear visited property of nodes clearNodes(); } } Class Node { Char data; Public Node(char c) { this.data=c; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.