Question

In: Computer Science

import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...

import java.util.Stack;
import java.util.ArrayList;
import java.util.Scanner;

class TreeNode{
int data;
ArrayList<TreeNode> children = new ArrayList<>();
TreeNode parent = null;
  
public TreeNode(int d){
data = d;
}
  
public TreeNode addChild(int d){
TreeNode n = new TreeNode(d);
n.setParent(this);
children.add(n);
return n;
}
  
public ArrayList<TreeNode> getChildren(){
return children;
}
  
public void setParent(TreeNode p){
parent = p;
}
  
public TreeNode getParent(){
return parent;
}
}

class Main {
public static void main(String[] args)
   {
       Scanner scan = new Scanner(System.in);
       int num;
       //Create a tree
       TreeNode root = new TreeNode(scan.nextInt());
TreeNode node1 = root.addChild(scan.nextInt());
TreeNode node11 = node1.addChild(scan.nextInt());
TreeNode node12 = node1.addChild(scan.nextInt());
TreeNode node2 = root.addChild(scan.nextInt());
TreeNode node21 = node2.addChild(scan.nextInt());
TreeNode node22 = node2.addChild(scan.nextInt());
       TreeNode node211 = node21.addChild(scan.nextInt());
       int newK = scan.nextInt();
       System.out.println(""+largerK(root, newK));
   }

   
public static int largerK(TreeNode n, int k) {
       //goal: Find the number of elements greater than k in the tree rooted in n
       //Example: if the elements in the tree are: 10, 20, 30, 40, 50, and k=25, the method should return: 3
  
      
   }

Solutions

Expert Solution

import java.util.Stack;
import java.util.ArrayList;
import java.util.Scanner;

class TreeNode{
    int data;
    ArrayList<TreeNode> children = new ArrayList<>();
    TreeNode parent = null;

    public TreeNode(int d){
        data = d;
    }

    public TreeNode addChild(int d){
        TreeNode n = new TreeNode(d);
        n.setParent(this);
        children.add(n);
        return n;
    }

    public ArrayList<TreeNode> getChildren(){
        return children;
    }

    public void setParent(TreeNode p){
        parent = p;
    }

    public TreeNode getParent(){
        return parent;
    }
}
class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num;
        //Create a tree
        TreeNode root = new TreeNode(scan.nextInt());
        TreeNode node1 = root.addChild(scan.nextInt());
        TreeNode node11 = node1.addChild(scan.nextInt());
        TreeNode node12 = node1.addChild(scan.nextInt());
        TreeNode node2 = root.addChild(scan.nextInt());
        TreeNode node21 = node2.addChild(scan.nextInt());
        TreeNode node22 = node2.addChild(scan.nextInt());
        TreeNode node211 = node21.addChild(scan.nextInt());
        int newK = scan.nextInt();
        int count=largerK(root,newK);
        if(root.data>newK)//check if the root data is greater that k then increment count by 1
            System.out.println("" + (count+1));
        else System.out.println(""+ count);
    }


    public static int largerK(TreeNode n, int k) {
        //goal: Find the number of elements greater than k in the tree rooted in n
        //Example: if the elements in the tree are: 10, 20, 30, 40, 50, and k=25, the method should return: 3
        int count=0;
        ArrayList<TreeNode> children=n.getChildren();
        //Traversing level wise and checking the values of children of node if it is greater than k and increment count
        for(int i=0;i<children.size();i++){
            if(children.get(i).data>k){
                count++;
            }
        }
        //Recursively calling same function for each child to get and find the values of nodes which are greater than k
        for(int i=0;i<children.size();i++){
            count+=largerK(children.get(i),k);
        }
        return count;
    }
}

Related Solutions

import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
PostFixEvaluator.java import java.util.Stack; import java.util.Scanner; /** * Represents an integer evaluator of postfix expressions. Assumes *...
PostFixEvaluator.java import java.util.Stack; import java.util.Scanner; /** * Represents an integer evaluator of postfix expressions. Assumes * the operands are constants. * * @author Java Foundations * @version 4.0 */ public class PostfixEvaluator {    private final static char ADD = '+';    private final static char SUBTRACT = '-';    private final static char MULTIPLY = '*';    private final static char DIVIDE = '/';    private Stack stack;    /**    * Sets up this evaluator by creating a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT