CSS 342 Data Structures, Algorithms, and Discrete Mathematics I

Slides:



Advertisements
Similar presentations
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
CSS342: Queues1 Professor: Munehiro Fukuda. CSS342: Queues2 Topics Basic concepts of queue Queue implementation Queues used in –Applications –Operating.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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.
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.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Trees, Binary Trees, and Binary Search Trees COMP171.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CUSACK CHAPT 4.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CSS430 Process Management
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CSS342: Propositions1 Professor: Munehiro Fukuda.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CHAPTER 13, 14.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
CSE373: Data Structures & Algorithms
Chapter 12 – Data Structures
Data Structure and Algorithms
Data Structure #1: The Array
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Chapter 16 Tree Implementations
ITEC 2620M Introduction to Data Structures
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CMSC 341 Lecture 10 Binary Search Trees
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Lecture 6: Uniprocessor Scheduling(cont.)
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
CMSC 341 Binary Search Trees.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Pointers & Dynamic Data Structures
CMSC 341 Binary Search Trees.
Life is Full of Alternatives
CS 367 – Introduction to Data Structures
Chapter 3: Processes CSS503 Systems Programming
Presentation transcript:

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 17. 161201. Carrano Chapt. 15 (partial); CUSACK CHAPT 4.

Agenda Queues HW5: Questions / Discussion Peer Design Review How will we grade this? Signatures of BST Peer Design Review Trees: BST If time, prop logic

Queues

A Pointer-Based Implementation 2 4 1 7 NULL front back

class MyQueue { public: MyQueue(); ~MyQueue(); bool isEmpty() const; int getCount() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: struct Node int val; Node *next = NULL; }; Node *front; Node *back; int count;

bool MyQueue::isEmpty() const { if (front == NULL) return true; } else return false; int MyQueue::getCount() const return count; MyQueue::MyQueue() { front = NULL; back = NULL; count = 0; } MyQueue::~MyQueue() { while (getCount() > 0) Dequeue(); }

void MyQueue::Enqueue(const int &value) { Node *insNode; insNode = new Node; insNode->val = value; if (count == 0) back = insNode; front = insNode; } else back->next = insNode; count++; int MyQueue::Dequeue() throw (exception) { if (count == 0) throw exception("Queue is empty"); } Node *temp = front; int retVal = temp->val; front = front->next; delete temp; if (count == 1) back == NULL; count--; return retVal;

Are we done? Do we need to overload =, copy constructor? Why or why not? Any other overloads?

Queue usage in OS, Network, Services Message queues Scheduling queue TCP Flow control Payment processing

Unix Message Queues 1 2 Message queue (id = msgid) struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); strcpy( message_body.mtext, “hello world\n” ); msgsnd( msgid, &message_body, 512, 0 ); } struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); msgrcv( msgid, &message_body, 512, 0, 0 ); cout << message_body.mtext << endl; } Message queue (id = msgid) 1 2 Some other process can enqueue and dequeue a message

Multilevel Queue Scheduling Each queue has its own scheduling algorithm, foreground (interactive) – RR, 80% CPU time background (batch) – FCFS, 20% CPU time

Multilevel Feedback-Queue Scheduling A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

Flow Control X X X X Sending application Receiving application packet Send Socket Buffer Receive Socket Buffer send ack read blocked packet X packet packet retransmitted packet dropped Application is slow to read. X packet packet retransmitted packet dropped X X write blocked

Lab 5 PEER DESIGN REVIEW

Grading Lab5 Copy all *.cpp and *.h files into a directory Copy BankTransIn.txt into directory Open up a command line window for compilation cl *.cpp name.exe > BankTrans.out 2> BankTrans.err

Trees

FIGURE 15-1 (a) A tree; (b) a subtree of the tree in part a Terminology Root Parent Child Ancestor Descendent Height Subtree N-ary tree Binary Tree FIGURE 15-1 (a) A tree; (b) a subtree of the tree in part a

Binary Tree: Algebraic Expressions.

Binary Search Tree Well suited for searching 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

Binary Search Tree: Example

Binary Search Tree: Example FIGURE 15-14 Binary search trees with the same data as in Figure 15-13

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

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

Computer Scientist of the Week J. Alex Halderman Professor at University of Michigan Expert on Computer security and privacy Focus on issues which impact public policy and society Coursera: Securing Digital Democracy 2016: Advised for a recount in WI, MI Explanation Popular science top-10 brightest mind: Popular Science

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;

Searching a Binary Search Tree Data Structures and Poblem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a Binary Search Tree: pseudo-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)

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

Efficiency of BST Operations Retrieve: Insert: Removal: Traversal:

Class Bell

Propositional Logic

Text Book An Active Introduction to Discrete Mathematics and Algorithms (version 2.6), Charles Cusack, David Santos, GNU Free Software, 2016: http://www.cs.hope.edu/~cusack/Notes/Notes/Books/Active%20Introduction%20to%20Discrete%20M athematics%20and%20Algorithms/ActiveIntroToDiscreteMathAndAlgorithms.2.6.pdf Chapter 4: Logic.

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.

Propositions Math History Programming languages 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 Alejandro Inarritu won an Academy Award in 1999 for directing “Amores perros”: (false, he won in 2015 for Birdman) 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

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 T p q p v q F T

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 !p F T p q p q F T

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 condition proposition a < 1 1 <= a <= 100 100 < a a >= 1 a <= 100 false true condition proposition

Theorems for Boolean algebra 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 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

Precedence of Operators NOT: ¬ AND: ^ XOR: OR: v Conditional: → iff: ↔

Prove the following Boolean equations ¬p ^ q v p ^ ¬q = (¬p v ¬q) ^ (p v q) p ^ q v p ^ q ^ r v p ^ ¬ q = p (p ^ q v r ) ^ q = p ^ q ^ ¬r v ¬p ^ q ^ r v p ^ q ^ r