Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 element with this key return true; else return false Add(element) – if there is no element with this element’s key, add it to the map and return true; else return false Remove(key) – if there is an element with this key, remove it from the map and return true; else return false Retrieve(key) – if there is an element with this key, return the value; else return null

2 Many ways to store the elements of a map
Linked list Vector Binary search tree Balanced search tree Hash table

3 Assignments 5 and 6 Both require implementing a map (as defined in mapinterface.h) Assignment using a linked list Assignment using a binary search tree You do not know what the map is going to be used for What will a program that wants to use your map class need to do in order to use it?

4 If you were writing a program that needed to use a priority queue could you use the priority queue class you wrote for Assignment 4?

5 The priority Queue interface
Operation Description size() Return number of elements in the PQ isEmpty() Returns true if PQ is empty, else false enqueue(element) Add element to the PQ dequeue() Remove element with the highest priority front() Return element with the highest priority

6 Writing a c++ class that provides priority queue behavior
(optional) define a Priorityqueueinterface abstract class Define the class in pq.h typedef statement for itemtype Public section Method Prototype with comment describing behavior for each operation in the interface Default constructor Big 3 methods as needed Nothing else! Private section Data members depending on how items are being stored Implement the class in pq.cpp

7 Divide and conquer

8 2 ways to apply divide and conquer strategy when writing a program
Using classes that do pieces of what the program needs to do Container classes List, stack, queue, priority queue, map, graph Classes specific to the program Type of items – itemtoPurchase, call Shopping cart Call center Functional decomposition

9 Architecture of assignment 4
has-a program CallCenter has-a Priority Queue has-many Call

10 Which is better? main main int,double int, double getSysVar user
simulate terminal int, double user simulate terminal

11 Binary search trees

12 binary search tree A binary search tree is a binary tree that has the bst property Each item holds a value Its left child is either empty or holds a smaller value Its right child is either empty or holds a larger value Are recursive 45 36 56 42 25 52 60 30 50

13 Storing the elements of a bst
Have to store each element along with how that element is related to other elements Parent, left child, right child can we store the elements in a vector so we can calculate where an element’s parent, left child and right child are stored? Yes, but very inefficient for a binary search tree Linked storage stores each element in a node along with pointers to its children and (maybe) its parent Needs a pointer to the root (like head for a linked list)

14 45 36 56 25 42 52 60 30 50 root Each node holds a key-value pair, only the key is shown here

15 How do we find/retrieve an element?
45 36 56 25 42 52 60 30 50 root

16 How do we add an element? 45 36 56 25 42 52 60 30 50 root

17 An exercise Starting with an empty binary search tree
Draw the BST that results from adding (in the order shown) elements with the following keys Repeat with

18 52 / \ / \ \ / / \ 30 6 \ 8 / \ 25

19 operations all start by searching for element with given key
Find – follow path from root If found return true If not found return false Retrieve – follow path from root If found return value If not found return null add – follow path from root If found return false If not found add node at end of the path followed and return true remove – follow path from root If found delete that node (or another node) and return true

20 Searching a bst Search algorithm can be iterative or recursive
Zybook shows iterative search algorithm Will look at recursive search algorithm in class A bst is recursively defined

21 Outline for A recursive search function
bool search (nodePointer, key) if nodePointer is null // base case #1 else if element in this node has the key // base case #2 else if key < key of element in this node search left subtree else search right subtree

22 Implementing map methods recursively
find, add, retrieve and remove methods do not have a node* as a parameter Find, add, remove, retrieve need to call recursive helper methods These will have a node* as a parameter Each recursive call will search a smaller BST What happens when a base case is reached is different for each method

23 Recursive helper methods
Are called by map methods Need to be defined as private methods (in .h) Ex: Bool find(keytype key, node* p); Should the node* parameter be Pass by value? Pass by reference? Pass by const reference?

24 How do we remove an item? 45 36 56 25 42 52 60 30 50 root

25 Removing an element Start by searching for the element with the given key If element not found return false If element found Case 1: element is in a leaf node Change pointer from parent to null and delete node Case 2: element is in a node with one empty subtree Change pointer from parent to point to the non-empty subtree and delete node Case 3: element is in a node with no empty subtrees Find smallest item in the right subtree (or largest in the left subtree) Replace the element to be deleted with that element Delete the node containing that element It will have at least one empty subtree

26 45 36 56 25 42 52 60 30 50 root remove (50) remove (25) remove (45)

27 If you are storing the elements of a map in a linked list that is sorted based on the key values do you need to write a sort function or method?

28 Traversing a collection of items
“visit” each item in the collection once visit is some action to be taken with each item There is more than one order in which the items can be visited In what orders can the items in a list be visited? First to last Last to first Some operations that require traversing a collection of items Displaying all items Making a copy of a linked list (or BST) Destructor for a linked list (or bst)

29 Traversing a bst Three common orders in which to visit the items in a Bst Preorder Visit the root Traverse the left subtree Traverse the right subtree Postorder Inorder

30 45 36 56 42 25 52 60 30 50 preorder: postorder: inorder:

31 45 36 56 42 25 52 60 30 50 preorder: postorder: inorder:

32 Backing up a bst Searching a bst follows a path of successors
Do not need to go from a node to its parent Traversing a bst requires backing up the tree Go from a node to its parent 3 ways to do this Each node has a parent pointer Use recursion Keep a stack of node pointers that is used to back up the tree

33 Outline for a recursive traversal of a bst
if BST is empty return // base case – no tree to traverse visit the root traverse the left subtree traverse the right subtree what parameter(s) are required? what kind of traversal is done? how to do other traversals?

34 Outline for a recursive traversal of a bst
if BST is empty return // base case – no tree to traverse visit the root traverse the left subtree traverse the right subtree what parameter(s) are required? Node* p what kind of traversal is done? preorder how to do other traversals? move “visit the root”

35 Some reasons to traverse a bst
Display elements in the bst Traversal order? What does visit mean? Free all the nodes of a bst (destructor) Make a copy of a bst (copy constructor)

36 Some reasons to traverse a bst
Display elements in the bst Traversal order -- inorder (most likely) What does visit mean -- insert element into an istream Free all the nodes of a bst (destructor) Traversal order -- postorder What does visit mean -- delete a node Make a copy of a bst (copy constructor) Traversal order -- preorder What does visit mean -- allocate a node and store an element in it

37 Some map data structures
data structure add find remove retrieve array (unordered) O(n)* O(n) O(n) O(n) array (ordered by key) O(n) O(log2 n) O(n) O(log2 n) linked list (unordered) O(n) * O(n) O(n) O(n) linked list (ordered by key) O(n) O(n) O(n) O(n) binary search tree ?? ?? ?? ?? * key must be unique

38 Big o depends on length of search path followed
Length of search paths depends on height of the tree Height of the tree depends on order in which items were added Suppose we added the items in order of their keys? Height of a bst containing n items Worst case: Best case: Average case:

39 Big o depends on length of search path followed
Length of search paths depends on height of the tree Height of the tree depends on order in which items were added Suppose we added the items in order of their keys? Height of a bst containing n items Worst case: n-1 Best case: floor (log2 n) Average case: 1.5 (log2 n)

40 Why storing a bst in an array is too inefficient to be usble

41 A complete binary tree All levels except the bottom most have all possible nodes Nodes on the bottom most level fill the left most positions

42 A complete binary tree can be efficiently stored in an array

43 Allows computation of location of an element’s parent, left child and right child
left child of i is at 2 * i + 1 right child of i is at 2 * i + 2 parent of i is at (i – 1) / 2 How can we tell if there is no left or right child? How can we tell if there is no parent?

44 Requires memory space for all possible items
Level number possible number of items total possible number of items in a at this level binary tree with this many levels Level Level Level Level Level Level n n n+1 -1

45 What size array is needed? What size array is needed?
52 / \ / \ \ / / \ 30 6 \ 8 / \ 25

46 What size array is needed? What size array is needed?
52 / \ / \ \ / / \ 30 6 \ 8 / \ 25 15 to hold 5 actual elements 31 to hold 9 actual items

47 Zybook assignment before Tuesday’s class do ch.20 – hash tables

48 Exam 2 Thursday March 29 Memory management Hierarchical collections
Run-time stack Heap Big 3 methods (destructor, copy constructor, operator=) Hierarchical collections General Trees Priority queue adt implementations Recursion Set and map adts Binary search tree


Download ppt "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."

Similar presentations


Ads by Google