Link

Sets, Maps, and BSTs

Complete the Reading Quiz by noon before lecture.

Table of contents

  1. Collections
  2. Binary Search Trees

In the very first lecture, we introduced the idea of an abstract data type.

A variable’s data type (or simply type) determines its possible values and operations. An abstract data type (ADT) is a data type that does not specify any one implementation. Data structures, such as resizable arrays or linked nodes, implement ADTs.

For example, a List (java.util.List<E>) is a collection storing an ordered sequence of elements.

add(int index, E element)
Elements can be added to the front, back, or any index in the list.
get(int index)
Each element is accessible by a zero-based index.
remove(Object o)
Optionally, elements can be removed from the list.

Collections

In Java, abstract data types and their data structure implementations are provided by the Java Collections Framework, which includes familiar interfaces like List as well as classes like ArrayList and LinkedList. Two other commonly used interfaces are the Set and Map interfaces which represent the two new ADTs that we’ll be learning in class.

A Set (java.util.Set<E>) is an unordered collection of unique elements.

add(E element)
Elements can be added to the set.
contains(Object o)
Elements can be checked to see if they are contained in the set.
remove(Object o)
Optionally, elements can be removed from the set.

A Map (java.util.Map<K, V>) is a set of key-value pairs where each key is unique and has a corresponding value that is not necessarily unique.

put(K key, V value)
A key-value pair can be added to the map.
containsKey(Object key)
Keys can be checked to see if they are contained in the map.
get(Object key)
The value field in each key-value pair is accessible given its corresponding key.
remove(Object key)
Optionally, elements can be removed from the map.

A note on terminology.

Key
A unique identifier used to access items in a data structure as used in the Map ADT. The elements added to a Set can be considered keys. Depending on the context, it can be used interchangeably with “item” or “element”.

Binary Search Trees

A tree is a node-based data structure. Like linked nodes, trees are a recursive data structure: each parent node has references to any number of child nodes. We use very particular terminology to discuss trees.

A binary tree is a tree where each node is restricted to only 0, 1, or 2 children, addressed as either the left or the right child relative to the parent node.

A binary search tree is a binary tree with the added binary search property: all children to the left of a node contain smaller values and all children to the right of a node contain larger values.

We have some specific vocabulary when discussing tree structures.

Tree Terminology

The labels on the left side represent a recursive description of the tree, while the labels on the right side represent a relative description of the tree.

Recursive Description: root and subtree
Each subtree is itself a valid tree. A tree with zero subtrees is a leaf.
Relative Desciption: node and value
Each node has a value. A parent node is joined with an edge to each child node.

Reading Quiz