CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

31
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Transcript of CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Page 1: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

CSS 342DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I

LECTURE 16. 150309.

CUSACK CHAPT 4.

Page 2: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Agenda• HW5: Questions / Discussion

• Queues: finish.

• Trees: BST

• Prop Logic Intro. Will be on final.

Page 3: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

HW5• What should occupy the Queue?

• Where should History live?

• What should main look like?

Page 4: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

A Pointer-Based Implementation

2 4 1 7

front back

NULL

Page 5: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Queues with a linked list?• Do we need to overload =, copy constructor?

• Why or why not?

• Any other overloads?

Page 6: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Trees

Page 7: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Terminology

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

Page 8: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Binary Tree: Algebraic Expressions.

Page 9: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Binary 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

Page 10: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Binary Search Tree: Example

Page 11: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Binary Search Tree: Example

FIGURE 15-14 BINARY SEARCH TREES WITH THE SAME DATA AS IN FIGURE 15-13

Page 12: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Binary 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

Page 13: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Binary 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

Page 14: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

In-class problem1. 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.

Page 15: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

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;

};

Page 16: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Computer Scientist of the weekGeorge Boole

• English Mathematician, Philosopher and Logician• Wrote: “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 logic• Logical propositions expressed as algebras

• bool keyword named after him• Died by the fever started by walking to class in the rain

to lecture

Page 17: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Searching a Binary Search Tree

Data Structures and Poblem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 18: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Searching a Binary Search Tree: psudeo-codeSearch(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)}

}

Page 19: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Creating 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

Page 20: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Efficiency of BST Operations• Retrieve:

• Insert:

• Removal:

• Traversal:

Page 21: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Propositional Logic

Page 22: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

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

Page 23: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Propositions• 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 n2 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.

Page 24: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Propositions• Math

• The 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)• History

• Alfred 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 languages

• Boolean expressions in if-else, while, and for statements• for ( index = 0; index < 100; index++ ) {

…….;}

A proposition

Not a proposition

Page 25: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Compound propositions•Conjunction (AND) of p and q

• notations: p ^ q, p && q• True only if both p and q are true• Truth table

•Disjunction (OR) of p or q• Notations: p v q, p || q• True if either p or q or both are true• truth table

p q p ^ q

F F F

F T F

T F F

T T T

p q p v q

F F F

F T T

T F T

T T T

Page 26: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Compound Propositions• The negation (NOT) of p

• Notations: not p, ¬p !p• Examples

• P: 1 + 1 = 3 (false)• !p: !(1 + 1 = 3) ≡ 1 + 1 ≠ 3 (true)

•Exclusive OR (XOR)• Notations: p xor q,

p q p q

F F F

F T T

T F T

T T F

p !p

F T

F T

Page 27: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Binary Expressions in C++ How do you examine the behavior of if-else?

◦ if ( a > = 1 && a <= 100 )◦ true if 1 ≤ a ≤ 100

◦ if ( a >= 1 || a <= 100 )◦ always true

a < 1 1 <= a <= 100 100 < a

a >= 1

a <= 100

false

true

true

true

true

false

conditionproposition

a < 1 1 <= a <= 100 100 < a

a >= 1

a <= 100

false

true

true

true

true

false

conditionproposition

Page 28: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

• Commutative Law• p v q = q v p• p ^ q = q ^ p

• Associative Law• p v (q v r) = (p v q) v r• p ^ (q ^ r) = (p ^ q) ^ r

• Distributive Law• p ^ (q v r) = (p ^ q) v (p ^ r)• p v (q ^ r) = (p v q) ^ (p v r)

• Identity• p v F = p• P ^ T = p

•Domination• p v T = T• p ^ F = F

Theorems for Boolean algebra

Page 29: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

• Complement Law (tautologies)• p ^ ¬p = F• p v ¬p = T

• Square Law• p ^ p = p• p v p = p

• Double Negation• ¬(¬p) = p

• Absorption• p ^ (p v q) = p • p v (p ^ q) = p

Theorems for Boolean algebra

Page 30: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Precedence of Operators• NOT: ¬

• AND: ^

• XOR

• OR: v

• Conditional: ->

Page 31: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 16. 150309. CUSACK CHAPT 4.

Prove the following Boolean equation

• ¬p ^ q v p ^ ¬q = (¬p v ¬q) ^ (p v q)