Data Structures Midterm

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Binary search time complexity

O(log n)

numberlist: toString method

public String toString() { String listContent = "The content of the list is: "; LNode current = m_first; while (current != null) { listContent += current.getInfo() + " "; current = current.getLink(); } return listContent; }

Stack operations and time complexities for both array and linked(they're the same)

push: O(1) pop: O(1) top: O(1) search: O(n) toString: O(n)

Queue

First in first out. elements are added to the rear

2 14+23× equates to 9 7×16 5 1−÷3× + equates to

(2+14) ×23 9×7+16÷ (5−1)×3

Define a doubly linked list

A linked list in which each node is linked to both its successor and its predecessor.

define circular linked list

A list in which every node has a successor; the "last" element is succeeded by the "first" element. Adding and removing elements at the front of a list might be a common operation for some applications. Then what is the problem with the above list structure? You have to traverse the list from its beginning to the end and then insert the new node. This would take a long time when the list contains a large number of elements. We can fix this problem by letting our list reference point to the last element in the list rather than the first; now we have easy access to both the first and the last elements in the list.

Recursive call

A method call in which the method being called is the same as the one making the call.

Java interface

similar to a Java class: it can include variable declarations and methods. However, variables must be constants; methods must be abstract. a java interface cannot be instantiated java interfaces can be used to represent adts

Linked list numberlist: isFull()

public boolean isFull() { return false; }

Java abstract method

Only includes a description of its parameters. No method bodies or implementations are allowed. In other words only the interface of the method is included

Stack

last in first out

unbounded

linked list without capacity

List ADT

A collection of elements with a linear relationship existing between its elements. like alphabetical order

Abstract data type

A data type whose properties (domain and operations are specified independently of any particular implementation

Method call stack

A typical application involves many levels of method calls, which is managed by a so-called method call stack.

Linked and array based constructor time complexity

Array-based: O(N) Link-based: O(1)

Stack Constructors for linked and array time complexity

Array-based: O(N) Link-based: O(1)

Compare the storage size of array based and linked based Stacks

Array-based: takes the same amount of memory, no matter how many array slots are actually used, proportional to current capacity. Link-based: takes space proportional to actual size of the queue (but each element requires more space than with array approach).

Comparing queue implementations

Array-based: takes the same amount of memory, no matter how many array slots are actually used, proportional to maximum size. Link-based: takes space proportional to actual size of the queue (but each element requires more space than with array approach)

iterative vs recursive solutions

Both run at the same speed recursion takes more memory recursive solutions are easier to understand and implement correctly than their iterative counterparts. There is a certain elegance and economy of thought to recursive solutions that makes them more appealing.

Implementing ADTs by copy or by reference definitions

By copy: The ADT manipulates copies of the data used in the client program. Making a valid copy of an object can be a complicated process. Drawbacks: Copy of object might not reflect up-to-date status of original object Copying objects takes time, especially with deep-copying methods. Storing extra copies of objects also requires extra memory. By reference: The ADT manipulates references to the actual elements passed to it by the client program. This is the most commonly used approach and is the approach we use in our course.The contents of the collection ADT are exposed to the client program so it has direct access to the individual elements in the collection. Drawbacks: Objects are accessed through aliases so the client program could accidently change an attribute of the objects. This might in turn cause problems. For example if it changes the key value for an element stored in a sorted list, then the order of the list is broken.

steps for the binary search algorithm

Examine the midpoint of the array and compare the element found there to our target element (first + last) / 2; Eliminate half the array from further consideration ; Recursively repeat this approach on the remaining half of the array until we find the target or determine that it is not in the array.

which is better by copy or by reference

If processing time and space are issues, and if we are comfortable counting on the application programs to behave properly, then the "by reference" approach is probably best. If we are not too concerned about time and space (maybe our list objects are not too large), but we are concerned with maintaining careful control over the access to and integrity of our lists, then the "by copy" approach is probably best.

Linked list operations and time complexity:

Inserting: O(1), if done at the head, O(n) if anywhere else since we have to reach that position by traveseing the linkedlist linearly. Deleting: O(1), if done at the head, O(n) if anywhere else since we have to reach that position by traveseing the linkedlist linearly. Searching: O(n)

Three adt perspectives

Logical or abstract level: what is this ADT? what does it model? What are its responsibilities? what is its interface? Implementation or concrete level: How do we represent and manipulate the data in memory? How do we fulfill the responsibilities of the ADT Application or user, client: what program statements to use to create instances of the ADT and invoke its operations

Indirect recursion (a.k.a mutual recursion)

Recursion in which a chain of two or more method calls returns to the method that originated the chain, for example method A calls method B which in turn calls method A.

Direct recursion

Recursion in which a method directly calls itself, like the factorial method.

three factors for a successful recursive method

Recursive algorithm A solution that is expressed in terms of smaller instances of itself and a base case. Base case The case for which the solution can be stated non-recursively. General (recursive) case The case for which the solution is expressed in terms of a smaller version of itself.

To verify that a recursive solution works, we must be able to answer "Yes" to all three of these questions:

The Base-Case Question: Is there a nonrecursive way out of the algorithm, and does the algorithm work correctly for this base case? The Smaller-Caller Question: Does each recursive call to the algorithm involve a smaller case of the original problem, leading inescapably to the base case? The General-Case Question: Assuming the recursive call(s) to the smaller case(s) works correctly, does the algorithm work correctly for the general case?

What is the difference between the unsorted and sorted array implementation of the list add method

The add method for sorted arrays must find the spot it goes in then shift all elements after it starting from the end going in till there is an empty space for the value the unsorted array it can just be added to the front or back

which Stack implementation is better for what reasons?

The array-based implementation is short, simple, and efficient. Its operations have less overhead. When the maximum size is small and we know the maximum size with certainty, the array- based implementation is a good choice. The linked implementation does not have space limitations, and in applications where the number of stack elements can vary greatly, it wastes less space when the stack is small.

what is object serialization

The process of converting the object to a stream of bytes that can be saved to a file for later retrieval. Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

Collection methods time complexity unsorted array, sorted array and unsorted Linked list

Unsorted array: bounded constructor o(n) size O(1) contains O(N) get O(N) add O(1) remove O(N) Sorted array: Unbounded constructor O(n) size O(1) contains O(logN) get O(logN) add O(N) remove O(N) Unsorted Linked List: unbounded constructor o(1) size O(1) contains O(N) get O(N) add O(1) remove O(N)

java exception mechanism three major phases

defining the exception, generating the exception, handling the exception

Collections

does not restrict data based on the order in which the data was stored.

collection method names and explanations

boolean add(T element); // Attempts to add element to this collection. Returns // true if successful, false otherwise. T get(T target); // Returns an element e from this collection such that // e.equals(target). If no such element exists, returns // null. boolean contains(T target); // Returns true if this collection contains an element e // such that e.equals(target); otherwise returns false. boolean remove (T target); // Removes an element e from this collection such that // e.equals(target) and returns true. If no such element // exists, returns false. boolean isFull(); boolean isEmpty(); int size();

What does an unbounded array do when full

create a new, larger array, when needed, and copy the queue into the new array. This method instantiates an array with a size equal to the current capacity plus the original capacity.

Queue methods time complexity

enqueue O(1) dequeue O(1) isEmpty O(1) size O(1) (isFull) O(1) toString O(n) enlarge O(n)

equals method vs compare to method

equals is exported from the Object class it compares two objects to find out if they are from the same Object. you can override the equals method to say that if the variables of the object are the same then they are equal. for example a reasonable definition for equality of Circle objects is that they are equal if they have equal radii CompareTo returns an integer value that indicates the relative size relationship between the object upon which the method is invoked and the object passed to the method as an argument for example return -1 if the object is less than the parameter 0 if they are equal 1 if the object is greater than the parameter

fixed front and floating front differences and time complexity

fixed front: moves every element up when dequeued O(n) floating front: does not shift every element more efficient if the end of the queue is full it circles around and uses the front. O(1)

enlarge method for an unbounded array queue

private void enlarge() // Increments the capacity of the queue by an amount // equal to the original capacity. { // create the larger array T[] larger = (T[]) new Object[elements.length + origCap]; // copy the contents from the smaller array into the larger array int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++) { larger[currLarger] = elements[currSmaller]; currSmaller = (currSmaller + 1) % elements.length; } // update instance variables elements = larger; front = 0; rear = numElements - 1; }

linked based collection methods

protected void find(T target) { location = head; found = false; while (location != null) { if (location.getInfo().equals(target)) { found = true; return; } else { previous = location; location = location.getLink(); } } } public boolean remove (T target) { find(target); if (found) { if (head == location) head = head.getLink(); else previous.setLink(location.getLink()); numElements--; } return found; }

Benefits of using interfaces

provides a contract for all the implementation classes We can formally verify that the interface contract is met by the implementation we can provide a consistent interface to applications from among alternate implementations of the ADT

Collection array based methods

public boolean add(T element) // Attempts to add element to this collection. // Returns true if successful, false otherwise. { if (isFull()) return false; else { elements[numElements] = element; numElements++; return true; } } protected void find(T target) { location = 0; found = false; while (location < numElements) { if (elements[location].equals(target)) { found = true; return; } else location++; } } public boolean remove (T target) // Removes an element e from this collection // such that e.equals(target) and returns true; // if no such element exists, returns false. { find(target); if (found) { elements[location] = elements[numElements - 1]; elements[numElements - 1] = null; numElements--; } return found; } public boolean contains (T target) { find(target); return found; } public T get(T target) { find(target); if (found) return elements[location]; else return null; }

numberlist: contains method linked list

public boolean contains(int v) { LNode current = m_first; while (current != null) { if (current.getInfo() == v) return true; else current = current.getLink(); } return false; }

write the remove method for a circular linked list

public boolean remove (T element) { find(element); if (found) { if (list == list.getLink()) list = null; else if (previous.getLink() == list) list = previous; previous.setLink(location.getLink()); numElements--; } return found; }

unsorted reference based list add and remove methods

public void add(T element) // Adds element to this list. { LLNode<T> newNode = new LLNode<T>(element); newNode.setLink(list); list = newNode; numElements++; } public boolean remove (T element) // Removes an element e from this list such that // e.equals(element) and returns true; if no such // element exists returns false. { find(element); // a method that finds the element and set // location to the reference of the element if (found) { if (list == location) list = list.getLink(); else previous.setLink(location.getLink()); numElements--; } return found; }

write the circular linked list add method

public void add(T element) { LLNode<T> newNode = new LLNode<T>(element); if (list == null) { list = newNode; newNode.setLink(list); } else { newNode.setLink(list.getLink()); list.setLink(newNode); list = newNode; } numElements++; }

Sorted add and remove methods for linked lists

public void add(int v) { SLNode newNode = new SLNode(v); SLNode current = m_first; SLNode previous = null; while (current != null) { if (current.getInfo() < v) { previous = current; current = current.getLink(); } else break; } if (previous == null) { newNode.setLink(current); m_first = newNode; } else { newNode.setLink(current); previous.setLink(newNode); } } public boolean remove(int v) { SLNode current = m_first; SLNode previous = null; boolean found = false; while (current != null) { if (current.getInfo() < v) { previous = current; current = current.getLink(); } else if (current.getInfo() == v) { found = true; current = current.getLink(); if (previous == null) m_first = current; else previous.setLink(current); } else return found; } return found; }

public void enqueue(T element) // Adds element to the rear of this queue. { if (numElements == elements.length) enlarge(); rear = (rear + 1) % elements.length; elements [rear] = element; numElements = numElements + 1; }

public void enqueue(T element) // Adds element to the rear of this queue. { if (numElements == elements.length) enlarge(); rear = (rear + 1) % elements.length; elements [rear] = element; numElements = numElements + 1; }

queue methods written out array based-bounded

public void enqueue(T element) // Throws QueueOverflowException if this queue is full, // otherwise adds element to the rear of this queue. { if (isFull()) throw new QueueOverflowException("Enqueue attempted on a full queue."); else { rear = (rear + 1) % elements.length; elements[rear] = element; numElements = numElements + 1; } } public T dequeue() // Throws QueueUnderflowException if this queue is empty, // otherwise removes front element from this queue and // returns it. { if (isEmpty()) throw new QueueUnderflowException("Dequeue attempted on empty queue."); else { T toReturn = elements[front]; elements[front] = null; front = (front + 1) % elements.length; numElements = numElements - 1; return toReturn; } } public boolean isEmpty() // Returns true if this queue is empty, otherwise returns // false { return (numElements == 0); } public boolean isFull() // Returns true if this queue is full, otherwise returns // false. { return (numElements == elements.length); } public int size() // Returns the number of elements in this queue. { return numElements; }

linked based queue methods

public void enqueue(T element) { LLNode<T> newNode = new LLNode<T>(element) if(rear == null) front = newNode; else rear.setLink(newNode); rear = newNode; numElements++; } public void dequeue() { if(isEmpty()) throw new QueueUnderflowException("Dequeue attempted on an empty queue"); else { T element; element = front.getInfo(); front = front.getLink(); if (front == null) rear = null; numElements--; return element; } }

numberlist: insert method

public void insert(int v) { LNode newNode = new LNode(v); newNode.setLink(first); first = newNode; }

write out push, pop and top for the linked based stack implementation

public void push(T element) // Places element at the top of this stack. { LLNode<T> newNode = new LLNode<T>(element); newNode.setLink(top); top = newNode; } public void pop() // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an empty stack."); else top = top.getLink(); } public T top() { if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else return top.getInfo(); }

write out push pop and top for the array based stack implementation

public void push(T element) { if (isFull()) throw new StackOverflowException("Push attempted on an empty stack."); else { topIndex++; elements[topIndex] = element; } } public void pop() { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an else { elements[topIndex] = null; topIndex--; } } public T top() // Throws StackUnderflowException if this stack is empty, // otherwise returns top element from this stack. { T topOfStack = null; if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else topOfStack = elements[topIndex]; return topOfStack; }

What is the difference between the unsorted and sorted array implementation of the list remove method

the remove method for unsorted is faster because the last element can be shifted into the deleted elements spot but the sorted list has to shift every element behind the removed element

push what if the stack is full?

throw a StackOverflowException define an isFull method for use by application

pop and top what if the stack is empty?

throw a StackUnderflowException and define isEmpty for application use

time complexities for lists

unsorted remove O(1) sorted removeO(n) add O(n) linked based remove O(n) add O(n)


Set pelajaran terkait

Final Kinesiology_STUDY GUIDE_4 vm

View Set

Module 6 - Display and Video Advertising

View Set