Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Structures and Algorithms: A Comprehensive Guide with Exercises and Explanations, Exercises of Financial Economics

A comprehensive overview of data structures and algorithms, covering fundamental concepts, real-world applications, and practical examples. It includes detailed explanations of various data structures, such as arrays, linked lists, stacks, queues, and priority queues, along with their respective algorithms. The document also features numerous exercises and questions to reinforce understanding and promote critical thinking.

Typology: Exercises

2024/2025

Available from 02/26/2025

patrick-maina-2
patrick-maina-2 🇬🇧

300 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
What is a data structure? ✔✔An arrangement of data inside a computer’s memory or a
disk.
Name 5 data types ✔✔BLASH: binary trees, linked lists, arrays, stacks, hash tables
What do algorithms do? ✔✔Manipulate the data in data structures in various ways such
as sorting.
What is real-world storage data? ✔✔Data structure storage that describes physical
entities external to the computer. Accessed by a programs user.
What are programmer's tools? ✔✔Data structure storage that is accessed by the program
itself. Ex - stacks and queues.
What is real-world modeling? ✔✔Data structures that directly model real-world
situations...think graphs.
What is an object? ✔✔A software bundle of variables and related methods.
What is a class? ✔✔A blueprint that defines an objects variables and methods.
The keyword "new" is used to do what? ✔✔Create a new object in Java.
An object is often referred to as: ✔✔An instance of a class.
object.method(); is and example of ✔✔Invoking a method for a specific object.
Execution of the program starts where? ✔✔main()
To create an object in Java you must do two things: ✔✔
1. Use the keyword "new"
2. Store a reference to the object in a variable that is the same type as the class.
So:
Object = new ClassReference();
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Data Structures and Algorithms: A Comprehensive Guide with Exercises and Explanations and more Exercises Financial Economics in PDF only on Docsity!

Data Structures and Algorithms

What is a data structure? ✔✔An arrangement of data inside a computer’s memory or a disk. Name 5 data types ✔✔BLASH: binary trees, linked lists, arrays, stacks, hash tables What do algorithms do? ✔✔Manipulate the data in data structures in various ways such as sorting. What is real-world storage data? ✔✔Data structure storage that describes physical entities external to the computer. Accessed by a programs user. What are programmer's tools? ✔✔Data structure storage that is accessed by the program itself. Ex - stacks and queues. What is real-world modeling? ✔✔Data structures that directly model real-world situations...think graphs. What is an object? ✔✔A software bundle of variables and related methods. What is a class? ✔✔A blueprint that defines an objects variables and methods. The keyword "new" is used to do what? ✔✔Create a new object in Java. An object is often referred to as: ✔✔An instance of a class. object.method(); is and example of ✔✔Invoking a method for a specific object. Execution of the program starts where? ✔✔main() To create an object in Java you must do two things: ✔✔

  1. Use the keyword "new"
  2. Store a reference to the object in a variable that is the same type as the class. So: Object = new ClassReference();

How do other parts of your program interact with objects? ✔✔By interacting with an objects methods What is a constructor? ✔✔A special method that is called automatically whenever a new object is created. It prepares the object for use. How is the constructor named? ✔✔It has the same name as the class it resides in. A field of method that is private can only be accessed by ✔✔Methods that are apart of the same class. Any outside entity that wants to access data in a class can do so by: ✔✔Using a method of that class. What must be added to all input methods for them to work? ✔✔throws IOException What does the charAt() method do? ✔✔Returns a character at the specific position in the string object. What does the parse.Int() method do? ✔✔Converts the string type into an integer. What is an array? ✔✔A container object that holds multiple values of the same type. What are the two types of data in java? ✔✔Primitive types (int, char, boolean,long, double etc) and objects. Why must you use the key word "new" when creating an array? ✔✔Arrays in java are treated as objects. What does the [] operator tell the compiler? ✔✔That we are naming an array object. What are the steps to creating an array? ✔✔MRCNL: Main(), Reference, Create an Array, Number of Items, Loop. long [] arr is; an example of: ✔✔Referencing an array. arr = new long [100];is an example of: ✔✔Creating an array.

What is public static void main (String args[])? ✔✔This is a main () method with three modifiers What is meant by public in public static void main(String args[])? ✔✔Public indicates that the main() method can be called by any object. What is meant by static in public static void main(String args[])? ✔✔Static indicates that the main() method is a class method. What is meant by void in public static void main(String args[])? ✔✔Void indicates that the main() method has no return value. "____ is like a queen bee, kept hidden in the middle of the hive, fed and cared for by worker-bee methods" ✔✔Data What is the sorting process in bubble sort? ✔✔

  1. Compare first two elements
  2. If the one on the left is bigger, swap
  3. Move attention one position to the right. Repeat until largest element is on the right. What are invariants? ✔✔Conditions that remain unchanged as the algorithm proceeds. What is the sorting process in selection sort? ✔✔
  4. Making a pass through all the elements and selecting the smallest one
  5. Swap the smallest element with the the element at index 0
  6. Repete until all the elements are sorted What is insertion sort? ✔✔... Why are stacks, queues and priority queues more abstract than arrays? ✔✔... What are stacks allowed access to? ✔✔...

Use the postal analogy to describe stacks. ✔✔... Explain stacks using the work day analogy. ✔✔... What is the "push"? ✔✔Placing a data item on top of the stack. What is "popping"? ✔✔Removing a data item from the top of the stack. Explain LIFO ✔✔Last-In-First-Out. A stack is a LIFO storage mechanism because the last item inserted is the first one to be removed. What is "peek"? ✔✔It allows the user to read the value at the top of the stack with out removing it. Explain how you would create an algorithm that matches delimiters on the stack. Include errors in your explanation. ✔✔

  1. Read the characters from a string one at a time.
  2. Find and place the opening delimiters on the stack one at a time
  3. When the program reads a closing delimiter from the input, pop the corresponding opening delimiter from the top of the stack
  4. If the delimiters are not of the same type an error will occur, telling us that the string is missing either an opening or closing delimiter. If there is no opening delimiter on the stack an error will occur. What happens to non delimiter characters? ✔✔They are not inserted onto the stack; they are ignored. Whatever you can do to manipulate the data storage structure becomes our: ✔✔Methods. What is the difference between a stack and a queue? ✔✔In a queue the first item inserted is the first to be removed (FIFO). In a stack the last item inserted is the first to be removed (LIFO) Where does the term queue come from? ✔✔It is a British reference to waiting in line. Fist in, first out.

What is a reference? ✔✔A number that refers to an object. It's the objects address in the computers memory. How are primitive types like int and double stored in the computers memory? ✔✔The actual value is stored in memory. How is finding an element in a list different than finding an element in an array? ✔✔An item in an array can be accessed by directly accessing it's index. In a list an item can't be accessed directly; you must use the relationship between the items to access it. What is a double-ended linked list? ✔✔A linked list that has reference to the last link as well as the first. What is an ADT? ✔✔An Abstract Data type is a conceptual tool. It suggests that we describe the fields and methods of a class while excluding how the methods carry out their tasks. Ex I know that I need to use push() and pop() but don't know how the code for these methods are written. What is an ADT list? ✔✔Sometimes called a linear list, it is a group of items arranged in a linear order. What is a sorted list? ✔✔A list in which the items are in sorted order by key value. What is the advantage of using a doubly linked list? ✔✔It allows you to transverse forward and backward in a list. Each list links to the next link (just like in reg. lists) and to the previous link. What is the down side to using a doubly linked list? ✔✔To delete or insert a link you must deal with four links instead of two. What is an iterator? ✔✔An Object containing references to items in a data structure, used to traverse through the structure. What is recursion? ✔✔A programming technique in which a method calls itself. What is the divide and conquer approach? ✔✔Divide one big problem into two smaller ones and solve each separately. Then you divide each solution into an even smaller

problem and solve them. Continue this process until you get to the base case which is easily solved. What is the difference between a triangular number and a factorial? ✔✔A triangular number is the sum of a number plus each number smaller than it to zero. A factorial of a number is a number * each number smaller than it to zero. Mergesort requires a workspace equal in size to what? ✔✔the original array If a recursive approach in inefficient what should we replace it with? ✔✔A simple loop or a stack-based approach. Describe Shellsort. ✔✔Shellshort is a multi pass algorithm. In each pass the algorithm insertion sorts widely spaced elements. (The spacing of these elements is represented by the letter h.) Next the algorithm shifts over one cell and sorts those elements. This process continues until all elements have been "h-sorted", meaning that all items spaced h cells apart are sorted amongst themselves. Now the array is "almost" sorted. Next we must "diminish the gap". In order to do so, we must calculate the interval/gap sequence. This is the sequence of numbers used to generate the intervals at which we sort. What size arrays is Shellsort good for? ✔✔Medium-sized arrays of a few thousand items depending on the implementation. What algorithm is Shellsort based on? ✔✔Insertion sort. What are the benefits to using Shellsort? ✔✔The code is short and simple. What is Knuth's interval sequence? ✔✔h = (h-1)/ What is partitioning? ✔✔To divide data into two groups, so that all the items with a key value higher than a specified amount are in one group and all items with a lower key value are in another. Give an example of when partitioning would be useful. ✔✔If you want to divide a list of records into two groups s/a employees who sell above or below yearly sales goals.

What is a disadvantage of a linked list? ✔✔Finding an element quickly is difficult. You must start at the beginning of the list and visit each element until you find the one you're looking for. A tree consists of ______ connected by________. ✔✔nodes(represented by circles),edges(represented by connecting lines). What do nodes represent? ✔✔typical items we store in any kind of data structure. ex. People, cars, airline res, anything represented by objects. What do the edges represent? ✔✔The way the nodes are related. Why are edges beneficial? ✔✔Its easy and fast for a program to get from one node to another if there is a lind connecting them. What is the restriction of edges? ✔✔Generally, you are restricted to going in one direction: from the root downward. How many "children" are a binary tree allowed? ✔✔2 max. What is a path? ✔✔The way in which a sequence of nodes is connected. What is the Root? ✔✔The node at the top of the tree. There is only one root for every tree and only one path from root to any other node. (See pg. 368 for an example of a non tree) What is a leaf? ✔✔A node that has no children. What is a subtree? ✔✔A node with two children. The children have no children. Explain what happens when a node is visited. ✔✔This happens when a program control arrives at the node, usually for the purpose of carrying out some operation on the node, such as checking the value of a data field or displaying it. Passin over one node to get to another is NOT a visit. What does it mean to traverse a tree? ✔✔To visit all the nodes in a specified order.

What is a level? ✔✔This represents how many generations the node is from the root. The root is level 0, it's children level 1 and so on. Where are keys represented in trees? ✔✔When a circle represents a node holding a data item, the key value of the item is typically shown in the circle. What is a binary tree? ✔✔A tree in which each node can have only two children: a left child and a right child. What is a binary search tree? ✔✔A binary tree in which the nodes left child has a key value less than it's parent and it's right child must have a key that's great than or equal to its parent. What causes a tree to become unbalanced? ✔✔When the data items are inserted in an ascending or descending sequence. A tree is balanced when the data items are inserted randomly. What is the most common way to represent a tree in the computers memory? ✔✔To store the nodes at unrelated locations in memory, and connect them using references in each node that point to its children. First we need a class of node objects. What is stored in this class? ✔✔Objects containing the data representing the objects being stored(employee numbers for example) and also references to each of the node's two children. class Node { int iData; // data used as key value double fData; // other data node leftChild; node rightChild; public void displayNode() { // (see Listing 8.1 for method body) } } Why do we need a Tree Class? ✔✔We need a class from which to instantiate the tree itself: the object that holds all the nodes. It has only one field: a node variable that holds

What is a Huffman tree? ✔✔A binary tree used in a data-compression algorithm called Huffman Coding. What is a red-black tree? What problem does it solve? ✔✔A red-black tree is a binary search tree that What is top-down insertion? ✔✔Structural changes may be made to the tree as the search routine descendes the tree looking for the place to insert the node. What works the same in red-black tress as it does in ordinary binary trees? What works differently? ✔✔Searching works the same. Insertion and Deletion are modified. What is bottom-up insertion? ✔✔Finding a place to insert the node and then working back up through the tree making structural changes. When there are no branches, the tree becomes ✔✔a linked list; the arrangement of data in one-dimensional instead of being two-dimensional. This requires that we, on average, search through half of the items to find the one we are looking for. Data that is partially sorted will result in ✔✔a partially unbalanced tree. How can we ensure that our red-black tree in balanced? ✔✔During insertion. As an item is inserted, the insertion routine checks that certain characteristics of the tree are not violated. If they are it reconstructs the tree as necessary. What are characteristics of the red-black tree? ✔✔The nodes are colored and during insertion and deletion rules are followed that preserve various arrangements of these colors. What are the red-black rules? ✔✔

  1. Every node is either red or black
  2. The root is always black
  3. If a node is red its children must be black
  4. Every path from the root to the leaf, or root to null child, must contain the same number of black nodes.

What is a null child? ✔✔its a place where a child could be attached to a non-leaf node; a node that already has one child. What is the black height? ✔✔The number of black nodes on a path from root to leaf. How can you fix a problem caused by color rule violations? ✔✔You can either change the color of the nodes or perform rotations. What is the best way to flip colors? ✔✔The parent switches color with its children at the same time. What is a null child? ✔✔A child that non-leaf node might have, but doesn't. Why do we use rotations? ✔✔To balance the tree by physically rearranging the nodes. Rotations must do what two things at once? ✔✔ 1.Raise some nodes and lower others to help balance the tree 2.Ensure that the characteristics of a binary search tree are not violated. What is rotating? Why is this term misleading? ✔✔The term is misleading because the nodes themselves are not rotated; it's the relationship between them that changes. One node is chosen as the "top" of the rotation. In a right rotation, the "top" node will move down to the right, into the right child's position. The left child moves up to take it's place. Like ordinary binary search trees, red-black trees allow for searching, inserting and deletion in what time? ✔✔O(long2N) A leaf node has no children but can contain up to how many data items? ✔✔ 3 Unless its a link a 2- 3 - 4 tree node with one data item must have at least how many links? ✔✔ 2 We number the data items in a link from __ to __ and the child links from ___ to ___. ✔✔0 to 2 0 to 3 Explain how to split a 2- 3 - 4 tree with data items ABC. ✔✔

What is external storage? ✔✔this typically refers to some kind of disk system, s/a a hard disk, cd, dvd, flash drive. What is a B-tree? When is it useful? ✔✔A multiway tree that is useful when data resides in external storage. What is the main advantage and disadvantage of using external storage? ✔✔Disadvantage: much slower than main memory(RAM, Random Access Memory). Advantage: When the amount of data to be processed is too large to fit into main memory we can use external storage. Also Disk files retain data indefinitely so if the power fails your work will not be lost. What is a hash table? ✔✔A data structure that offers very fast insertion and searching. How quick is a hash table? ✔✔Insertion and searching taks close to constant time, 0(1) making it essentially instantaneous. What are the advantages to using a hash table? ✔✔They are fast and relatively easy to program. If you don't need to visit items in a specific order, and you can predict the size of your data base they are unparalleled in speed and convenience. What are the disadvantages to using a hash table? ✔✔They are based on arrays which are difficult to expand after they've been created. For some kinds of hash tables performance may degrade when a table becomes too full, so the programmer should have an fairly accurate idea of how many data items will need to be stored( or be prepared to periodically transfer data to a larger hash table which is time consuming). There is no convenient way to visit items in a hash table in any kind of order(s/a smallest to largest) What is a symbol table? ✔✔A symbol table is a data structure that associates a value with a key. It It supports two primary operations: insert (put) a new pair into the table and search for (get) the value associated with a given key. The symbol table holds all the variable and function names made up by the programmer, along with the address where they can be found in memory. What is the ASCII code? ✔✔Its a way to represent individual characters as numbers. a is 97, b is 98, c is 99 and so on. It runs from 0 to 255 to accomodate capitals and punctuation.

In an ordinary multi-digit number, each digit-position represents a value: ✔✔10 times as big as the position to it;s right. So 1234 really means 11000 + 2100 + 310 + 41. ( OR writing the multipliers as powers of 10 110^3 + 210^2 + 310^1 + 410^ How would you convert the word "money" into a multi-digit number? ✔✔1. Convert each digit into a number. I can use my own code instead of ASCII. So I would set a=1, b=2 and so on. I would set blank = 0 to accomodate for for a space giving my 27 characters.

  1. Next I would multiply wach number by the appropriate power of 27 and add the results. This process generates a unique number for every potential word. An array can handle a number/word this small. What is the % operator in java? ✔✔The modulo operator finds the remainder when one number is divided by another. What is a hash function? ✔✔It hashes (converts) a number in a large range into a number in a smaller range. The smaller range corresponds to the index numbers in an array. What is a hash table? ✔✔An array into which data is inserted using a hash function. How does a hash function work? ✔✔
  2. We convert a word into a huge number by multiplying each character in the word by an appropriate power of 27 and set it equal to hudgeNumber
  3. Using the modulo operator (%), we squeeze the resulting huge range of numbers into a range about twice as big as the number of items we want to store. What's the problem with squeezing a large range into a small one? ✔✔There's no longer a guarantee that two words won't hash to the same array index. What is a collision? ✔✔You hash a word to obtain its index number but find that the cell at that number is already occupied by another word because it hashes to the exact same number.