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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

PROOF BY CONTRADICTION
School of Computing and Mathematics, University of Huddersfield CAS2545: WEEK 11 LECTURE: n The meaning of Algebraic Specifications TUTORIAL/PRACTICAL:
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Binary Search Trees vs. Binary Heaps. Binary Search Tree Condition Given a node i –i.value is the stored object –i.left and i.right point to other nodes.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
1 Section 10.1 What Is an Algebra? To most people the word algebra means high school algebra. But there are many kinds of algebra. An algebra consists.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
1 Chapter Elementary Notions and Notations.
Construction Techniques (1) - 1ICOM 4075 (Fall, 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez 2005.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
1 Section 13.2 The Church-Turing Thesis The Church-Turing Thesis: Anything that is intuitively computable can be be computed by a Turing machine. It is.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
1 Section 3.2 Recursively Defined Functions and Procedures A function ƒ is recursively defined if at least one value ƒ(x) is defined in terms of another.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Section 13.1 Turing Machines A Turing machine (TM) is a simple computer that has an infinite amount of storage in the form of cells on an infinite tape.
1 Turing Machines and Equivalent Models Section 13.1 Turing Machines.
1 Chapter Construction Techniques. 2 Section 3.1 Inductively Defined Sets To define a set S inductively is to do three things: Basis: Specify one.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Data Structure By Amee Trivedi.
Trees Chapter 15.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Algebraic Specifications
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Chapter 20: Binary Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 21: Binary Trees.
General Trees & Binary Trees
Priority Queues and Heaps
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Objective Solve equations in one variable that contain variable terms on both sides.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
2018, Fall Pusan National University Ki-Joune Li
Stacks with Dynamic Memory
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Section 3.1 Inductively Defined Sets
Objective Solve equations in one variable that contain variable terms on both sides.
Chapter 20: Binary Trees.
B.Ramamurthy Chapter 9 CSE116A,B
Tree.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
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  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  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. Notice that the two constuctors 0 and succ construct the infinite set N = {0, succ(0), succ(succ(0)), …, succ n (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. 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

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 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)). 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

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 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)))). Binary Trees:  A, B(A), Bool; emptyT, isemptyT, tree, root, left, right  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 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)

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)  Adelbest : 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.