Abstract data types (B4.1)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/32

encourage image

There are no tags or description

Looks like no tags are added yet.

33 Terms

1
New cards

Abstract data type (ADT)

Public operation that determines how data structure behaves

  • Contract - agreement on data behavior

  • Implementation - how to actually code contract: different methods


2
New cards

ADT Property: Abstraction

Hide low-level details to focus on the essentials

3
New cards

ADT Property: Encapsulation

Protect internal state behind methods

4
New cards

ADT Property: Modularity

Top-down approach/step-wise refinement: develop and test components independently

5
New cards

ADT Property: Reusability

Use same contract in other applications

6
New cards

Linked list

Chain of nodes, not necessarily stored contiguously

  • Node - data + pointer to another node

  • Head - start

  • Tail - end, pointer to null

  • Sequential access: time complexity O(n)


<p>Chain of nodes, not necessarily stored contiguously</p><ul><li><p>Node - data + pointer to another node</p></li><li><p>Head - start</p></li><li><p>Tail - end, pointer to null</p></li><li><p>Sequential access: time complexity O(n)</p></li></ul><p></p>
7
New cards

Linked list types

Singly linked - unidirectional

Doubly linked - bidirectional

Circular - tail points back to head instead of null

<p>Singly linked - unidirectional</p><p>Doubly linked - bidirectional</p><p>Circular - tail points back to head instead of null</p>
8
New cards

Linked list insertion at head

  1. Move head to be the node after the inserted node

  2. Set inserted node to be the head


<ol><li><p>Move head to be the node after the inserted node</p></li><li><p>Set inserted node to be the head</p></li></ol><p></p>
9
New cards

Linked list insertion in middle

Node that is currently at position that node needs to be inserted into will be referred to as current node

  1. Set the node after the current node to be the node after the inserted node (new.next = current.next)

  2. Set the node after the current node to be the inserted node (current.next = new)


<p>Node that is currently at position that node needs to be inserted into will be referred to as current node</p><ol><li><p>Set the node after the current node to be the node after the inserted node (new.next = current.next)</p></li><li><p>Set the node after the current node to be the inserted node (current.next = new)</p></li></ol><p></p>
10
New cards

Linked list insertion at end

  1. Set the node after the inserted node to be null (new.next = null)

  2. If the head is null, set the inserted node to be head

  3. Else set node after current tail to be inserted node (tail.next = new)


11
New cards

Linked list deleting node

  • If deleting head: set node after head to be new head (head.next = head)

  • Else: Let the node after the deleted node to be after the node before the deleted node (previous.next = deleted.next)

  • Old node becomes unlinked, to be reclaimed by garbage collector


12
New cards

Linked list evaluation

+Dynamic size

+O(1) insertion time complexity

+Insertion doesn’t require shifting of later elements

-O(n) access time complexity

-Cannot jump directly to index in O(1)

-Extra memory required for pointers/link field

-Difficult to identify specific location in memory (poor locality)


13
New cards

Binary search tree (BST)

Tree with values so that left subtree values are smaller and right are greater

  • Root: first node

  • Parent: node with subnodes

  • Child: submode

  • Edge: connection between parent and child

  • Subtree: smaller tree

  • Leaf: node with no child

  • Height/depth: longest edge from root to last leaf


<p>Tree with values so that left subtree values are smaller and right are greater</p><ul><li><p>Root: first node</p></li><li><p>Parent: node with subnodes</p></li><li><p>Child: submode</p></li><li><p>Edge: connection between parent and child</p></li><li><p>Subtree: smaller tree</p></li><li><p>Leaf: node with no child</p></li><li><p>Height/depth: longest edge from root to last leaf</p></li></ul><p></p>
14
New cards

BST build from input sequence

  1. Start at root

  2. If inserted node is smaller, go left. Else, go right.

  3. Continue until there’s no more subnodes

Unbalanced if insert in ascending order: best to start with median

<ol><li><p>Start at root</p></li><li><p>If inserted node is smaller, go left. Else, go right.</p></li><li><p>Continue until there’s no more subnodes</p></li></ol><p>Unbalanced if insert in ascending order: best to start with median</p>
15
New cards

Inorder traversal

Left, main, right: visit nodes in ascending order

  • Can be used for infix notation


<p>Left, main, right: visit nodes in ascending order</p><ul><li><p>Can be used for infix notation</p></li></ul><p></p>
16
New cards

Preorder traversal

Main, left, right

  • Can be used for prefix notation (operators before operands)


<p>Main, left, right</p><ul><li><p>Can be used for prefix notation (operators before operands)</p></li></ul><p></p>
17
New cards

Postorder traversal

Left, right, main

  • Can be used for postfix notation (operator after operands)


<p>Left, right, main</p><ul><li><p>Can be used for postfix notation (operator after operands)</p></li></ul><p></p>
18
New cards

BST min and max

Go left/right respectively until no more subnodes

19
New cards

BST Insertion

  1. If BST is empty: insert at root

  2. Else: follow root until finding parent node that:

    1. If larger than inserted: insert as right child

    2. If smaller than inserted: insert as left child


20
New cards

BST duplicate values

Either ban or modify selection (time consuming, have to traverse entire tree if searching for value)

21
New cards

BST deletion

  1. If deleted has no child: just delete

  2. If it has 1 child: connect deleted’s parent directly to child

  3. If it has 2 children: replace deleted node with largest value in left subtree OR smallest value in right subtree


<ol><li><p>If deleted has no child: just delete</p></li><li><p>If it has 1 child: connect deleted’s parent directly to child</p></li><li><p>If it has 2 children: replace deleted node with largest value in left subtree OR smallest value in right subtree</p></li></ol><p></p>
22
New cards

Set

All elements must be unique, order doesn’t matter

  • Subset - all elements are in a parent set

  • Superset - parent set

  • Implemented using hash tables


<p>All elements must be unique, order doesn’t matter</p><ul><li><p>Subset - all elements are in a parent set</p></li><li><p>Superset - parent set</p></li><li><p>Implemented using hash tables</p></li></ul><p></p>
23
New cards

Comparing sets

Union (U): all in either

Intersection: only elements in common

Subtraction: what don’t have in common

<p>Union (U): all in either</p><p>Intersection: only elements in common</p><p>Subtraction: what don’t have in common</p>
24
New cards

Hash table

Set that stores and retrieves data using a hash function

  • Stores in key-value pairs

  • Uses underlying array to organize entries

  • Key - identifier of record

  • Index - numerical position in underlying array

  • Bucket - storage location in hash table that hold 1/+ entries

  • Value - info associated with key

  • Well designed = average time complexity O(1) for searching, inserting, deleting


25
New cards

Hash table hash function

Convert key into array index to find which bucket in underlying array to store pair

  • Assign key-value pair to bucket of number (key % size) (search for a pair in same way)

  • Allows keys to be calculated instead of having to iterate sequentially


<p>Convert key into array index to find which bucket in underlying array to store pair</p><ul><li><p>Assign key-value pair to bucket of number (key % size) (search for a pair in same way)</p></li><li><p>Allows keys to be calculated instead of having to iterate sequentially</p></li></ul><p></p>
26
New cards

Hash table Collision

Multiple different pairs have same calculated bucket

27
New cards

Collision solution: Chaining

Have each bucket point to chain (linked list of entries)

<p>Have each bucket point to chain (linked list of entries)</p>
28
New cards

Collision solution: Open addressing

Search for another bucket within table

29
New cards

Open addressing: Linear probing

Check directly next available bucket

  • may cause primary cluster (all values clustered in 1 area): ^ clustering = ^ likelihood of further collisions


30
New cards

Open addressing: Quadratic probing

Increasingly spaced offsets based on square increments

  • Reduces some clustering


31
New cards

Open addressing: Double hashing

Use second hash function to find bucket

  • ^ complexity, better distribution


32
New cards

Hash table Load factor

Calculated threshold: how full table is (full buckets / size)

33
New cards

Hash table Rehashing

When reach threshold/resizing: allocation larger table and calculate new buckets for existing keys