Data Structures

Ace your homework & exams now with Quizwiz!

What's the main difference between a stack and a queue?

A stack uses the LIFO priority, while a queue uses the FIFO priority.

What is an a-cyclic graph?

A-cyclic Graphs do not form loops; performing a DFS of an A-cyclic Graph will result in a dead-end. Ex.: Linked Lists and Trees.

What is a linked list?

Data structures that allow sequential order of elements. Solves some memory issues of arrays, yet provides array-like functionality. A linked-list is made up of nodes, and each node contains data and a reference to another node in the list. The first node is called the head.

What kind of graph is a linked list?

Directed, A-cyclic, unweighted graph

How does a hash table store data?

A hash table stores data by taking a key input and running it through a hash function. This function usually maps strings to numbers, with the numbers corresponding to indexes in an array.

What is a node without children called?

A leaf node

Give real world examples/analogies of linked lists

A linked list is like a scavenger hunt.

What are the ways in which we can search a tree?

Depth-first search (DFS) and Breadth-first search (BFS).

What is a hash table?

It's a data structure that allows us to impliment a map. Hash tables basically made of two parts: a hash function and an array. Hash tables are similiar to JS objects in that it stores values by linking it to a key, creating key-value pairs. Each key should be unique. Most languages have hash tables built in, and in JS behind the scenes an object is built using a hash table.

Describe a binary search tree

It's a kind of binary tree, but with extra rules: a right child node's value has to be greater than or equal to its parent, and the left child node's value has to be less than its parent.

What is a tree data structure?

It's a particular kind of graph, and it simulates a hierarchal tree structure where each tree consists of nodes, sorta like a linked list. The "root node" is at the top (kinda like a "head" in a linked list").

What is a binary tree?

It's a type of tree where we also start with a root node, but every child node may have up to a max of two child nodes: a left and a right node.

How are linked lists stored in memory?

Nodes are spread out in memory at whatever spot was open at the time the node was created; the location of a node is not dependent on the location of other nodes.

What are the advantages of a binary search tree?

The benefits are almost like a cross between a linked list and an array. The nodes in a BST are sorted, allowing us to do things like: find the Nth smallest/largest element (hash tables can't even do that/aren't sorted). Traversing a BST is O(log n) which is great, and even if unbalanced degrades into O(n) (same as a linked list). BST sizes are dynamic, offering similar benefits that linked lists have over arrays.

Give an example algorithm that uses a heuristic

The nearest neighbor solution to the "traveling salesmen" problem.

What is the basic characteristic/rule of a binary search tree (as compared with a regular tree)?

The value of a parent node is greater than the value of its left child, and is less than the value of its right child (assuming the parent node has either).

Explain how a typical hash table works step by step

We input a key (usually a string) and it's value. The key/string is pushed through a hashing function, which gives a hash code. We modify that hash code (can be done within the same hash function) so that it spits out an index number (we also make sure that the index number isn't larger than the length of the array). The value then gets stored at that index number.

Describe a BFS

We search an entire row of nodes before proceeding to the next (could go left to right or right to left, but maybe easier to go left to right since that's how we read?). At the root node we are only searching two nodes, but the deeper we get into the tree, the number of nodes keeps increasing.

Describe a DFS

We travel down a single branch searching for the query. If you find it great, if not, move upwards to

Give an scenario when we might want to use a linked list

When the number of elements is unknown and is expected to grow and/or shrink

What is collision?

When two keys get mapped to the same index

Code a simple hash table in JavaScript. Don't worry about collisions.

class HashTable { constructor() { this.storage = []; this.storageLimit = 20; } hashFunction(key, storageLimit) { let hashCode = 0; for(let i = 0; i < key.length; i++) { hashCode += key.charCodeAt(i); } return hashCode % storageLimit; } addEntry(key, value) { let index = this.hashFunction(key, this.storageLimit); if(this.storage[index] === undefined) { this.storage[index] = [[key, value]]; } else { this.storage[index].push([key, value]); } } search(key) { let index = this.hashFunction(key, this.storageLimit); if(this.storage[index] === undefined) { return "Couldn't find the droid that you're looking for"; } else { return this.storage[index]; } } }

What kind of graph is a binary search tree?

directed, a-cyclic, unweighted graph

What is a hash code?

A deterministic value resulting from calling a hashing function on arbitrary data.

How is a hash table different from an array?

A hash table holds a key/value pair at a particular index while an array only holds a value at a particular index.

Give real world examples of a stack

A pile of plates at a buffet table (plates stacked last are first removed), a fulfillment system favoring the newest orders first. Applications: the "undo" button in a text editor...

What's a graph?

Any data structure composed of nodes that can connect to other nodes. A nonlinear data structure that represents a pictorial structure of a set of objects that are connected by links. Two major components in a graph are vertex (a data element) and edge (a link that connects vertices).

What are some disadvantages of arrays?

Because memory for arrays is consecutive, the larger the array gets the more difficult it is to find that free consecutive memory. Inserting new elements can be expensive, especially if the element is in the middle of the array because we have to shift the other elements over to make room.

What is an undirected graph?

Contains an unordered pair of vertices. The edge connecting two Nodes is ambiguous. It neither starts nor ends at either Node, it merely connects the two like hands in a handshake. Ex.:

What is the primary purpose of hashing?

Converting arbitrary data into fixed-length data. Although hashing functions can also do things like: generate an index for a key/value pair in a hash table, convert strings to numeric hash codes, and find a value in a hash table, the main feature of hashing is to to take data inputs and turn them into hash codes that all have the same bit-length.

What is a stack?

Data structure that uses LIFO (Last In First Out) priority. It provides two functions: push (adds an element to the top of the stack) and pop (removes the top-most element).

What is a data structure?

Data structures are abstract templates to organize data. They organize data in a way that renders it easier to use and manipulate. Most programming languages provide the features necessary to implement all data structures.

What kind of scenarios would it be best to use a data structure, and when should you use something like an object instead?

Data structures are best for holding variable amounts of similarly grouped data (ex.: a list of follows on social media, a list of applicants for a job opening, etc.). For things like a vehicle listing or a performance review, each piece of held data describes a distinct part of the element, so classes would be more appropriate to model them.

What is a directed graph?

Each edge connecting two Nodes indicates a start and an end location. A directed graph contains an ordered pair of vertices. Ex.: each node in a linked list points to the next node, implying a direction of travel. Ex.: In a one-way street, traffic begins at one end and ends at the other.

Describe an array

Elements are stored in consecutive memory, elements are indexed, allows for random access memory, can't grow the length of the array too big.

What are advantages of linked lists over arrays?

Elements aren't stored next to each other in memory, so a lot less expensive to add/remove elements and grow them to huge sizes. Each node is instanstiated dynamically, so the list never has empty placeholders. Since each node is independent in memory, the operating system can use any free space allowing linked lists to grow to immense sizes.

True or false: Data structures must be implemented using classes.

False

True or false: indexing is possible in a linked list

False

True or false: a key in a hash table must be a string

False, although strings are commonly used, any data type can be used including numeric values.

True or false: deleting a node in a binary search tree involves merely moving a child node to take its place

False. Deleting a node from a binary search tree involves finding a suitable replacement and ensuring no sub-trees are lost. Simply promoting a child doesn't guarantee both.

How are hash tables and objects different?

Hash tables vs. objects isn't comparing apples to oranges, but rather is more like comparing apples to Fiji apples: one is a subset of the other. Advantages of hash tables are that it can handle cases where a key corresponds to a native object attribute, and it provides a constant time for lookup. However, in JavaScript we can use bare objects (ES5: we use the create method on the Object prototype to instantiate a new object with the delegation chain specified). These bare objects offer similar benefits of a hash table, but without the headache of creating/implimenting a hash table.

What is hashing?

Hashing is the process of converting a string (the typical data type of a key) into a numerical index. We do this because a hash table ultimately stores its key/value pairs in certain locations in an array, an arrays can only be indexed by numbers.

What is a cyclic graph?

In Cyclic Graphs, Nodes may form a loop. For example, Node A points to B, B points to C, and C points to A. This creates a closed loop in which a Depth-First Search will ultimately travel to a Node it already visited. Ex.: Imagine taking four right-turns in your neighborhood until you reach your starting location: this is a cyclic connection of Nodes.

What is a weighted graph?

In Weighted Graphs, each edge connection features an associated "weight." Engineers use weights to determine the cost of traveling from one Node to another. Ex.: Google may use this technique to determine travel times between locations: each street may have a weight associated with it (travel time).

Describe a queue data structure

It uses FIFO priority (First In First Out) AKA: the first items inserted are the first items removed.

What priority does a queue data structure use?

It uses FIFO priority (First In First Out) AKA: the first items inserted are the first items removed.

What are disadvantages of linked lists over arrays?

Since nodes may exist anywhere in memory (sequential access), accessing the linked list can be inefficient since we can mostly only use a linear search O(n). With arrays, elements are indexed so they can be immediately accessed using a constant growth rate search O(1) (random access). Linked lists are also more complex that arrays.

How can you find an item in a linked list?

Start at the head, check each node following its .next until the item is found or the tail is reached.

What is heuristics?

These are guesses that an algorithm makes to solve a complex problem sooner by sacrificing accuracy.

How are stacks and queues similar?

They are both data structures, and enable engineers to rank tasks, order priorities and operations, and support system functions. Also, both stacks and queues basically push newly added elements after/on top of old elements (FIFO/LIFO, AKA the order in which elements are pulled off, basically determines whether the structure is a stack or queue).

Why do engineers need to use data structures when writing software?

To organize data for easier use and manipulation

True or false: Classes are a type of data structure.

True

True or false: classes belong to object-oriented languages

True

What is an unweighted graph?

Unweighted graphs do not assign the cost of travel between Nodes. Ex.: Linked Lists and most Trees fall under this category of Graph.

What happens when we delete a parent node in a binary search tree?

We can move either a left or right child up to take it's place as long as we follow the other rules. Either algorithm works fine, we just have to stick to that same rule of moving either the left or the right child up to take the parent's place.

Code a singly linked list in JavaScript along with some basic methods

class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } addNode(data) { let newNode = new Node(data); if(this.head === null) { this.head = newNode; } else { let current = this.head; while(current.next !== null) { current = current.next; } current.next = newNode; } } removeNode(data) { let current = this.head; let previous; if(current.data === data) { this.head = current.next; } else { while(current.data !== data) { previous = current; current = current.next; } previous.next = current.next; } } print() { let current = this.head; while(current !== null) { console.log(current.data); current = current.next; } } linearSearch(query) { let current = this.head; while(current !== null) { if (current.data === query) { return current.data; } else { current = current.next; } } return "Item not found"; } }

Write a binary search tree in JavaScript along with some basic methods like insert() and traversing

class Node { constructor(data, left = null, right = null) { this.data = data; this.left = left; this.right = right; } } class binarySearchTree { constructor() { this.root = null; } add(data) { const node = this.root; if(node === null) { this.root = new Node(data); return; } else { const searchTree = function(node) { if(data < node.data) { if(node.left === null) { node.left = new Node(data); } else if (node.left !== null) { return searchTree(node.left); } } else if (data > node.data) { if (node.right === null) { node.right = new Node(data); return; } else if (node.right !== null) { return searchTree(node.right); } } else { return null; } }; return searchTree(node); } } find(data) { let current = this.root; while (current.data !== data) { if(data < current.data) { current = current.left; } else { current = current.right; } if(current === null) { return null; } } return current; } }


Related study sets

MKTG 4280 Ch. 8 McGraw-Hill Connect

View Set

Unit 6: Palliative Care REVIEW QUESTIONS

View Set

Chapter 34: Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set