Question

In: Computer Science

How could I implement the contain and min method for a tree in this code. import...

How could I implement the contain and min method for a tree in this code.


import java.util.List;
import edu.princeton.cs.algs4.*;


class ProgrammingAssignment1 {

    //////////////////// EXERCICE 1: TWO THREE TREES

    static public <Key extends Comparable<Key>>
    int size (NakedTree<Key> tree) {
      if (tree == null)
        return 0;
      int val = tree.getNKeys ();
      for (int i = 0; i <= tree.getNKeys (); ++i)
        val += size (tree.getChild (i));
      return val;
    }

    //////// EXERCICE 1.1: contains AND min

    static public <Key extends Comparable<Key>>
    boolean contains (NakedTree<Key> tree, Key key) {

       
    }

Solutions

Expert Solution

        public static <Key extends Comparable<Key>> Key min(NakedTree<Key> tree) {
                if(tree == null) {
                        return null;
                }
                Key min = null;
                for(int i=0; i<tree.getNKeys(); i++) {
                        if(min == null || min.compareTo(tree.getKey(i)) > 0) {
                                min = tree.getKey(i);
                        }
                }
                
                for (int i = 0; i <= tree.getNKeys(); ++i) {
                        Key m = min(tree.getChild(i));
                        if(m != null && min.compareTo(m) > 0) {
                                min = m;
                        }
                }
                return min;
        }

        static public <Key extends Comparable<Key>> boolean contains(NakedTree<Key> tree, Key key) {
                if(tree == null) {
                        return false;
                }
                for(int i=0; i<tree.getNKeys(); i++) {
                        if(key.compareTo(tree.getKey(i)) == 0) {
                                return true;
                        }
                }
                for (int i = 0; i <= tree.getNKeys(); ++i) {
                        if(contains(tree.getChild(i), key)) {
                                return true;
                        }
                }
                return false;           
        }
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

how could I implement an intStack class that has only a push and pop method? in...
how could I implement an intStack class that has only a push and pop method? in java of course.
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random;...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { Character char1 = new Orc("Grumlin"); Character char2 = new Elf("Therae"); int damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); char1.hasCastSpellSkill = true; damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage");...
the purpose of the code is to implement a stack how can i convert this code...
the purpose of the code is to implement a stack how can i convert this code in a way that these changes apply to it? // simplify the logic of the main // getline(cin, line) needs to be changed to a normal cin >> line; //make a function for 'list' // not allowed temporary stack (stack s2) //merge the catches (errors) // introduce another function for main // avoid repetitive code here is the code: #include #include #include #include #include...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
I have code for an AVL tree. I Have have a node class and tree class....
I have code for an AVL tree. I Have have a node class and tree class. I need a  node object that refrences the root(top) of the tree. my current code throws NullPointerException when I try to access the root properties. here is the important snippets of my code, it is in java. public class Node { private Node left=null,right=null; public int height=1; private String Item; private int balance=0; Node(String item) { this.Item=item; } } -------------------------------------------------- public class AVLTree { public...
Re-do Problem 1, but now implement the following method: i must use this code :- public...
Re-do Problem 1, but now implement the following method: i must use this code :- public static BagInterface intersection (BagInterface bagA, BagInterface bagB) and here how my java program must work This method must return a new bag which is the intersection of the two bags: bagA and bagB. An element appears in the intersection of two bags the minimum of the number of times it appears in either. For example, {1,1,2} ∩{1,1,2,2,3}= {1,1,2}. Do not forget to state the...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public class Main { public static void main(String[] args) { /* Let's create a family tree (for instance like one used on genealogy sites). For the sake of simplicity, assume an individual can have at most two children. If an individual has 1-2 children, they are considered a "tree". If an individual does not have children, they are considered a "person". With that in mind,...
I need to implement incrementalInserstionSort im stuck on that part import java.util.*; /** * This class...
I need to implement incrementalInserstionSort im stuck on that part import java.util.*; /** * This class represents chains of linked nodes that * can be sorted by a Shell sort. * * @author Charles Hoot * @author Frank M. Carrano * Modified by atb * @author YOUR NAME * @version 9/29/2020 */ public class ChainSort> { private Node firstNode; // reference to first node public ChainSort() { this.firstNode = null; } public void display() { Node currentNode = this.firstNode; while...
Python I am creating a class in python. Here is my code below: import csv import...
Python I am creating a class in python. Here is my code below: import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(obj.POP(line)) population.append(obj.POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip() The problem is, I get an error saying:  ...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT