- Problem Statement
- Options
- Trees
- Definitions
- Data Structure
- Anatomy
Problem Statement Options Trees Definitions Data Structure - - PowerPoint PPT Presentation
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
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:
- Quickly search to determine whether a specific Pokémon is already in the
collection.
- Print all the Pokémon in our Pokédex in order by number.
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
________________
- An example of a binary tree that contains this sequence of numbers:
[1, 3, 4, 5, 6, 7, 9]. Binary Tree
5 7 3 6 1 9 4 5 7 3 6 1 9 4
Binary Tree Definition
- A ____________ is one of the following:
- An _____________, or
- A _________________, which has the following parts:
- a ________________
- a ________________, which is a binary tree
- a ________________, which is a binary tree
- Does this remind you or another data structure we have discussed?
- ___________________________
Binary Tree empty tree
non-empty tree
data value
left sub-tree
right sub-tree LinkedNode (Linked –List)
Balance
- When discussing binary trees there is a notion of _________
- Using the tree analogy, a ____________ tree is often referred to as
__________ in shape
- This means all the __________ in the tree are of approximately the
same _________ and are ____________
- The original example is a
___________ balanced tree balance balanced bushy branches length mostly full perfectly
5 7 3 6 1 9 4 5 7 3 6 1 9 4
Linked Node Review
- What are the parts of a Node in a linked list?
- ________ value
- _______________ to another Node
- In code this looks like: In a Diagram it looks like:
struct node { int data; struct node* p_next; };
data reference
1 2 X
Activity
Using the linked lists node struct as a base, define a struct to represent a binary tree. What is the empty tree? struct tree_node { int value; struct tree_node* left; struct tree_node* right; }; Your one freebie*
*Writing the starter code was really messy without this
See bst.h
Creating a Tree
Write a function that creates a tree. It should take in the label and subtrees (or NULL) as parameters. It should return a pointer to the new tree. // Convenience typedef typedef struct tree_node* tree; tree bt_make (tree left, int value, tree right) { tree t; return t; } Starter code in bst.c
Anatomy of a Tree
5 7 3 6 1 9 4 5 7 3 6 1 9 4
___________ Node : has no parents _______________ Node : has a subtree _____________________ Node : has a parent and child _______________ Node: has a parent ________________ Node: has no children
Root Parent Internal Child Leaf
Activity (Group)
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 H R C W S R U H R C W S R
Remember, characters are just numbers so we can assign them to an int Starter code in westeros.c
- Definitions
- Inserting/Adding
- Searching
- Traversal
Searching
- Since our Binary trees have no order, searching for an item in them
requires visiting ____________ until we find it
- We can place some constraints on the tree to speed this up
- A ________________ Tree is a specialized binary tree with these
constraints
- If the BST is a _______________, then
- the ________ sub-tree of BST is a binary search tree and all data values of
the left sub-tree of BST are ____________ than the data value of BST
- the ________ sub-tree of BST is a binary search tree and all data values of
the right sub-tree of BST are ____________ than the data value of BST
every node Binary Search non-empty tree
left less right greater
BST?
Which of the following trees are Binary Search Trees
5 7 4 1 2 6 3 5 7 4 1 2 6 3 4 5 1 6 2 7 3 4 5 1 6 2 7 3 4 6 2 5 1 7 3 4 6 2 5 1 7 3 1 3 5 2 4 7 6 1 3 5 2 4 7 6
2 is greater than 1 but on its left 5 is greater than 3 but in its left subtree
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:
- Start at the _______ node
- Determine if the value to be added is less than or greater than the
current node's value
- If less move to the ________ child
- If greater move to the ________ child
- Continue until there is _____________ for the desired child
- Create a new ___________ with the value to be added
- Connect the new node to its ____________
root left right no subtree leaf node parent
Activity
Draw a binary search tree by inserting the above numbers from left to right. 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
11 6 19 4 17 43 31 49 8 5 10
Select Your Pokémon
Choose 6 of the Pokémon on the right and build a BST by inserting each into the tree using its number in the same
- rder that they appear in the
table of available Pokémon.
001 094 002 003 004 005 006 007 008 009 013 025 026 039 040 172 174 050 051 052 053 063 064 065 074 075 076 129 130 143 150 151 132 133 446 134 135 146 145 144 136 149 148 147 196 197 470 471 700 019 020 054 055 113 242 173 035 036 092 093
Feel free to pick your favorite six
Insert
Write the C code to insert a new value into a BST. tree insert (int value, tree bst) { return tree; } Starter code in bst.c
Insert Complexity
- Using your code, what is the time complexity for adding a new value to
a balanced BST?
- O(_____)
- What is the time complexity for adding a new value to an unbalanced
BST?
- O(_____)
logN N
Make a Pokédex
Write the code to build your tree of Pokémon using the BST struct and your insert function. Insert the Pokémon in the same order as before. tree pokedex = NULL; Starter code in pokemon.c
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:
- Start at the _______ node
- Determine if the searched value ____________ the current node's value
- If it is, __________
- Else determine if it is less than or greater than the current node's value
- If less move to the ________ child
- If greater move to the ________ child
- Continue until the value is _________ or
- Until there is _____________ for the desired child
root is equal to found it left right found no subtree
Searching a BST
Given the tree on the right, determine whether 10 and 18 are in that tree (fill in the table for each) 11 6 19 4 17 43 31 49 8 5 10
11 6 8 10 L R R 11 19 17 X R L R 10 18
Coding Search
Write the Python code to search for a given value in a BST. Return True if the value is present in the tree, and False if it is not. bool search (int value, tree bst): } Starter code in bst.c
Search Complexity
- Using your code, what is the time complexity for searching for a value
in a balanced BST?
- O(_____)
- What is the time complexity for searching for a value in an unbalanced
BST?
- O(_____)
logN N
Do I Have That One?
Write the code to search for two Pokémon: bool magicarp_in_dex = search (129, pokedex);
129
Starter code in pokemon.c
007
One that is in your Pokedex One that isn't in your Pokedex
Printing the Pokédex
- Now that we have our Pokédex we'll want the ability to print it out, in
- rder
- This is known as ___________ the tree
- For a BST use an ________ traversal to get an ordered listing
- The order for an infix traversal is:
- Add/print the node's __________ subtree
- Add/print the node's __________
- Add/print the node's __________ subtree
traversing infix
left value right
Infix Traversals
Given the following trees, write the infix order string for each. 2, 1, 3, 4, 6, 5, 7 1, 2, 3, 4, 5, 6, 7 1, 2, 4, 5, 6, 3, 7
5 7 4 1 2 6 3 5 7 4 1 2 6 3 4 5 1 6 2 7 3 4 5 1 6 2 7 3 1 3 5 2 4 7 6 1 3 5 2 4 7 6
On Your Own
Perform an infix traversal on the tree below. Write the value of each node as it is visited.
d l t e n 2 H l
- 5
S u e t