Lecture 16 150309 CUSACK CHAPT 4 Agenda HW5 Questions Discussion Queues finish Trees BST Prop Logic Intro Will be on final HW5 What should occupy the Queue Where should History live ID: 760843
Download Presentation The PPT/PDF document "CSS 342 Data Structures, Algorithms, an..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CSS 342
Data
Structures, Algorithms, and Discrete Mathematics I
Lecture 16. 150309.
CUSACK CHAPT 4.
Slide2Agenda
HW5: Questions / Discussion
Queues: finish.
Trees: BST
Prop Logic Intro. Will be on final.
Slide3HW5
What should occupy the Queue?
Where should History live?
What should main look like?
Slide4A Pointer-Based Implementation
2
4
1
7
front
back
NULL
Slide5Queues with a linked list?
Do we need to overload =, copy constructor?
Why or why not?
Any other overloads?
Slide6Trees
Slide7Terminology
FIGURE 15-1 (a) A tree; (b) a subtree of the tree in part a
Root
Parent
Child
Ancestor
Descendent
Height
Subtree
N-
ary
tree
Binary Tree
Slide8Binary Tree: Algebraic Expressions.
Slide9Binary Search Tree
For each node n, a binary search tree satisfies the following three properties:n ’s value is greater than all values in its left subtree T L .n ’s value is less than all values in its right subtree T R .Both T L and T R are binary search trees.Well suited for searching
Data Structures and Problem Solving with C++: Walls and Mirrors,
Carrano
and Henry, © 2013
Slide10Binary Search Tree: Example
Slide11Binary Search Tree: Example
FIGURE 15-14 Binary search trees with the same data as in Figure 15-13
Slide12Binary Search Tree: Example
FIGURE 15-14 Binary search trees with the same data as in Figure 15-13
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Slide13Binary Search Tree: Example
FIGURE 15-14 Binary search trees with the same data as in Figure 15-13
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Slide14In-class problem
The maximum number of leaves in a tree can be expressed by the following formula: h = height of the tree b = max number of branches per node. Write a recursive function which computes.
class
BinarySearchTree
{
public
:
BinarySearchTree
();
~
BinarySearchTree
();
void
Insert(Object *);
bool
Remove(
const
Object &);
bool
Retrieve(
const
Object &, Object * &);
void
Flush();
bool
Contains(
const
Object
&);
void
Display()
const
;
bool
isEmpty
()
const
;
int
Height()
const
;
int
getCount
()
const
;
private
:
struct
Node
{
Object *
pObj
;
Node
*right;
Node
*left;
};
Node
*root;
};
Slide16Computer Scientist of the week
George Boole
English Mathematician, Philosopher and LogicianWrote: “An Investigation of the Laws of Thought (1854), on Which are Founded the Mathematical Theories of Logic and Probabilities”Found of Boolean Algebra, the Algebra of logicLogical propositions expressed as algebrasbool keyword named after himDied by the fever started by walking to class in the rain to lecture
Slide17Searching a Binary Search Tree
Data Structures and
Poblem
Solving with C++: Walls and Mirrors,
Carrano
and Henry, © 2013
Slide18Searching a Binary Search Tree: psudeo-code
Search(
Obj
*root)
{
if
(root ==
NULL)
return
NULL
;
else
if
(target == *(root->
pObj
))
{
return
(root->
pItem
);
}
else
if
(target < *(root->
pObj
))
{
return
Search(root->left);
}
else
{
return
Search(root->right)
}
}
Slide19Creating a Binary Search Tree
FIGURE 15-16 Empty subtree where the search algorithm terminates when looking for Frank
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Slide20Efficiency of BST Operations
Retrieve:
Insert:
Removal:
Traversal
:
Slide21Propositional Logic
Slide22Text Book
An Active Introduction to Discrete Mathematics and Algorithms,
Charles Cusack, David Santos, GNU Free
Software,
2014.
http://www.cs.hope.edu/~cusack/Notes/Notes/Books/Active%20Introduction%20to%20Discrete%20
Mathematics%20and%20Algorithms/ActiveIntroToDiscreteMathAndAlgorithms.pdf
Chapter 4: Logic.
Slide23Propositions
Proposition
:
a statement that is either true or false, but not both
Examples of propositions
Proposition p
:
All
mathematicians wear sandals
.
Proposition r:
Discrete Math is fun
.
Proposition v:
This is not a proposition.
Proposition s:
The sum of the first n odd integer is equal to n
2
for n >1.
These are not propositions:
What is your name?
Go to the store and get me a steak?
Propositions have a truth value: True or False.
Slide24Propositions
MathThe only positive integers that divide 7 are 1 and 7 itself: (true)For every positive integer n, there is a prime number larger than n: (true)HistoryAlfred Hitchcock won an Academy Award in 1940 for directing “Rebecca”: (false, he has never won one for directing)Seattle held the World’s Fair, Expo 62: (true )Programming languagesBoolean expressions in if-else, while, and for statementsfor ( index = 0; index < 100; index++ ) { …….;}
A proposition
Not a proposition
Slide25Compound propositions
Conjunction (AND) of p and qnotations: p ^ q, p && qTrue only if both p and q are trueTruth tableDisjunction (OR) of p or qNotations: p v q, p || qTrue if either p or q or both are truetruth table
pqp ^ qFFFFTFTFFTTT
p
q
p v q
F
F
F
F
T
T
T
F
T
T
T
T
Slide26Compound Propositions
The negation (NOT) of pNotations: not p, ¬p !pExamplesP: 1 + 1 = 3 (false)!p: !(1 + 1 = 3) ≡ 1 + 1 ≠ 3 (true)Exclusive OR (XOR)Notations: p xor q,
pqp qFFFFTTTFTTTF
p
!p
F
T
F
T
Slide27Binary Expressions in C++
How do you examine the behavior of if-else?if ( a > = 1 && a <= 100 )true if 1 ≤ a ≤ 100if ( a >= 1 || a <= 100 )always true
a < 1
1 <= a <= 100
100 < a
a >= 1
a <= 100
false
true
true
true
true
false
condition
proposition
a < 1
1 <= a <= 100
100 < a
a >= 1
a <= 100
false
true
true
true
true
false
condition
proposition
Slide28Commutative Lawp v q = q v pp ^ q = q ^ p Associative Lawp v (q v r) = (p v q) v rp ^ (q ^ r) = (p ^ q) ^ r Distributive Lawp ^ (q v r) = (p ^ q) v (p ^ r)p v (q ^ r) = (p v q) ^ (p v r) Identityp v F = pP ^ T = pDominationp v T = Tp ^ F = F
Theorems for Boolean algebra
Slide29Complement Law (tautologies)p ^ ¬p = Fp v ¬p = T Square Lawp ^ p = pp v p = p Double Negation¬(¬p) = p Absorptionp ^ (p v q) = p p v (p ^ q) = p
Theorems for Boolean algebra
Slide30Precedence of Operators
NOT: ¬
AND: ^
XOR
OR: v
Conditional: ->
Slide31Prove the following Boolean equation
¬p
^
q
v
p
^
¬
q
= (¬
p
v
¬
q
) ^ (
p
v
q
)