Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 1
Lecture 36
Log into Linux. Copy files on csserver from
/home/hwang/cs215/lecture36/*.* into a subdirectory.
Reminder: Homework 8 due on Friday. Questions?
Lecture 36 Log into Linux. Copy files on csserver from - - PowerPoint PPT Presentation
Lecture 36 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture36/*.* into a subdirectory. Reminder: Homework 8 due on Friday. Questions? Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 1
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 1
Log into Linux. Copy files on csserver from
Reminder: Homework 8 due on Friday. Questions?
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 2
Review binary search trees Example class: Bag class
Attributes Reimplementation of operations
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 3
Review: A BST is defined as a binary tree with
For each node, the data values in the left subtree
For each node, the data values in the right subtree
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 4
To find an item, start at root node. Compare
To insert an item, start at a root node. Use the
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 5
To erase an item, start at root node. Using the
Node is a leaf. Delete node and set child pointer in
Node has one child. Set the child pointer is parent
Node has two children. Find the immediate
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 6
Starting with an empty BST, draw the tree that
Continuing with this tree, draw the result of
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 7
This bag class will be implemented using a
rootPtr – a pointer to the head node of the linked
Note that the textbook chooses to compute the
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 8
Here is a way to think about how this works.
Bag b; b.Insert(15); b.Insert(34); rootPtr 34 15
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 9
Note that today's bintree.h file is enhanced
Note that since BTNode is a template, we do
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 10
Examine the Bag template class definition in file
Has the same typedefs as the other Bag
Otherwise operation prototypes are the exactly
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 11
Examine file bag6test.cpp This is an example of an interactive test
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 12
Examine the rest of file bag6.h In addition to the Bag class operation
As before, we will ignore any bad_alloc
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 13
The default constructor simply sets rootPtr to
The copy constructor simply uses TreeCopy to
The destructor simply uses TreeClear to
The assignment operator= makes a self-
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 14
Since the storage representation is a BST, we
Since the left subtree contains items less than
Complete the Count function in bag6.h
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 15
Special case: empty tree
Create a node with the entry and make it the root
Otherwise, want to find the place the entry
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 16
Basic find algorithm
Complete the Insert function in bag6.h
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 17
The erasing algorithm for BSTs can be
The textbook presents an indirect method using
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 18
This function takes advantage of the fact that
As usual, we handle the empty tree first and
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 19
Otherwise, if the target is less than or greater
When the target is found, there are two cases:
If there is no left child, we can just make the right
If there is a left child, we need to replace the root
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 20
BSTRemoveMax receives a root pointer and
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 21
There are two cases to consider
The node has no right child, which means we have
The node has a right child, so the maximum is still
Complete the BSTRemoveMax function in
Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 22
Erase is similar to EraseOne, except that it
It uses BSTRemoveAll, which is left as a bonus