• No results found

Problem 1 (2.5 points)

N/A
N/A
Protected

Academic year: 2021

Share "Problem 1 (2.5 points)"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

University Carlos III of Madrid Department of Telematic Engineering Systems Programming, May 23rd, 2011

Bachelors in Telecommunication Technology Engineering, Communications Systems Engineering, Audiovisual Systems Engineering and Telematics Engineering Duration: 90 minutes

Instructions for the exam:

• Books and notes are not allowed.

• Please write your name, surname, NIA and group on all pages.

Problem 1 (2.5 points)

Context:

We have a simple linked list, implemented by MyLinkedList and Node classes:

Section 1 (1.25 points)

Implement the method

public class MyLinkedList { private Node first; private int numElems;

MyLinkedList (){ first=null; numElems=0; }

public int getSize(){return numElems;}

public Object retrieveNth (int pos){

// To implement

}

public int insertNth (Object o, int pos){

// To implement

} }

public class Node {

private Object info; private Node next;

Node (Object obj) {info=obj; next=null;}

public void setInfo(Object obj){ info=obj; } public void setNext(Node n){ next=n; }

public Object getInfo() {return info;} public Node getNext(){return next;}

(2)

University Carlos III of Madrid Department of Telematic Engineering Systems Programming, May 23rd, 2011

Bachelors in Telecommunication Technology Engineering, Communications Systems Engineering, Audiovisual Systems Engineering and Telematics Engineering

from MyLinkedList class. This method returns the element placed at the ”pos”-th position. If the position specified does not exist in the list (i.e. the position is out of bounds), the method must return null.

Positions start at index 1. The method must not delete any item from the list.

Section 2 (1.25 points)

Implement the method

public int insertNth (Object obj, int pos)

from MyLinkedList class.

This method takes as parameters both the object to be inserted and the position at which the object will be inserted. Positions start at index 1.

Inserting an object at a specific position means that this object must be placed at that position. It might be necessary to shift other elements. For example, if there is an object to be inserted at position 3, then the element that was at position 3 before the insertion will be shifted to position 4.

If the position specified is higher than the number of elements in the list, the object must be added at the end of the list (e.g. if the list contains 4 elements, and the position at which the new element is wanted to be added is 10, then the new element will be actually inserted at position 5).

If the position specified is 0 or lower than 0, then no insertion must be done.

The method must return the position at which the new element is finally inserted (the case in which pos>list size must be taken into account), or -1 is no insertion is made.

Problem 2

(2.5 points)

Context:

We have a binary search tree representing a library (Library class). Each tree node stores information about a book: title, author and ISBN code. The tree is sorted alphabetically by book title in increasing order. We can assume there will not be two books with the same title.

(3)

University Carlos III of Madrid Department of Telematic Engineering Systems Programming, May 23rd, 2011

Bachelors in Telecommunication Technology Engineering, Communications Systems Engineering, Audiovisual Systems Engineering and Telematics Engineering

public class BNode { private Book info; private BNode left; private BNode right;

public void setLeft(BNode n) {left=n;} public void setRight(BNode n) {right=n;} public BNode getRight() {return right;} public BNode getLeft() {return left;} public Book getInfo() {return info;} public void setInfo(Book c){ info = c;}

BNode (Book con) { setInfo(con); setLeft(null); setRight(null); }

public int getNumBooks(){

// // TO IMPLEMENT IN SECTION 1

}

public boolean insertBook(Book con){

// // TO IMPLEMENT IN SECTION 2

} }

public class Book{

String title; String author; String isbn;

Contact (){ title=""; author=""; isbn=""; }

public String getTitle() {return title;} public String getAuthor() {return author;} public String getISBN() { return isbn; }

}

public class Library {

BNode root;

public boolean insertBook(Book con){

if (root==null) { root=new BNode(con); return true;} else {

return root.insertBook(con);

} }

public int getNumBooks (){ if (root==null) return 0;

else return root.getNumBooks();

} }

(4)

University Carlos III of Madrid Department of Telematic Engineering Systems Programming, May 23rd, 2011

Bachelors in Telecommunication Technology Engineering, Communications Systems Engineering, Audiovisual Systems Engineering and Telematics Engineering Section 1 (1.25 points)

Implement the method getNumBooks from BNode class. This method returns the

number of books in the subtree which root node is the one invoking the method. Notice this method is the one called from getNumBooks method, from Library class, in order to get the total number of books in the library.

This method must be solved recursively.

Section 2 (1.25 points)

Implement the method insertBook from BNode class. This method inserts the book (Book) specified as parameter at its corresponding place.

Notice this method is the one called from the method insertBook from Library class. The method must return true if the book is inserted in the tree, or false if the book is not inserted because there is another book with the same title in the tree.

(5)

University Carlos III of Madrid Department of Telematic Engineering Systems Programming, May 23rd, 2011

Bachelors in Telecommunication Technology Engineering, Communications Systems Engineering, Audiovisual Systems Engineering and Telematics Engineering

SOLUCIONES PROBLEMA 1

Criterios de calificación Problema 1 Apartado 1)

0,75 puntos - Recorrer correctamente la lista hasta la posición requerida.

Si la indexación no la hace correctamente desde 1, sino desde 0, se le da 0,5 puntos.

0,25 puntos - Check correcto de índice de posición fuera de rangos. 0,25 puntos - Return correcto del objeto.

Apartado 2)

0,10 puntos. Crear correctamente el nodo a insertar.

0,25 puntos. Insertar correctamente en la primera posición.

public Object retrieveNth (int pos){

Node aux=first;

if (pos<=0) return null;

if (pos>getSize()) return null;

for (int i=1;i<pos;i++){ aux=aux.getNext(); }

return aux.getInfo();

}

public int insertNth (Object o, int pos){

Node aux=first;

Node newNode=new Node(o);

if (pos<=0){ return -1;} if (pos==1) { newNode.setNext(first); first=newNode; numElems++; } else { if (pos>getSize()){ pos=getSize()+1; }

for (int i=1;i<pos-1;i++){ aux=aux.getNext(); } newNode.setNext(aux.getNext()); aux.setNext(newNode); numElems++; } return pos; }

(6)

University Carlos III of Madrid Department of Telematic Engineering Systems Programming, May 23rd, 2011

Bachelors in Telecommunication Technology Engineering, Communications Systems Engineering, Audiovisual Systems Engineering and Telematics Engineering

0,15 controlar que la posición requerida sea mayor que el tamaño de la lista. 0,25 devolver correctamente la posición final.

SOLUCIONES PROBLEMA 2

Criterios de calificación Apartado 1)

0,25 Sumar correctamente 1 por el nodo actual.

0,25 Comprobar correctamente que las ramas != null antes de invocar. 0,25 Invocar correctamente de forma recursiva.

0,25 sumar correctamente los return de las ramas. 0,25 devolver suma completa.

Apartado 2)

0,15 Comparación correcta del título.

0,25 Comprobación de si es el título del nodo actual. 0,25 Comprobar si la rama correspondiente es null o no.

public int getNumBooks (){ int n=1; if (this.getLeft()!=null){ n+=this.getLeft().getNumBooks(); } if (this.getRight()!=null){ n+=this.getRight().getNumBooks(); } return n; }

public boolean insertBook(Book newcon){ boolean res=false;

int comp = this.getInfo().getName().compareTo(newcon.getName()); if (comp == 0) { res=false; } else if (comp < 0){ if (this.getRight()==null) { this.setRight(new BNode(newcon)); res=true; }else{ res = this.getRight().insertBook(newcon); } } else if (comp > 0){ if (this.getLeft()==null) { this.setLeft(new BNode(newcon)); res=true; } else { res = this.getLeft().insertBook(newcon); } } return res; }

(7)

University Carlos III of Madrid Department of Telematic Engineering Systems Programming, May 23rd, 2011

Bachelors in Telecommunication Technology Engineering, Communications Systems Engineering, Audiovisual Systems Engineering and Telematics Engineering

0,25 Insertar si la rama es null.

0,25 Invocar correctamente la llamada recursiva si la rama no es null. 0,10 return correcto de true/false.

References

Related documents

Ovarian &amp; deep infiltrating endometriotic lesions Genetic &amp; immunological factors Genetic factors: &lt;10% pts 6-7 times increased incidence in 1 st degree

Timber for furniture and construction purposes, bark/leaves boiled in water and drunk to treat malaria/fever/swollen bodies/body pains/dysentery/stomach ache and for

The following figure illustrates the system flow for a SOAP Connector - Listen inbound operation using the Universal Event Monitor for SOA SOAP Connector.... The following

By first analysing the image data in terms of the local image structures, such as lines or edges, and then controlling the filtering based on local information from the analysis

The emotion caused by what for a moment seemed almost a diplomatic incident was diverted by the appearance of two Chinese servants in long silk robes and four-sided hats

All stationary perfect equilibria of the intertemporal game approach (as slight stochastic perturbations as in Nash (1953) tend to zero) the same division of surplus as the static

This is gaining importance, because 5.8 million Afghans have already returned to their country under the Solutions Strategy for Afghan Refugees (SSAR), turning the general

Solo Cnt.I Solo Cnt.II Rep.. Eb Bass Bb