1.1 Data Structures

Ace your homework & exams now with Quizwiz!

What is a 2D array and what's it used for?

- A grid-like structure which can hold both rows and columns (array[row][column]) - It is an array of arrays and can be used for making tables of information

What are data structures?

- Allow for an efficient way of dealing with multiple data items. - However, they're more complex to program.

How do you go about deleting a data item?

- Follow the pointers until the item is found. - Change the pointers to remove links to the item. - Change the nextFree pointer to the index of the item that's being deleted.

What is a hash table?

- Hash table stores data in an associative array (with a key and a value) and uses a hashing technique to generate an index for where the data will be stored. - The index is a numeric value calculated from the data's key value. -The hash table provides direct access to an item via its index and therefore performance IS NOT affected by the number of items.

What is separate chaining?

- Original table has a dynamic data structure for each location. - Instead of just storing a single data item in each element of the hash table, we instead store a linked list inside each element. - After a collision occurs, a new node on the linked list in that location is created and the new data item is simply added in the same place. - While this is good at avoiding collisions, this technique will slow down finding the data again. - Once the hash location has been computed, the linked list will need to be searched as well. - This also doesn't require prior knowledge of how many elements as we're using a linked list.

What is overflow, when does it occur? How do stack overflow errors occur in programming languages?

- Overflow is where you attempt to add (push) something to the top of a full stack. - These usually occur in recursion algorithms with flawed exiting conditions. Everytime you call a subroutine, the return address to that subroutine is stored within a stack. Since you're recursively calling subroutines - without finishing any of them - this stack will keep filling up until it reaches its maximum size and overflow occurs.

What are the two functions related to stacks?

- Push: add something to the top of the stack - Pop: remove/retrieve something from the top of the stack.

How do you go about inserting a new data item?

- Store new node in address pointed by nextFree pointer. - Determine where the new item should be placed in the list (i.e. for ordered lists). - Change pointers from other nodes to attach the new node. Change nextFree to the next available address.

In a linked list, each element contains 2 things, what are the two things?

- The data itself - A pointer to the next element.

A hash table is made up of 2 components, which are...

- The hash function which uses the data entered to generate the location in the table where the data should be stored. - The table where the actaul data is stored.

What are some of the uses of Stacks (3 different uses)

- Undo function - Back button - Subprogram return addresses (winding back through methods)

What is an array and what's it used for?

- Used to store many data items of the same data type. - Elements in the array can only be of one data type and are accessed directly using an index.

What is a record and what's it used for?

- Used when more than one data type is being stored (so can't just be stored in an array) - It can be processed as one item by the computer - It's not one single data item, its several items (e.g in java it would be for example, String firstName = "Thomas"; int age = 17; etc.) - A set of data items related to a single entity.

Explain how you'd store a binary tree in a 2D array.

- You'd have 3 columns and n rows. - Each row would contain the data itself, a pointer to the index of the data item to the left in the tree and one to the right. - You then use -1 to show that there's nothing to the left/right.

It's important to pick a hash function which...

...spreads data evenly over the table to minimise collisions and the potential slow down caused as a result.

How would you search a binary tree?

1. Compare search term with the root node. 2. If not found then: a. If search term<root node, branch left. b. If search term>root node, branch right. (REPEAT until item is found or a node with no children is reached).

What is a balanced tree? What is an unbalanced tree?

A balanced tree is one where the max number of comparisons to locate an item is similar to the number of levels. An unbalanced tree is one where the maximum nubmer of comparisons is simliar to the numberof items.

Advantage and disadvantage of binary trees compared to linked lists.

Advantage: faster to access (can search in a non-linear manner) Disadvantage: There's an overhead of 2 pointers in a binary tree (one left and one right) whereas a linked list only has one pointer (to the next item).

Advantages/Disadvantages of binary trees compared to arrays. (1 of each)

Advantage: faster to search/add (in order) a value than arrays. Disadvantage: more complex to program.

Advantages/Disadvatnages of linked lists compared to arrays?

Advantages: - New items can be inserted without rearranging all the other elements - you only have to change pointers. - Uses memory more efficiently if programming dynamically. Disadvantages: - More complex to program. - can only be accessed in a linear manner

How would you delete a node from a binary tree? (root node, node with 1 child and node with 2 chidlren)

Deleting root: Replace the root node with the highest value from the left-hand side of the tree. (Doesn't have to be directly left, just anything on the LHS) Deleting node with 1 child: Replace the node with the child. Deleting node with 2 children: Replace the node with the child at the left pointer.

Pseudocode for removing something from a stack.

IF stackPointer > 0: dataItem = stackArray(stackPointer) stackPointer = stackPointer -1 ELSE PRINT("Stack is empty no data can be retrieved")

Pseudocode for adding something to a stack.

IF stackPointer<stackMaximum: stackPointer = stackPointer + 1 stackArray(stackPointer) = dataItem ELSE PRINT("Stack is full - your data won't be saved")

Compare the searches of a linked list for ordered lists, compared to unordered lists.

In an ordered list, the search can be terminated when a value greater than the search term is found. However, with an unordered list, you have to search the entire list.

What are some uses of Queues (2)?

Print queue - documents printed in order that they are received. Keyboard Buffer - characters appear on screen in the order they're received.

Give and explain two alternatives to avoiding hash table collisions.

Progressive Overflow: - If the location is occupied, use the next available location. - When the end of the file is reached, wrap around to the start. - Simple and easy to maintain. - Not very efficient, searching for a key not found could result in a search through the entire file. Overflow: - Separate pre-allocated file (wasted space?) - Flag the original block - Move data into the overflow area - Searching overflow area uses linear search - Re-organisation/bigger file will eventually be needed.

Inorder: How would you go about traversing the tree in in-order? Uses of in-order?

Traversing: - Draw dots below each of the nodes. - Starting from the top of the root node, and moving anti-clockwise, connect up the dots, this is the order of your traversal. Uses: - Sorting/Searching a binary tree - main ones. - Traversing alphabetically? The order will be in order, numerical or alphabetical.

Pre-order: How would you go about traversing the tree in pre-order? What are some of the uses of pre-order?

Traversing: - Draw dots to the left-hand side of all the nodes. - Starting from the top of the root node, and moving anti-clockwise, connect up the dots, this is the order of your traversal. Uses: - Cloning a tree. - Counting the number of leaves. - Converting expression tree to prefix notation?

Post order: How would you about traversing the tree in post-order? Uses of post-order?

Traversing: - Draw dots to the right of each nodes - Starting from the top of the root node, and moving anti-clockwise, connect up the dots, this is the order of your traversal. Uses: - Deleting/undo a binary tree - Stack-based programming -Converting postfix notation to expression tree

In what scenario is a queue used?

When the desirable procesisng order is first in, first out (FIFO).

When would you use a Stack?

When the desirable processing order is last in, first out. (LIFO)

What is underflow? When does it occur?

When you attempt to pop something from an empty steck.

What are the functions related to queues?

You can only add to the back and remove from the front of the queue.

How would you store a linked list as a 2D array?

You have n rows and 2 columns. Each row contains the data, and then the pointer, you use something like -1 for if there's no pointer. You need a start value (0) and then a nextFree pointer which is updated, to hold the index of the next address that's free.


Related study sets

Chapter 11: Human Resource Management: Finding and Keeping the Best Employees

View Set

EMT Module Exam 10 (Chapters 35 - 40)

View Set

Money & Banking 5.5, Chapter 5, 14,15,16 (Mid-Term), Money and Banking CH 5, Econ Chapter 5 part 2, econ206 chapter 5, Econ 2035 Ch5.1, 2035 Chapter 5, Money and Banking Ch 5, Chapter 3 FINC 3700, Chapter 4, Econ 2035 Ch4.3, Money & Banking HW #2, Ec...

View Set

A/V technology Careers and Education

View Set

ENT4113 FIU FINAL | Entrepreneurship SUCCESSFULLY LAUNCHING NEW VENTURES

View Set