Algorithms
and
Data
Structures

Spring 2021
course
site
-->

March 25 - heaps & hash tables

... start recording

Questions and/or comments on last homework ...

Tips for the midterm paper :

Where we are and where we're going :

heap

i.e. "priority queue as binary heap using indexed array"

... we started working through this last time; let's finish that. I've attached heap_v1.py (a slightly expanded version of what we started in class), and heap_final.py (working code).

hash tables

The next data structure I'd like to look at is an important one - the "hash table" , also called a "dictonary" or a "key/value store" or even a "map".

Hash tables a built-in feature of many programming languages, and even a part of how functions work - each "frame" is essentially a dictionary of variable names and values.

To get us all on the same page, let's first review how these work in python : a = {}; a['foo'] = 'bar'; a.get("thing", None) .

Their features are :

Places to start read about them :

There are many variations but the core ideas are

I'd like to write something together similar to this dictionary.py from a 2019 algorithms course of mine.

The goal is to implement a python dictionary with C-like data structures - not with the python builtin.

class HashTable:
    def __init__(self):
        pass   # TODO
    def get(self, key):
        """ return corresponding value or None if not found """
        # TODO
        return None
    def set(self, key, value):
        """ put (key,value) into the hash table """
        pass # TODO

Over the next week I'd like us to work on a more realistic C hash table with some sort of collision algorithm. (C doesn't have a builtin data structure like this.)

I'm thinking of something along the lines of a class project for next week, working on a template that I'll create, using strings as keys.

aside - javascript

Python has objects with "properties" and methods, and dictionaries with key lookup .

Javascript puts those two ideas into one thing ... see for example working with objects .

wiki

I'd like to use put a shortcut summary of everything we're doing this term in one place, as a reference and kind of "mind map" of the ideas and concepts.

Under constructions ... the wiki . (This is a page that only we can see, and all of us can edit.)

https://cs.bennington.college /courses /spring2021 /algorithms /notes /heap
last modified Thu March 25 2021 5:00 pm

attachments [paper clip]

  last modified size
TXT heap_final.py Thu Mar 25 2021 12:47 am 6.8K
TXT heap_v1.py Thu Mar 25 2021 12:47 am 4.9K