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.
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.
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 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++
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT