Docsity

Data Structures and Algorithm Exam and Answers, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures and
Algorithm
"____ is like a queen bee, kept hidden in the middle of the hive, fed and cared
for by worker-bee methods" - ANSData
{a = new long [size]; } is an example of what? - ANSCreating an array
A field in the list itself contains a reference to: - ANSThe first link.
A field of method that is private can only be accessed by - ANSMethods that
are apart of the same class.
A leaf node has no children but can contain up to how many data items? -
ANS3
A tree consists of ______ connected by________. - ANSnodes(represented by
circles),edges(represented by connecting lines).
An object is often referred to as: - ANSAn instance of a class.
Any outside entity that wants to access data in a class can do so by: -
ANSUsing a method of that class.
arr = new long [100];is an example of: - ANSCreating an array.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Data Structures and Algorithm Exam and Answers and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structures and

Algorithm

"____ is like a queen bee, kept hidden in the middle of the hive, fed and cared for by worker-bee methods" - ANSData {a = new long [size]; } is an example of what? - ANSCreating an array A field in the list itself contains a reference to: - ANSThe first link. A field of method that is private can only be accessed by - ANSMethods that are apart of the same class. A leaf node has no children but can contain up to how many data items? - ANS A tree consists of ______ connected by________. - ANSnodes(represented by circles),edges(represented by connecting lines). An object is often referred to as: - ANSAn instance of a class. Any outside entity that wants to access data in a class can do so by: - ANSUsing a method of that class. arr = new long [100];is an example of: - ANSCreating an array.

Breaking up the program into classes does what? - ANSMakes it easier to design, understand, modify and maintain the program. Briefly explain the class DataItem in Java code for a 2-3-4 Tree. - ANSDataItem contains :

  1. objects that represent the data items stored in nodes s/a a company's personnel or a bars inventory. The only actual data type here is of type long, associated with each DataItem object.
  2. Out constructor and
  3. System.out.print('/'+data); to display which item out of total data items. Briefly explain the class Node in Java code for a 2-3-4 Tree. - ANSNode contains two arrays. The first holds references to whatever children the node might have. The second holds references to objects of the type DataItem contained in the node. Briefly explain the class Tree234App in Java code for a 2-3-4 Tree. - ANSThe Tre234App class contains main(). main() inserts data items into the tree, It then presents a character-based interface for the user, who can enter 2 into the tree, i to insert a new data item, and f to find an existing item. Briefly explain the class Tress234 in Java code for a 2-3-4 Tree. - ANSAn object of the Tree234 class represents the entire tree. The class has only one field: root, of type Node. All operations start at the root, so that's all a tree needs to remember. It contains methods s/a find() and getNextChild() to search, insert() and split(). Data that is partially sorted will result in - ANSa partially unbalanced tree. Describe how to insert a node. - ANSWe must find the place to insert it. We follow the path from the root to the appropriate node, which will be the parent of the new node. When the parent is found, the new node is

Execution of the program starts where? - ANSmain() Explain how to split a 2-3-4 tree with data items ABC. - ANS1.A new, empty node is created as a sibling to the node being split and placed to the right

  1. Data item C is moved to the new node, B is moved into the parent of the node being split and A remains where it is.
  2. The rightmost two children of the node being split are disconnected and connected to the new node. Explain how to split the root of a 2-3-4 tree. - ANS1. A new root is created. It becomes the parent of the node being split.
  3. A second new node is created. It becomes a sibling of the node being split.
  4. Data item C is moved to the new sibling, B is moved to the new root and A remains where it is.
  5. The two rightmost children of the node being split are disconnected from it and connected to the new right-hand node. Explain how you would create an algorithm that matches delimiters on the stack. Include errors in your explanation. - ANS1. Read the characters from a string one at a time.
  6. Find and place the opening delimiters on the stack one at a time
  7. When the program reads a closing delimiter from the input, pop the corresponding opening delimiter from the top of the stack
  8. 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. Explain LIFO - ANSLast-In-First-Out. A stack is a LIFO storage mechanism because the last item inserted is the first one to be removed. Explain stacks using the work day analogy. - ANS...

Explain what happens when a node is visited. - ANSThis 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. First two steps of object-oriented approach? - ANS1. Separate the data structure storage from the rest of the program(now called the user).

  1. Improve the communication between the storage structure and the user. First we need a class of node objects. What is stored in this class? - ANSObjects 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) } } Give an example of when partitioning would be useful. - ANSIf you want to divide a list of records into two groups s/a employees who sell above or below yearly sales goals.

How do we delete a node that has one child? - ANSWe "snip" the node out of its sequence by connecting it's parent directly to its child. We must change the appropriate reference in the parent directly to it's child. How do we delete a node that has two children? - ANSDelete the node and replace it with its inorder successor. How does a hash function work? - ANS1. 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

  1. 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. How does the mail sorting analogy apply to the priority queue? - ANSYou insert a letter from the postman into your pile of pending letters according to priority. The higher the priority the higher the position in the pile. How does the partition algorithm work? - ANSIt starts with two pointers, one at each end of the array. The pointer on the left end moves to the right and right end moves to the left. When the left pointer encounters a data item smaller than the pivot value (the value used to determine into which of the two groups an item is placed), it keeps going because it is already on the right side of the array. When it encounters an item that is larger than the pivot value, it stops. When the right pointer encounters a data item larger than the pivot value it keeps going because it is already on the correct side of the array. When it comes to a data item smaller than the pivot value it stops. Now the two items which have stopped the pointers are swapped. After the swap, the pointers continue, stopping only to swap items that are on the wrong side of the array. The process continues until the pointer meet, making the partitioning process complete.

How is "peek" different in a queue? - ANSIt "peeks" at the value of the item at the front of the queue without removing it as opposed to the last value in a stack. How is a priority queue different than a queue? - ANSA priority queue items are ordered by key value so that the item with the lowest key ( or in some implementations the highest key) is always at the front. Items are inserted in the proper position to maintain the order. How is finding an element in a list different than finding an element in an array? - ANSAn 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. How is the constructor named? - ANSIt has the same name as the class it resides in. How many "children" are a binary tree allowed? - ANS2 max. How quick is a hash table? - ANSInsertion and searching taks close to constant time, 0(1) making it essentially instantaneous. How would you convert the word "money" into a multi-digit number? - ANS1. 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.

is an example of what? - ANSGet value public LowArray(int size) is an example of what? Note that LowArray is also the name of the class. - ANSCreating a Constructor. public void setElement(int index, long value) {a[index] = value; is an example of what? - ANSSet value Representing a tree with an array inefficient. Why? - ANSUnfilled nodes and deleted nodes leave holes in the array, wasting memory. Also when deletion of a node involves moving subtrees, every node in the subtree must be moved to its new location in the array, which is time-consuming in large trees. Rotations must do what two things at once? - ANS1.Raise some nodes and lower others to help balance the tree 2.Ensure that the characteristics of a binary search tree are not violated. The keyword "new" is used to do what? - ANSCreate a new object in Java. To create an object in Java you must do two things: - ANS1. Use the keyword "new"

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

Unless its a link a 2-3-4 tree node with one data item must have at least how many links? - ANS Unlike a stack, the items in a queue don't always: - ANSextend all the way down to index 0. Use the postal analogy to describe stacks. - ANS... We are missing a class: the class to: - ANSinstantiate the class itself. We number the data items in a link from __ to __ and the child links from ___ to ___. - ANS0 to 2 0 to 3 What algorithm is Shellsort based on? - ANSInsertion sort. What are characteristics of the red-black tree? - ANSThe nodes are colored and during insertion and deletion rules are followed that preserve various arrangements of these colors. What are invariants? - ANSConditions that remain unchanged as the algorithm proceeds. What are programmer's tools? - ANSData structure storage that is accessed by the program itself. Ex - stacks and queues. What are stacks allowed access to? - ANS...

What are the three steps to delete an item in an array? - ANS1.Search for the item

  1. Find the item
  2. Move the items with a higher index value down to fill the "hole" What are the two types of data in java? - ANSPrimitive types (int, char, boolean,long, double etc) and objects. What causes a tree to become unbalanced? - ANSWhen the data items are inserted in an ascending or descending sequence. A tree is balanced when the data items are inserted randomly. What do algorithms do? - ANSManipulate the data in data structures in various ways such as sorting. What do nodes represent? - ANStypical items we store in any kind of data structure. ex. People, cars, airline res, anything represented by objects. What do the edges represent? - ANSThe way the nodes are related. What does it mean to traverse a tree? - ANSTo visit all the nodes in a specified order. What does the [] operator tell the compiler? - ANSThat we are naming an array object. What does the charAt() method do? - ANSReturns a character at the specific position in the string object.

What does the parse.Int() method do? - ANSConverts the string type into an integer. What does the searchKey variable do? - ANSIT holds the value we are looking for. What evolved from the 2-3-4 tree? - ANSThe red-black tree. What happens to non delimiter characters? - ANSThey are not inserted onto the stack; they are ignored. What is "peek"? - ANSIt allows the user to read the value at the top of the stack with out removing it. What is "popping"? - ANSRemoving a data item from the top of the stack. What is a B-tree? When is it useful? - ANSA multiway tree that is useful when data resides in external storage. What is a binary search tree? - ANSA 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 is a binary tree? - ANSA tree in which each node can have only two children: a left child and a right child. What is a class? - ANSA blueprint that defines an objects variables and methods. What is a cluster? - ANSA cluster occurs during linear probing. Once a cluster

What is a Huffman tree? - ANSA binary tree used in a data-compression algorithm called Huffman Coding. What is a leaf? - ANSA node that has no children. What is a level? - ANSThis represents how many generations the node is from the root. The root is level 0, it's children level 1 and so on. What is a link? - ANSA data item embedded in a linked list. It is an object of a class. What is a null child? - ANSA child that non-leaf node might have, but doesn't. What is a null child? - ANSits a place where a child could be attached to a non-leaf node; a node that already has one child. What is a path? - ANSThe way in which a sequence of nodes is connected. What is a red-black tree? What problem does it solve? - ANSA red-black tree is a binary search tree that What is a reference? - ANSA number that refers to an object. It's the objects address in the computers memory. What is a sorted list? - ANSA list in which the items are in sorted order by key value. What is a subtree? - ANSA node with two children. The children have no children.

What is a symbol table? - ANSA 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 an ADT list? - ANSSometimes called a linear list, it is a group of items arranged in a linear order. What is an ADT? - ANSAn 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 array? - ANSA container object that holds multiple values of the same type. What is an iterator? - ANSAn Object containing references to items in a data structure, used to traverse through the structure. What is an object? - ANSA software bundle of variables and related methods. What is bottom-up insertion? - ANSFinding a place to insert the node and then working back up through the tree making structural changes. What is double hashing? - ANSIt eliminates both primary and secondary hashing.

What is public static void main(String args[])? - ANSThis is a main() method with three modifiers What is Quadratic probing? - ANSQuadratic probes eliminate the clustering of a linear probe. In a linear probe the primary hash index is x. Subsequent probes go to x+1, x+2, x+3 etc. In quadratic probing, probes go to x+1, x+4, x+9, X+16, x+25 and so on. (The distance from the initial probe is always the square of the step number) What is Radix sort? - ANSThis sort examines each digit of the key separately, starting with the least significant digit. All the data items are divided into 10 groups. These 10 groups are then reassembled starting with all the keys that end in 0, followed by all keys ending in1 and so on (up to keys ending in 9). These steps are a sub-sort. In the next sub-sort, all is divided into 10 groups again, but in order of their 10s digit. This is done without changing the order of the previous sort. The sub-sorts must be stable. The groups are recombined, those w a 10s digit of 0 first up to 9. This process is repeated. What is real-world modeling? - ANSData structures that directly model real- world situations...think graphs. What is real-world storage data? - ANSData structure storage that describes physical entities external to the computer. Accessed by a programs user. What is recursion? - ANSA programming technique in which a method calls itself. What is rotating? Why is this term misleading? - ANSThe 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.

What is secondary clustering? - ANSIt occurs when the algorithm that generates the sequence of steps in the quadratic probe always generates the same steps: 1,4,9,16,25, etc. What is separate chaining? - ANSAnother way to fix a collision. We create an array that consists of linked list of words instead of the words themselves. Then when a collision occurs,the new item is simply inserted in the list at that index. What is the "push"? - ANSPlacing a data item on top of the stack. What is the % operator in java? - ANSThe modulo operator finds the remainder when one number is divided by another. What is the advantage of using a doubly linked list? - ANSIt 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 ASCII code? - ANSIts 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. What is the best way to flip colors? - ANSThe parent switches color with its children at the same time. What is the black height? - ANSThe number of black nodes on a path from root to leaf.