Lab 12: Binary Search Tree ADT; InLab 1, PL 1 PowerPoint PPT Presentation

presentation player overlay
1 / 5
About This Presentation
Transcript and Presenter's Notes

Title: Lab 12: Binary Search Tree ADT; InLab 1, PL 1


1
Lab 12 Binary Search Tree ADT InLab 1, PL 1
Given Bstree.hs, Show12.cpp, Test12.cpp
(main(), Bridge), Getdbrec.cpp
(main(), Inlab 1), Accounts.dat,
Database.cs indexed accounts database Write
Bstree.h (prototypes of the
functions insertSub(), retrieveSub(),
removeSub(), writeKeysSub(),
clearSub()) Bstree.cpp
Database.cpp (main(), Inlab 1) build an index
tree
2
Remove(KF deleteKey)
  • Remove a leaf
  • Remove a node with only one child
  • Remove a node with two children?
  • - replace its info data member with the info
    data member from another node in the tree that
    retains the search property.
  • - remove this other node
  • What element could we replace?
  • items logical predecessor or successor

3
Bstree.h Class BSTree Public int remove (
KF deleteKey ) // Remove
element Private int removeSub (
BSTreeNodeltTE,KFgt p, KF deleteKey )
void cutRightmost ( BSTreeNodeltTE,KFgt r,
BSTreeNodeltTE,KFgt
delPtr )
3
4
Bstree.cpp int BSTreeltTE,KFgt removeSub (
BSTreeNodeltTE,KFgt p, KF deleteKey )
BSTreeNodeltTE,KFgt delPtr // Pointer to node to
delete int result
// Result returned if ( p 0 )
result 0 //
Search failed else if ( deleteKey lt
p-gtelement.key() ) result
removeSub(p-gtleft,deleteKey) // Search left
else if ( deleteKey gt p-gtelement.key() )
result removeSub(p-gtright,deleteKey) //
Search right else
// Found delPtr p
if ( p-gtleft 0 ) p p-gtright
// No left child else
if ( p-gtright 0 ) p p-gtleft
// No right child else
cutRightmost(p-gtright,delPtr) // Has both
children delete delPtr result
1 return result
4
5
Bstree.cpp void BSTreeltTE,KFgtcutRightmost (
BSTreeNodeltTE,KFgt r,

BSTreeNodeltTE,KFgt delPtr ) //Recursive
partner of the removeSub() function. Traces down
a // sequence of right children. Copies the data
from the last node in // the sequence into the
node pointed to delPtr, sets delPtr to point //
to the last node in the sequence, and links
around this node. if ( r-gtright ! 0 )
cutRightmost(r-gtright,delPtr) // Continue
down to the right else
delPtr-gtelement r-gtelement delPtr r
r r-gtleft
5
Write a Comment
User Comments (0)
About PowerShow.com