Data abstractions are a super powerful way to let people treat code as objects, rather than knowing how the thing works itself
Allows you to worry about how something works, rather than how something is implemented
You'll see a lot of abstractions in other courses (Data 8, Data 100 are filled with abstractions of some sort)
What are Data Abstractions?
Data abstractions have the following:
Constructors: Used to build the abstract data type
IMPORTANT: You do not need to know how the programmer decided to implement this!
Selectors: Used to interact with the data type
Example: Tree Data Abstraction
Trees are recursive data structures (as in, trees contain more trees)
Important terms:
Root Node
Branch(es)
This will be a list!
Leaf Node
Children
Sort of looks like an upside-down tree compared to the real world
Questions are generally solved using tree recursions
Tree ADT Implementation:
deftree(label, branches=[]):"""Construct a tree with the given label value and a list of branches."""return [label] + list(branches) # All items in branches must be trees!deflabel(tree):"""Return the label value of a tree."""return tree[0]
defbranches(tree):"""Return the list of branches of the given tree."""return tree[1:]
defis_leaf(tree):returnnot branches(tree)
Tree Example:
t = tree(1,
[tree(3,
[tree(4),
tree(5),
tree(6)]),
tree(2)])