problem statement options trees definitions data
play

Problem Statement Options Trees Definitions Data Structure - PowerPoint PPT Presentation

Problem Statement Options Trees Definitions Data Structure Anatomy Problem Problem Statement: Gotta Catch 'em All! Pokmon trainers use a Pokdex to keep track of the Pokmon that they have captured. When


  1. • Problem Statement • Options • Trees • Definitions • Data Structure • Anatomy

  2. Problem ● Problem Statement: Gotta Catch 'em All! Pokémon trainers use a Pokédex to keep track of the Pokémon that they have captured. When encountering a Pokémon in the wild, it is important for a trainer to know whether they have already captured that species. We need a data structure that meets the following requirements: o Quickly search to determine whether a specific Pokémon is already in the collection. o Print all the Pokémon in our Pokédex in order by number.

  3. Introduction to Binary Trees ● We need something faster than an array and sortable (unlike a map) ● We're going to create a new data structure called a Binary Tree ________________ ● An example of a binary tree that contains this sequence of numbers: [1, 3, 4, 5, 6, 7, 9]. 5 5 3 3 7 7 4 4 6 6 1 1 9 9

  4. Binary Tree Definition Binary Tree ● A ____________ is one of the following: empty tree o An _____________, or non-empty tree o A _________________, which has the following parts: data value  a ________________ left sub-tree  a ________________, which is a binary tree right sub-tree  a ________________, which is a binary tree ● Does this remind you or another data structure we have discussed? LinkedNode (Linked –List) ● ___________________________

  5. Balance balance ● When discussing binary trees there is a notion of _________ balanced ● Using the tree analogy, a ____________ tree is often referred to as bushy __________ in shape branches ● This means all the __________ in the tree are of approximately the mostly full length same _________ and are ____________ ● The original example is a 5 5 perfectly ___________ balanced tree 3 3 7 7 4 4 6 6 9 9 1 1

  6. Linked Node Review ● What are the parts of a Node in a linked list? data ● ________ value reference ● _______________ to another Node ● In code this looks like: In a Diagram it looks like: struct node { 1 2 X int data; struct node* p_next; };

  7. Activity See bst.h Using the linked lists node struct as struct tree_node a base, define a struct to represent { a binary tree. What is the empty int value; tree? struct tree_node* left; struct tree_node* right; }; Your one freebie* *Writing the starter code was really messy without this

  8. Creating a Tree Starter code in bst.c Write a function that // Convenience typedef creates a tree. It typedef struct tree_node* tree; should take in the label and subtrees (or tree bt_make (tree left, int value, tree right) NULL) as parameters. { It should return a tree t; pointer to the new tree. return t; }

  9. Anatomy of a Tree Root ___________ Node : has no parents 5 5 Parent _______________ Node : has a subtree Internal _____________________ Node : 3 3 7 7 has a parent and child 4 4 6 6 9 9 1 1 Leaf Child ________________ Node: _______________ Node: has no children has a parent

  10. Activity (Group) Starter code in westeros.c Use the bt_make function to create a balanced tree containing a label for the capitals of the seven great houses of Westeros ((W)interfell, The (E)yrie, (R)iverrun, (C)asterly Rock, (S)torm's End, (H)ighgarden, S(U)nspear). U U Remember, characters are just numbers so we R R H H can assign them to an int R R C C W W S S

  11. • Definitions • Inserting/Adding • Searching • Traversal

  12. Searching ● Since our Binary trees have no order, searching for an item in them every node requires visiting ____________ until we find it ● We can place some constraints on the tree to speed this up Binary Search ● A ________________ Tree is a specialized binary tree with these constraints non-empty tree ● If the BST is a _______________, then left o the ________ sub-tree of BST is a binary search tree and all data values of less the left sub-tree of BST are ____________ than the data value of BST right o the ________ sub-tree of BST is a binary search tree and all data values of greater the right sub-tree of BST are ____________ than the data value of BST

  13. BST? Which of the following trees are Binary Search Trees 4 4 5 5 4 4 1 1 2 2 6 6 4 4 7 7 1 1 5 5 3 3 3 3 5 5 3 3 6 6 1 1 7 7 2 2 2 2 7 7 7 7 6 6 2 2 5 5 1 1 3 3 2 is greater than 1 but on its left 5 is greater than 3 but in its left subtree 4 4 6 6

  14. Capturing Pokémon ● Returning to our problem statement, we need a way to add captured Pokémon to our Pokédex ● When adding an element to a BST: root o Start at the _______ node o Determine if the value to be added is less than or greater than the current node's value left o If less move to the ________ child right o If greater move to the ________ child no subtree o Continue until there is _____________ for the desired child leaf node o Create a new ___________ with the value to be added parent o Connect the new node to its ____________

  15. Activity Draw a binary search tree by inserting the above numbers from left to right. 11 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 6 19 17 8 4 43 49 5 10 31

  16. Select Your Pokémon Choose 6 of the Pokémon on the right and build a BST by 147 146 020 039 004 075 129 008 026 036 inserting each into the tree using its number in the same 093 196 151 001 145 174 003 132 050 019 order that they appear in the table of available Pokémon. 054 130 470 009 051 144 052 006 076 094 197 035 143 134 055 074 025 136 002 053 065 005 113 149 172 064 133 446 471 173 092 242 150 007 148 063 135 700 013 040 Feel free to pick your favorite six

  17. Insert Starter code in bst.c Write the C code to tree insert (int value, tree bst) { insert a new value into a BST. return tree; }

  18. Insert Complexity ● Using your code, what is the time complexity for adding a new value to a balanced BST? logN ● O(_____) ● What is the time complexity for adding a new value to an unbalanced BST? N ● O(_____)

  19. Make a Pokédex Starter code in pokemon.c Write the code to tree pokedex = NULL; build your tree of Pokémon using the BST struct and your insert function. Insert the Pokémon in the same order as before.

  20. Checking the Pokédex ● Now that we have some Pokémon in our Pokédex we'll want to search it when we find a wild Pokémon to see if we already have one ● When searching a BST: root o Start at the _______ node is equal to o Determine if the searched value ____________ the current node's value found it o If it is, __________ o Else determine if it is less than or greater than the current node's value left o If less move to the ________ child right o If greater move to the ________ child found o Continue until the value is _________ or no subtree o Until there is _____________ for the desired child

  21. Searching a BST Given the tree on the right, 11 determine whether 10 and 18 are in that tree (fill in the table for 6 19 each) 17 8 4 43 49 5 10 31 10 18 11 6 8 10 11 19 17 X L R R R L R

  22. Coding Search Starter code in bst.c Write the Python code to search bool search (int value, tree bst): for a given value in a BST. Return True if the value is present in the tree, and False if it is not. }

  23. Search Complexity ● Using your code, what is the time complexity for searching for a value in a balanced BST? logN ● O(_____) ● What is the time complexity for searching for a value in an unbalanced BST? N ● O(_____)

  24. Do I Have That One? Starter code in pokemon.c Write the code to search for two bool magicarp_in_dex = Pokémon: search (129, pokedex); One that is in your Pokedex 129 One that isn't in your Pokedex 007

  25. Printing the Pokédex ● Now that we have our Pokédex we'll want the ability to print it out, in order traversing ● This is known as ___________ the tree infix ● For a BST use an ________ traversal to get an ordered listing ● The order for an infix traversal is: left o Add/print the node's __________ subtree value o Add/print the node's __________ right o Add/print the node's __________ subtree

  26. Infix Traversals Given the following trees, write 1 1 the infix order string for each. 5 5 3 3 4 4 4 4 7 7 1 1 5 5 7 7 2 2 2 2 6 6 5 5 3 3 6 6 7 7 2 2 1 1 3 3 4 4 6 6 1, 2, 3, 4, 5, 6, 7 1, 2, 4, 5, 6, 3, 7 2, 1, 3, 4, 6, 5, 7

  27. On Your Own You will know if you have Perform an infix traversal on the the correct answer tree below. Write the value of each node as it is visited. 0 d l n t 2 e l H S u 5 o e t

  28. Printing the BST Write the C code to implement a void print_infix (tree tree) infix traversal of a BST. What do { you do when the tree is empty? Strings are messy in C so just print the tree as you go rather than returning a string. }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend