tar.gz
archive of your asg4/ directory, including:
README
file containing
Makefile
.h
headers and .cpp
source)
make clean
before tar
)
handin
notes
erase()
function
private
member functions:
bool contains(const T&, Node* ) const;
int insert(const T& x, Node* &t);
int erase(const T& x, Node* &t);
void clear(Node* &);
Node* min(Node* ) const;
Node* max(Node* ) const;
Node* clone(Node* ) const;
void rotate_left(Node* &t);
void rotate_right(Node* &t);
insert()
and delete()
routines
print out rotation information if rotations are needed
operator<<
output function prints the
tree contents via inorder traversal, outputting node
contents along with balance info in ()
's
main()
routine works unaltered:
#include <iostream>
#include <iomanip>
using namespace std;
#include "tree.h"
// prototypes
int main(int argc, char **argv);
int main(int argc, char **argv)
{
Tree<int> tree;
// single rotations
cerr << "inserting: " << setw(2) << 6 << " "; tree.insert(6); cerr << endl;
cerr << "inserting: " << setw(2) << 4 << " "; tree.insert(4); cerr << endl;
cerr << "inserting: " << setw(2) << 2 << " "; tree.insert(2); cerr << endl;
cerr << "inserting: " << setw(2) << 8 << " "; tree.insert(8); cerr << endl;
cerr << "inserting: " << setw(2) << 10 << " "; tree.insert(10); cerr << endl;
cerr << "inserting: " << setw(2) << 12 << " "; tree.insert(12); cerr << endl;
cerr << "inserting: " << setw(2) << 14 << " "; tree.insert(14); cerr << endl;
// double rotations
cerr << "inserting: " << setw(2) << 32 << " "; tree.insert(32); cerr << endl;
cerr << "inserting: " << setw(2) << 30 << " "; tree.insert(30); cerr << endl;
cerr << "inserting: " << setw(2) << 28 << " "; tree.insert(28); cerr << endl;
// set up for deletion
cerr << "inserting: " << setw(2) << 3 << " "; tree.insert(3); cerr << endl;
cerr << "inserting: " << setw(2) << 31 << " "; tree.insert(31); cerr << endl;
// inorder tree traversal
cout << "Tree: " << endl << tree;
// erase
cerr << " erasing: " << setw(2) << 6 << " "; tree.erase(6); cerr << endl;
// inorder tree traversal
cout << "Tree: " << endl << tree;
}
inserting: 6 inserting: 4 inserting: 2 rotate_left inserting: 8 inserting: 10 rotate_right inserting: 12 rotate_right inserting: 14 rotate_right inserting: 32 inserting: 30 double_rotate_right inserting: 28 double_rotate_right inserting: 3 inserting: 31 Tree: 2(1) 3(0) 4(-1) 6(0) 8(1) 10(0) 12(-1) 14(1) 28(0) 30(1) 31(0) 32(-1) erasing: 6 double_rotate_left rotate_right Tree: 2(0) 3(0) 4(0) 8(0) 10(0) 12(-1) 14(0) 28(0) 30(1) 31(0) 32(-1)
bal
integer to encode
balance info (instead of height
to
encode height—this is a minor syntactic change
but a major semantic one)
double_left()
and double_right()
routines and just use
rotate_left()
and rotate_right()
;
this compacts the code a good deal
erase()
routine