CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 19 CS 1813 – Discrete Mathematics Trees and Inductive Definitions.

Slides:



Advertisements
Similar presentations
1 Functional Programming Lecture 8 - Binary Search Trees.
Advertisements

22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Recursive Definitions and Structural Induction
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 3 CS 1813 – Discrete Mathematics Truth Inference and the Logical Way.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Department of Computer Science University of Maryland, College Park
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
1 Order Statistics on Binary Trees Goal: find the k th element (in order) of a binary tree where 1
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Induction and recursion
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
What it is? Why is it a legitimate proof method? How to use it?
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Induction and recursion
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.
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 4 Induction and Recursion 歐亞書局.
Chapter 4: Induction and Recursion
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
May 1, 2002Applied Discrete Mathematics Week 13: Graphs and Trees 1News CSEMS Scholarships for CS and Math students (US citizens only) $3,125 per year.
Multiway Trees. Trees with possibly more than two branches at each node are know as Multiway trees. 1. Orchards, Trees, and Binary Trees 2. Lexicographic.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Indexing and hashing Azita Keshmiri CS 157B. Basic concept An index for a file in a database system works the same way as the index in text book. For.
Theory of Computation, Feodor F. Dragan, Kent State University 1 TheoryofComputation Spring, 2015 (Feodor F. Dragan) Department of Computer Science Kent.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Marwan Al-Namari Hassan Al-Mathami. Indexing What is Indexing? Indexing is a mechanisms. Why we need to use Indexing? We used indexing to speed up access.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
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. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
1 2/21/2016 MATH 224 – Discrete Mathematics Sequences and Sums A sequence of the form ar 0, ar 1, ar 2, ar 3, ar 4, …, ar n, is called a geometric sequence.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction What it is? Why is it a legitimate proof method? How to use it?
Chapter 4: Induction and Recursion
Decision Trees DEFINITION: DECISION TREE A decision tree is a tree in which the internal nodes represent actions, the arcs represent outcomes of an action,
String is a synonym for the type [Char].
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Binary Search Tree (BST)
Induction and Recursion
CS 1813 – Discrete Mathematics
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Lecture 6 CS 1813 – Discrete Mathematics
Lecture 13 CS 1813 – Discrete Mathematics
Lecture 16 CS 1813 – Discrete Mathematics
Quizzes CS 1813 – Discrete Mathematics
Lecture 9 CS 1813 – Discrete Mathematics
Introduction to Finite Automata
Lecture 11 CS 1813 – Discrete Mathematics
Data Structures Using C++ 2E
Tutorial 2 - Daniel Razavi
Heaps.
Presentation transcript:

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 19 CS 1813 – Discrete Mathematics Trees and Inductive Definitions

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 2 What Is a Tree?  Tree  a diagram or graph that branches usually from a simple stem without forming loops or polygons Merriam-Webster  a type of data structure in which each element is attached to one or more elements directly beneath it — Webopedia  a node, together with a sequence of trees — inductive definition  Tree terminology  subtree — a node in a tree, together with its sequence of trees  root — the node that, with its subtrees, comprises the entire tree  interior node — a node with a nonempty sequence of subtrees  leaf — a node whose associated sequence of subtrees is empty  branch — a line connecting a node to its subtrees (in tree diagram)  binary tree — a tree with no nodes having more than 2 subtrees

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 3 What Are Trees Good For?  Computing applications that use trees  Databases, parsing, games, expert systems, word processors, operating systems, …  Search Trees — a common use of trees  Structures to make data quickly retrievable  Each node stores a key for retrieval and a package of data associated with the key  Keys are from a datatype that has an ordering  Subtrees are arranged to narrow the search quickly  Naïve search — just look sequentially through the pile Retrieval time proportional to number of items  Tree search — log(n) retrieval time, n = number items

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 4 Binary Search Tree – diagram form  Each node stores key and data  Left subtree contains all smaller keys  Right subtree contains all larger keys  Leafs just mark tree boundaries No data in a leaf 5120 PDA Cam 9605 Palm Pilot 4403 HotSync 4878 Palm Games 7268 Zip Drive 8444 Audio System 6876 Intellimouse 3663 Net Hub MB RAM K Modem 2088 LaserJet 1143 InkJet Key – ordered Data – anything To find an item start at root look left if smaller right if bigger 12 items: 4 steps, max

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 5 Binary Search Tree a formal representation data SearchTree key dat = Nub | Cel key dat (SearchTree key dat) (SearchTree key dat)  Type parameters — key, dat key — might be Int, for example (datatype with an ordering) dat — could be any type, typically a tuple  Example  s :: SearchTree Int (String, Float, [String] )  s is a SearchTree  key type – Int (maybe a catalog order number)  dat type – tuple storing a String (product description), a Float (price), and a sequence of strings (inventory records) Key goes hereNode data Left subtree (smaller keys) Right subtree (larger keys) Nub – leaf constructor Cel – constructor, non-empty trees

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 6 Retrieving Data from a Search Tree  Found or Not Found  Key sought may or may not be in search tree  What to do if it’s not there Deliver some sort of not-found indicator Use Maybe datatype for this data Maybe a = Just a | Nothing getItem :: Ord key => SearchTree key dat -> key -> Maybe (key, dat) getItem (Cel key dat smaller bigger) searchKey= if searchKey  key then (getItem smaller searchKey) else if searchKey  key then (getItem bigger searchKey) else (Just(key, dat)) getItem Nub searchKey = Nothing Not-Found IndicatorExample, item found Just (2088, “LaserJet”)

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 7 Tree Induction — another proof method  Definitions  Subtree s, t :: SearchTree key dat s  t  (s = t)  (  k,d,left,right. (t = Cel k d left right))  ((s  left)  (s  right))  Proper Subtree s, t :: SearchTree key dat s  t  (  k,d,left,right. (t = Cel k d left right))  ((s  left)  (s  right)) Equivalent Definition: s  t  s  t  s  t  Tree induction  P — predicate parameterized over SearchTrees P(t) is a proposition whenever t :: SearchTree key dat  Prove:  t. (  s  t. P(s))  P(t)  Conclude:  t. P(t)  Note: {s | s  Nub} = , so must prove P(Nub) directly (  s  Nub. P(s))  P(Nub) is equivalent to (True  P(Nub))

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 8 Properties of Trees  Definition “occurs in”  s :: SearchTree key dat, k :: key, d :: dat  k occurs in s — that is, k  s k  s  (  x,d,left,right. s  (Cel x d left right))  (k  x  k  left  k  right)  Definition “ordered” ordered s   (Cel k d left right)  s. (x  left  x  k)  (x  right  x  k)  Theorem: ordered trees have no duplicate keys P(s)  ((ordered s)  (x  s)  (s  (Cel k d left right)))  (x  k  (x  left  x  right))  (x  left  (x  k  x  right))  (x  right  (x  k  x  left)) Proof base case: s = Nub P(s) is True because the hypothesis of P(s) is False, since it cannot be the case that Nub  (Cel k d left right) and a thing constructed by Nub cannot be the same as a thing constructed by Cel inductive case: next slide

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 9 Ordered Trees Have No Duplicate Keys inductive case  Theorem: ordered trees have no duplicate keys P(s)  ((ordered s)  (x  s)  (s  (Cel k d left right)))  ((x  k  (x  left  x  right))  {1} (x  left  (x  k  x  right))  {2} (x  right  (x  k  x  left))) {3} Note: ordered s   (Cel k d left right)  s. (x  left  x  k)  (x  right  x  k) Proof inductive case: s = (Cel k d left right) 1. x  k   (x  k)   (x  k) {property of  and  }  x  left  x  right {ordered, contrapositive} 2. x  left  x  k   (x  k) {ordered, arithmetic}  x  k  x  right {arithmetic, ordered} 3. x  right  x  k   (x  k) {ordered, arithmetic}  x  k  x  left {arithmetic, ordered}  Conclusion:  s. P(s)

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 10 Proving That getItem Works  Theorem (getItem) ( (ordered s)  k  s )  getItem s k = Just (k, d)  Proof  P(s)  ((ordered s)  k  s)  getItem s k = Just (k, d)  Base case P(Nub)  ((ordered Nub)  k  Nub)  getItem Nub k = Just(k, d) The implication is true because its hypothesis is always false k  Nub would require that Nub = Cel x a left right A thing constructed by Nub cannot be the same as a thing constructed by Cel  Inductive case – next slide

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 11 getItem Proof — inductive case getItem :: Ord key => SearchTree key dat -> key -> Maybe (key, dat) getItem (Cel key dat smaller bigger) searchKey = if searchKey  key then (getItem smaller searchKey) else if searchKey  key then (getItem bigger searchKey) else (Just(key, dat)) getItem Nub searchKey = Nothing P(s)  ((ordered s)  k  s)   d. getItem s k = Just(k, d) Inductive Case: P(s), s  Nub  (ordered s)  k  s s = Cel x a left right s  Nub Case 1. x = k getItem s k = Just(k, a) x = k   (k  x)   (k  x) Case 2. k  x k  left s is ordered  d. getItem left k = Just(k, d) induction hyp: left  s  P(left) getItem s k = getItem left k searchKey  key branch of if-then getItem s k = Just(k, d) both sides = (getItem left k) Case 3. k  x — like Case 2, but use P(right)

CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 12 End of Lecture