class: center, title-slide
## CSCI-UA 480: APS ## Algorithmic Problem Solving
## Non-Linear Data Structures ### BST and Hash Table .author[ Instructor: Joanna Klukowska
] .license[ Copyright 2020 Joanna Klukowska. Unless noted otherwise all content is released under a
[Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).
Background image by Stewart Weiss
] --- layout:true template: default name: section class: inverse, middle, center --- layout:true template: default name: challenge class: challenge --- layout:true template: default name: poll class: inverse, full-height, center, middle --- layout:true template: default name: breakout class: breakout --- layout:true template:default name:slide class: slide .bottom-left[© Joanna Klukowska. CC-BY-SA.] --- ## Balanced Binary Search Tree - binary tree - for each node - all the values in the left subtree are smaller than the value in that node - all the values in the right subtree are greater than the value in that node - (equal values should be stored in one of the subtrees, if allowed) -- - `map`/`set`/`multiset` in C++ and `TreeMap` / `TreeSet` in Java - the _map_ versions of the classes store `(key, value)` pairs - the _set_ versions of the classes store only keys (which have to be unique) - the `multiset` (only in C++) allows duplicate keys -- - $O(log N)$ on - add key, - remove key, - find key , - find min/max, - successor/predecessor key -- - AVL tree, Red-Black tree --- template: challenge ## Challenge For each o the following problems 1. come up with tests that could be used for the implementation, 2. come up with an algorithm(s) that can be used to solve the problem, 3. what is the performance of your algorithms? -- __Problem 1__ Given a root to a binary tree containing `N` nodes, determine if it is a binary search tree? __Problem 2__ Given a binary search tree containing `N` nodes, output the elements with values in the range [a..b] in ascending order. __Problem 3__ Given a binary search tree containing `N` nodes, output the values in the leaves in ascending/descending order. --- ## Hash Table - mapping from _keys_ to _values_ not sorted in any particular/predictable way - lookups by key can be done in $O(1)$ (assuming a good hash function, but $O(N)$ worst case) - addition / removal done in $O(1)$ (but $O(N)$ worst case) -- - [`unordered_map`](http://www.cplusplus.com/reference/unordered_map/unordered_map/?kw=unordered_map) in C++ () - starting in C++11 standard - `HashMap`, `HashSet`, `HashTable` in Java -- - challenge: depends on a well designed hash function (and that is often tricky) -- - Direct Addressing Table (DAT) is the simplest form of a hash table in which _keys_ are the indexes - it is simply an array ---