Lecture 36 Log into Linux. Copy files on csserver from - - PowerPoint PPT Presentation

lecture 36
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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?

slide-2
SLIDE 2

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 2

Outline

 Review binary search trees  Example class: Bag class

 Attributes  Reimplementation of operations

slide-3
SLIDE 3

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 3

Binary Search Trees

 Review: A BST is defined as a binary tree with

the following ordering:

 For each node, the data values in the left subtree

are less than or equal to the node value.

 For each node, the data values in the right subtree

are greater than the node value.

slide-4
SLIDE 4

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 4

Binary Search Trees

 To find an item, start at root node. Compare

item to node value. If equal, then done. Otherwise, go down the appropriate subtree and repeat. If reach null, item is not present.

 To insert an item, start at a root node. Use the

same algorithm as finding an item also maintaining a parent pointer that points to the parent of the node being examined. When reach a null pointer, create a new node with item and attach it to the parent on that side.

slide-5
SLIDE 5

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 5

Binary Search Trees

 To erase an item, start at root node. Using the

same algorithm as inserting an item, stop when find a node with the item value. Identify case:

 Node is a leaf. Delete node and set child pointer in

parent to null.

 Node has one child. Set the child pointer is parent

to point to the child of the node. Delete node.

 Node has two children. Find the immediate

  • predecessor. Replace node value with immediate
  • predecessor. Erase the immediate predecessor

node.

slide-6
SLIDE 6

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 6

In-class Exercise

 Starting with an empty BST, draw the tree that

results from inserting the following integers: 30, 67, 17, 47, 15, 25, 50, 45, 19, 27

 Continuing with this tree, draw the result of

deleting (in order): 50, 67, 17

slide-7
SLIDE 7

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 7

Bag Class Attributes

 This bag class will be implemented using a

binary search tree. There is one attribute:

 rootPtr – a pointer to the head node of the linked

list that contains the bag items

 Note that the textbook chooses to compute the

size of the data structure rather than keep track

  • f the size as items are inserted and removed,

making the Size operation O(n) rather than O(1).

slide-8
SLIDE 8

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 8

Bag Object

 Here is a way to think about how this works.

Bag b; b.Insert(15); b.Insert(34); rootPtr 34 15

slide-9
SLIDE 9

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 9

BTNode Class File

 Note that today's bintree.h file is enhanced

from what we had last class, and you may use it in your project. In this version, non-const Left( ) and Right( ) operations return references to the children pointers so that they may be modified using code like:

ptr->Left( ) = new BTNode<T> (entry); TreeClear(ptr->Left());

 Note that since BTNode is a template, we do

not need to change anything to use it. However, it is in namespace BinaryTreeToolkit.

slide-10
SLIDE 10

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 10

Bag Class Definition

 Examine the Bag template class definition in file

bag6.h

 Has the same typedefs as the other Bag

classes.

 Otherwise operation prototypes are the exactly

the same as the previous Bag classes. Note that a Bag only receives and returns data items; never pointers to the nodes.

slide-11
SLIDE 11

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 11

Bag Class Usage

 Examine file bag6test.cpp  This is an example of an interactive test

  • program. Note that using the Bag object is the

same as previous usages.

slide-12
SLIDE 12

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 12

Bag Class Implementation

 Examine the rest of file bag6.h  In addition to the Bag class operation

implementations, there are several BST utility functions that encapsulate BST operations on

  • BTNodes. Various parts are left as exercises to

be completed today during class.

 As before, we will ignore any bad_alloc

exceptions.

slide-13
SLIDE 13

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 13

Constructors, Destructor, Assignment operator=

 The default constructor simply sets rootPtr to

null to indicate an empty tree.

 The copy constructor simply uses TreeCopy to

make a copy of the source's tree.

 The destructor simply uses TreeClear to

destroy the tree's nodes.

 The assignment operator= makes a self-

assignment test, then uses TreeClear and TreeCopy.

slide-14
SLIDE 14

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 14

Count

 Since the storage representation is a BST, we

can use an iterative algorithm to provide count

  • f a particular element.

 Since the left subtree contains items less than

  • r equal to the node value, we need to find the

first node that equals the target. Once there we need to count the node containing the target as we continue down the left subtree. Eventually, we get to a null child.

 Complete the Count function in bag6.h

slide-15
SLIDE 15

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 15

Insert

 Special case: empty tree

 Create a node with the entry and make it the root

node

 Otherwise, want to find the place the entry

  • goes. Use a boolean flag done to know when

to stop the loop that looks for this place. Need a pointer to the node where the new entry node is attached.

slide-16
SLIDE 16

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 16

Insert

 Basic find algorithm

  • 1. Initialize cursor to rootPtr and done to false
  • 2. While not done do

2.1. Check if entry <= cursor node value 2.1.1. If so, check if left child exists 2.1.1.1. Attach new node with entry as left child 2.1.1.2. Set done to true 2.1.2. Else 2.1.2.1. Move cursor to left child 2.2. Else 2.2.1. Do the same for the right child

 Complete the Insert function in bag6.h

slide-17
SLIDE 17

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 17

EraseOne

 The erasing algorithm for BSTs can be

implemented directly, and is not too difficult. However, a parent pointer must be maintained in order to delete a node from a tree in a manner similar to linked lists.

 The textbook presents an indirect method using

  • recursion. Two auxilliary functions,

BSTRemove and BSTRemoveMax, are used.

slide-18
SLIDE 18

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 18

BSTRemove

 This function takes advantage of the fact that

we can use the Left( ) and Right( ) operations to change the values of the left and right children pointers, respectively. The function receives a root pointer and a target, and passes back the root of the tree with the target removed as well as returning true if it removed the target.

 As usual, we handle the empty tree first and

return false.

slide-19
SLIDE 19

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 19

BSTRemove

 Otherwise, if the target is less than or greater

than the node value, we call BSTRemove with the left or right child pointer, respectively.

 When the target is found, there are two cases:

 If there is no left child, we can just make the right

child the new root and delete the node.

 If there is a left child, we need to replace the root

node value with its immediate predecessor and delete that node. This is done using BSTRemoveMax.

slide-20
SLIDE 20

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 20

BSTRemoveMax

 BSTRemoveMax receives a root pointer and

passes back the root of the tree with the maximum element removed. It also passes back the item whose node was delete in

  • removed. (Since we call BSTRemoveMax with

the data field of the node where we want the maximum value to go, this causes that value to be stored in the correct place.)

slide-21
SLIDE 21

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 21

BSTRemoveMax

 There are two cases to consider

 The node has no right child, which means we have

found the maximum value in the tree. We set removed to the value in this node and then replace it with its left child.

 The node has a right child, so the maximum is still

farther down the right subtree and we call BSTRemoveMax with the right child.

 Complete the BSTRemoveMax function in

bag6.h

slide-22
SLIDE 22

Wednesday, November 17 CS 215 Fundamentals of Programming II - Lecture 36 22

Erase

 Erase is similar to EraseOne, except that it

must delete all of the nodes containing the target.

 It uses BSTRemoveAll, which is left as a bonus

exercise.