Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 19 CS 1813 – Discrete Mathematics Trees and Inductive Definitions."— Presentation transcript:

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

2 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

3 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

4 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 6419 128MB RAM 1425 56K 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

5 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

6 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”)

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

8 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

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

10 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

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

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


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

Similar presentations


Ads by Google