Question

In: Computer Science

Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...

Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is empty.

import java.util.*;

public class Node {
int data;
Node leftChild;
Node rightChild;
  
public Node(int data) {
this.data = data;
}
  
public Node(int data, Node leftChild, Node rightChild) {
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
  
public static List<Integer> valuesInLevelOrder(Node root) {
return null;
}

Solutions

Expert Solution

Refer to code below for the implementation of valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order

public static List<Integer> valuesInLevelOrder(Node root)
{
   Queue<Node> q = new LinkedList<Node>();
   int i = 0;
   List<Integer> levelOrder = new ArrayList<Integer>();
   q.add(root);
   while (!queue.isEmpty())
      {
        Node n = q.poll();
       levelOrder.add(i,n.data);
       i = i+1;

        if (n.left != null)
       {
            q.add(n.left);
        }

        if (n.right != null)
       {
            q.add(n.right);
        }
   }

   return levelOrder;
}


        


Related Solutions

java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers. You are essentially writing a method that will become part of the IntegerTree class. You may define private helper methods to solve this problem. Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch...
Assume that a given binary tree stores integer values in its nodes. Write a Java method...
Assume that a given binary tree stores integer values in its nodes. Write a Java method "smallest" that returns the smallest item in the tree.
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
10) A binary tree with N nodes is at least how deep? How deep is it...
10) A binary tree with N nodes is at least how deep? How deep is it at most? 12) A Binary Search Tree is a binary tree with what additional property? 13) Beginning with an empty binary search tree, insert the following values in the order given. Draw the tree at each step of the process. 1 10 5 20 22 7 14) Now delete the value 10 from the tree in question 13. Show each of two possible configurations...
Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
A binary tree model with 7 decision nodes will have how many terminal nodes?
A binary tree model with 7 decision nodes will have how many terminal nodes?
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT