1 / 6

Introduction to Abstract Data Types and their Algebras

This article provides an overview of abstract data types (ADTs) and their algebras. It explores various basic ADTs such as propositional logic, natural numbers, lists, strings, stacks, queues, and binary trees. The article also includes example definitions and operations for each ADT.

capetillo
Download Presentation

Introduction to Abstract Data Types and their Algebras

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 10.3 Abstract Data Types as Algebras An abstract data type (ADT) is an algebra in which the carriers (i.e., the data sets) and the operations can be programmed. We’ll look at some basic abstract data types. Propositional Logic: Bool; true, false, not, and Bool = {true, false} not : Bool  Bool and : Bool  Bool  Bool Axioms: not(true) = false not(false) = true and(x, y) = true iff x = y = true. Example. The logical or operation can be defined as follows: or(x, y) = not(not(x) and not(y)). Quiz (1 minute). Define the logical implies operation. Answer. implies(x, y) = not(x) or y. Natural Numbers: N, Bool; 0, iszero, succ, pred Axioms: iszero(0) = true iszero(succ(x)) = false pred(0) = 0 pred(succ(x)) = x 0  N iszero : N  Bool succ : NN pred : N  N Notice that the two constuctors 0 and succ construct the infinite set N = {0, succ(0), succ(succ(0)), …, succn(0), ….}. The axioms imply the elements of N are distinct. e.g., succ(x) ≠ 0. For if succ(x) = 0, then apply iszero to both sides to get the contradiction false = true. So succ(x) ≠ 0.

  2. Quiz (1 minute). The nonzero elements of N are distinct because succ is an injection. i.e., succ(x) = succ(y) implies x = y. Prove that succ is an injection. Proof: If succ(x) = succ(y), then apply pred to get x = pred(succ(x)) = pred(succ(y)) = y. Many useful operations can be defined with the algebra: e.g., plus, times, less, etc. Example. Let equal : N  N  Bool be a test for equality. We can define equal as follows: equal(0, 0) = true equal(0, succ(x)) = false equal(succ(x), 0) = false equal(succ(x), succ(y)) = equal(x, y). Quiz (1 minute). Define equal using if-then-else. Solution: equal(x, y) = if iszero(x) and iszero(y) then true else if iszero(x) or iszero(y) then false else equal(pred(x), pred(y)). Example/Quiz. Let gte(x, y) mean x is greater than or equal to y. Define gte. Solution. gte(x, 0) = true gte(0, succ(x)) = false gte(succ(x), succ(y)) = gte(x, y). Alternatively, gte(x, y) = if izero(y) then true else if izero(x) then false else gte(pred(x), pred(y)).

  3. Lists:A, lists(A), Bool;  , isempty, cons, head, tail, where A represents any set.    lists(A) isempty : lists(A)  Bool cons : A  lists(A)  lists(A) head : lists(A)  A tail : lists(A)  lists(A) Axioms: isempty( ) = true isempty(cons(h, t)) = false head(cons(h, t)) = h tail(cons(h, t)) = t Many useful operations can be defined with the algebra: e.g., length, member, etc. Example. Let last : lists(A)  A return the rightmost element of a nonempty list. We can define last as follows: last(x) = if isempty(x) then error else if isempty(tail(x)) then head(x) else last(tail(x)). Quiz (1 minute). Write the definition for last in equational form. Answer. last(cons(x,  ) = x last(cons(x, y) = last(y). Example. Let cat : lists(A)  lists(A)  lists(A) concatenate two lists. We can define cat as follows: cat( , x) = x cat(cons(h, t), x) = cons(h, cat(t, x)). Quiz (1 minute). Write the definition for cat in if-then-else form. Solution: cat(x, y) = if isempty(x) then y else cons(head(x), cat(tail(x),y)).

  4. Example. Let putlast : A  lists(A)  lists(A) place an element at the right end of a list. We can define putlast as follows: putlast(x,  ) = cons(x, ) putlast(x, cons(h, t)) = cons(h, putlast(x, t)). Quiz (1 minute). Write the definition for putlast in if-then-else form. Solution: putlast(x, y) = if isempty(y) cons(x, ) else cons(head(y), putlast(x, tail(y))). Strings, Stacks, and Queues ADT for strings is like that for lists. ADT for stacks can defined in terms of lists by letting stacks(A) = lists(A) emptyS =   isemptyS = isempty push = cons pop = tail top = head. ADT for queues can defined in terms of lists by letting queues(A) = lists(A) emptyQ =   isemptyQ = isempty addqueue = putlast frontqueue = head deletequeue = tail.

  5. Binary Trees:A, B(A), Bool; emptyT, isemptyT, tree, root, left, right emptyT  B(A) isemptyT : B(A)  Bool tree : B(A)A  B(A)  B(A) root : B(A)  A left : B(A)  B(A) right : B(A)  B(A) Axioms: isemptyT(emptyT) = true isempty(tree(L, x, R)) = false root(tree(L, x, R)) = x left(tree(L, x, R)) = L right(tree(L, x, R)) = R Example. Let leaves: B(A)  N return the number of leaves in a binary tree. We can define leaves as follows, assuming we have the ADT for natural numbers. leaves(emptyT) = 0 leaves(tree(emptyT, x, emptyT)) = 1 leaves(tree(L, x, R)) = leaves(L) + leaves(R). Quiz (1 minute). Write an if-then-else definition for leaves. Solution. leaves(T) = if isempty(T) then 0 else if isempty(left(T)) and isempty(right(T)) then 1 else leaves(left(T)) + leaves(right(T)). Example/Quiz. Let pre : B(A)  lists(A) return the list of nodes from a preorder traversal of a binary tree. Solution. pre(emptyT) =   pre(tree(L, x, R)) = cons(x, cat(pre(L), pre(R))). Quiz (1 minute). Write the if-then-else form of pre. Answer. pre(T) = if isemptyT(T) then   else cons(root(T), cat(pre(left(T)), pre(right(T)))).

  6. Quiz (1 minute). Let inord : B(A)  lists(A) return the list of nodes from an inorder traversal of a binary tree. Solution. inord(emptyT) =   inord(tree(L, x, R)) = cat(inord(L), cons(x, inord(R))). An if-then-else form: inord(T) = if isemptyT(T) then   else cat(inord(left(T), cons(root(T), inord(right(T))). Priority Queues:A, P(A), Bool; emptyP, isemptyP, insert, better, best, delbest emptyP  P(A) isemptyP : P(A)  Bool insert : A  P(A)  P(A) better : A  A  Bool best : P(A)  A delbest : P(A)  P(A). Main Axioms: best(insert(a, p)) = if isemptyP(p) or better(a, best(p)) then a else best(p). delebest(insert(a, p)) = if isemptyP(p) or better(a, best(p)) then p else insert(a, delebest(p)). Example/Quiz. Let ƒ : P(A)  lists(A) be the function where ƒ(p) is the list of elements from p with the best element at the head of the list. Find a definition for ƒ. Solution. ƒ(p) = if isemptyP(p) then   else cons(best(p), ƒ(delebest(p))). Quiz (1 minute). How can we implement a stack with a priority queue? Answer. Let push = insert, pop = delbest, top = best, and better = true.

More Related