
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This study guide provides a concise overview of fundamental data structures, including arrays, linked lists, binary search trees, and hash tables. It covers key concepts such as hashing, chaining, max-heaps, min-heaps, and abstract data types (adts) like stacks, queues, bags, sets, and priority queues. The guide also explains essential operations and implementations in python, making it a valuable resource for students learning data structures and algorithms. It includes clear definitions and examples to aid understanding and retention, focusing on the practical aspects of data structure implementation and usage. Useful for university students.
Typology: Exams
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















Array - CORRECT ANSWERA data structure that stores an ordered list of items, with each item is directly accessible by a positional index. Linked List - CORRECT ANSWERA data structure that stores ordered list of items in nodes, where each node stores data and has a pointer to the next node. Bianary Search Tree - CORRECT ANSWERA data structure in which each node stores data and has up to two children, known as a left child and a right child. Hash Table - CORRECT ANSWERA data structure that stores unordered items by mapping (or hashing) each item to a location in an array (or vector). Hashing - CORRECT ANSWERmapping each item to a location in an array (in a hash table).
Chaining - CORRECT ANSWERhandles hash table collisions by using a list for each bucket, where each list may store multiple items that map to the same bucket. Hash key - CORRECT ANSWERvalue used to map an index bucket - CORRECT ANSWEReach array element in a hash table ie A 100 elements hash table has 100 buckets modulo hash function - CORRECT ANSWERcomputes a bucket index from the items key. It will map (num_keys / num_buckets) keys to each bucket. ie... keys range 0 to 49 will have 5 keys per bucket. 50 / 10 = 5 hash table searching - CORRECT ANSWERHash tables support fast search, insert, and remove. Requires on average O(1) Linear search requires O(N) modulo operator % - CORRECT ANSWERcommon has function uses this. which computes the integer remainder when dividing two numbers. Ex: For a 20 element hash table, a hash function of key % 20 will map keys to bucket indices 0 to 19. Max-Heap - CORRECT ANSWERA binary tree that maintains the simple property that a node's key is greater than or equal to the node's childrens' keys. (Actually, a max-heap may be any tree, but is commonly a binary tree).
Implementing priority queues with heaps. - CORRECT ANSWERBoth functions return the value in the root, but the Pop function removes the value and the Peek function does not. Pop is worst-case O(logN) and Peek is worst-case O(1). Push and pop operate have runtime O(logN). All other operations (Peek, IsEmpty, GetLength) happen in constant time O(1). Array based list - CORRECT ANSWERA list ADT implemented using an array. An array-based list supports the common list ADT operations, such as append, prepend, insert after, remove, and search. Linked list vs Array - CORRECT ANSWERIf a program requires fast insertion of new data, a linked list is a better choice than an array. Abstract Data Type (ADT) - CORRECT ANSWERA data type described by predefined user operations, such as "insert data at rear," without indicating how each operation is implemented. List - CORRECT ANSWERAn ADT for holding ordered data. Dups ok Sequence type: A mutable container with ordered elements. Underlying data structures: Array, linked list Array in Java - CORRECT ANSWERgeneric class that supports different data types. declared as follows, where T is the data type. Tuple - CORRECT ANSWERSequence type: An immutable container with ordered elements.
Peek(stack) Returns but does not remove item at top of stack Peek(stack) returns 99. Stack still: 99, 77 IsEmpty(stack) Returns true if stack has no items IsEmpty(stack) returns false. GetLength(stack) Returns the number of items in the stack GetLength(stack) returns 2. Implementing a stack in python (Linear data structure) - CORRECT ANSWERCan be implemented in Python using a class with a single LinkedList data member. The class has two methods, push() and pop(). push() adds a node to the top of the stack's list by calling LinkedList's prepend() method. *New elements are place on the top of the stack, not at the bottom of the stack.
pop() removes the head of the stack's list by calling the LinkedList's remove_after() method and then returns the removed node. Implementing a queue in python (Linear data structure) - CORRECT ANSWERCan also be implemented in Python using a class with a single LinkedList data member and class methods push() and pop(). push() adds a node to the end of the queue's list by calling LinkedList's append() method. *New elements are added to the end of a queue. The pop() method removed the queue's head node and is identical to Stack's pop() method. Queue - CORRECT ANSWERAn ADT in which items are inserted at the end of the queue and removed from the front of the queue. *first-in first-out ADT. Underlying data structures: Linked list, Array, Vector The Queue class' push() method uses the LinkedList append() method to insert elements in a queue. Both the Stack and Queue pop() methods operate exactly the same by removing the head element and returning the removed element.
Deque - CORRECT ANSWERShort for double-ended queue- an ADT in which items can be inserted and removed at both the front and back. Underlying data structures: Linked list Bag - CORRECT ANSWERAn ADT for storing items in which the order does not matter and duplicate items are allowed. Underlying data structures: Linked list, Array Set - CORRECT ANSWERAn ADT for a collection of distinct items. (no dups!) Underlying data structures: Binary search tree, Hash table Priority queue - CORRECT ANSWERA queue where each item has a priority, and items with higher priority are closer to the front of the queue than items with lower priority. Dups ok Underlying data structures: Heap *In addition to push and pop, a priority queue usually supports peeking and length querying. A peek operation returns the highest priority item, without removing the item from the front of the queue. Pop returns front or head item which is top priority item Dictionary (Map) - CORRECT ANSWERA dictionary is an ADT that associates (or maps) keys with values.
Underlying data structures: Binary search tree, Hash table Dictionary key characteristic - CORRECT ANSWERThey are unique and immutable. Dictionary method - CORRECT ANSWERD1[key].remove(value) dict.items() - CORRECT ANSWERreturns a view object that yields (key, value) tuples. dict.keys() - CORRECT ANSWERreturns a view object that yields dictionary keys. dict.values() - CORRECT ANSWERreturns a view object that yields dictionary values. Dict for loop - CORRECT ANSWERA for loop over a dict retrieves each key in the dict. ie.. for key in dictionary: dict operations - CORRECT ANSWERmy_dict[key] Indexing operation - retrieves the value associated with key. john_grade = my_dict['John'] my_dict[key] = value Adds an entry if the entry does not exist, else modifies the existing entry. my_dict['John'] = 'B+' del my_dict[key] Deletes the key entry from a dict.
my_dict1.update(my_dict2) Merges dictionary my_dict with another dictionary my_dict2. Existing entries in my_dict1 are overwritten if the same keys exist in my_dict2. my_dict = {'Bob': 1, 'Jane': 42} my_dict.update({'John': 50}) print(my_dict) {'Bob': 1, 'Jane': 42, 'John': 50} my_dict.pop(key, default) Removes and returns the key value from the dictionary. If key does not exist, then default is returned. my_dict = {'Bob': 1, 'Jane': 42} val = my_dict.pop('Bob') print(my_dict) {'Jane': 42} Underlying data structures: Binary search tree, Hash table - CORRECT ANSWERSet, Dictionary(Map) Underlying data structures: Heap - CORRECT ANSWERPriority queue Underlying data structures: Linked list, Array - CORRECT ANSWERBag, Queue, List Underlying data structures: Linked list - CORRECT ANSWERDeque, Stack
Common operations for ADT List - CORRECT ANSWERAppend, Prepend, InsertAfter, Print, PrintReverse, Sort, Remove, Search, IsEmpty, GetLength Common operations for ADT Queue, Stack, Deque - CORRECT ANSWERPush, Pop, Peak, IsEmpty, GetLength Float - CORRECT ANSWERData type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers. Double - CORRECT ANSWERA data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. Byte - CORRECT ANSWERData type is an 8-bit signed two's complement integer. The byte data type is useful for saving memory in large arrays. Range(5) - CORRECT ANSWER0 1 2 3 4 Every integer from 0 to 4 Assignment vs comparison - CORRECT ANSWER= vs == garbage collection - CORRECT ANSWERIt reclaims memory from data structures implemented using linked allocations. Python is a managed language, meaning objects are deallocated automatically by the Python runtime, and not by the programmer's code. When an object is no longer referenced by any variables, the object becomes a candidate for deallocation. Python's garbage collector will deallocate objects with a reference count of 0. However, the time between an object's reference count becoming 0 and that object being deallocated may differ across different Python runtime implementations.
edge - CORRECT ANSWERa connection between two vertices in a graph. A finite set of ordered pair of the form (u, v) E - > Number of Edges Graph weight - CORRECT ANSWERWeighted Graph : The Graph in which weight is associated with the edges. Unweighted Graph : The Graph in which their is no weight associated to the edges. Graph Direction - CORRECT ANSWERUndirected Graph : The graph in which all the edges are bidirectional. Directed Graph : The graph in which all the edges are unidirectional. Binary Tree - CORRECT ANSWERIn a list, each node has up to one successor. In a binary tree, each node has up to two children, known as a left child and a right child. "Binary" means two, referring to the two children. Leaf - CORRECT ANSWERA tree node with no children. Internal node - CORRECT ANSWERA node with at least one child. Parent - CORRECT ANSWERA node with a child is said to be that child's parent. Node's ancestors - CORRECT ANSWERinclude the node's parent, the parent's parent, etc., up to the tree's root. Root - CORRECT ANSWERThe one tree node with no parent (the "top" node).
Depth, level, and height (bianary tree) - CORRECT ANSWERThe link from a node to a child is called an edge. A node's depth is the number of edges on the path from the root to the node. The root node thus has depth 0. All nodes with the same depth form a tree level. A tree's height is the largest depth of any node. A tree with just one node has height 0. A binary tree is full if: - CORRECT ANSWERevery node contains 0 or 2 children. A binary tree is complete if: - CORRECT ANSWERall levels except possibly the last are completely full, and the last level has all its nodes to the left side A binary tree is perfect if: - CORRECT ANSWERif all internal nodes have 2 children and all leaf nodes are at the same level. Tree traversal - CORRECT ANSWERAn algorithm visits all nodes in the tree once and performs an operation on each node. BST inorder traversal - CORRECT ANSWERVisits all nodes in a BST from smallest to largest, which is useful for example to print the tree's nodes in sorted order. Starting from the root, the algorithm recursively prints the left subtree, the current node, and the right subtree.
push() adds a node to the top of the stack's list by calling LinkedList's prepend() method. New elements are place on the top of the stack, not at the bottom of the stack. push(numStack, 8) = 8, 7, 5 Queue: queue: 43, 12, 77 push() adds a node to the end of the queue's list by calling LinkedList's append() method. New elements are added to the end of a queue. Push(queue, 56). Queue: 43, 12, 77, 56 Priority Queue: The priority queue push operation inserts an item such that the item is closer to the front than all items of lower priority, and closer to the end than all items of equal or higher priority. O(logN) - CORRECT ANSWERWe write O(logN) not O(log10N) or O(log^2N) Fast sorting algorithm - CORRECT ANSWERA sorting algorithm that has an average runtime complexity of O(N logN) or better. Bubble sort algorithm - CORRECT ANSWERLook for something that swaps so the result can "bubble" to the top. *best used when the data is small
Sorting algorithm that iterates through a list, comparing and swapping adjacent elements if the second element is less than the first element. Bubble sort uses nested loops. Given a list with N elements, the outer i-loop iterates N times. Because of the nested loops, bubble sort has a runtime of O(N2). Bubble sort is often considered impractical for real-world use because many faster sorting algorithms exist. Figure 11.20.1: Bubble sort algorithm. BubbleSort(numbers, numbersSize) { for (i = 0; i < numbersSize - 1; i++) { for (j = 0; j < numbersSize - i - 1; j++) { if (numbers[j] > numbers[j+1]) { temp = numbers[j] numbers[j] = numbers[j + 1] numbers[j + 1] = temp }} Bucket sort algorithm - CORRECT ANSWER**Look for something that distributes the values into "buckets" where they are individually sorted. **Bucket sort is mainly useful when input is uniformly distributed over a range. The bucket index is calculated as ⌊number∗(N−1)/M⌋