I have written some code below, and need help to finish the rest of my assignment, I got stuck. Please extend my code, here are some parts that I still need help:
Next, rearrange the sorted array into a second array which moves the middle element into the first position, the elements in the first quarter and third quarter positions of the sorted array into the second and third positions of the second array, the elements in the first, third, fifth and seven-eighths positions into the fourth through seventh positions, and so forth, until all elements of the sorted array have been entered into the second array. (I think I did this part by findMiddlePoint, but I am unsure)
Move each of the elements of the second array in succession into a new binary tree structure. Be sure to delete the original binary tree when the input names have all been moved to the second binary tree, which will be as balanced as possible.
Display the second binary tree from (4) using the available binary tree display algorithm which was adapted from M&S pages 505-506.
class Node {
private:
string val; // current value in collating sequence
Node* lh; // pointer to left-hand descendant Node
Node* rh; // pointer to right-hand descendant Node
public:
Node() { lh = rh = NULL; val = ""; } // Node () {}
void putVal(string v) { val = v; }
string getVal() { return val; }
void putLH(Node* x) { lh = x; }
Node* getLH() { return lh; }
void putRH(Node* x) { rh = x; }
Node* getRH() { return rh; }
void write(Node* n);
};
void Node::write(Node* n) { // Diagnostic to display a node of a Binary Tree
cout << "Value " << val << " in Node at " << (int)n;
cout << "\t Its LH child " << (int)(lh) << "\t Its RH child "
<< (int)(rh) << endl;
}
class BT { // Container class - does most of the work
private:
int count;
int idx = 0;
Node* root;
Node* tree;
array listofnames;
array array;
private:
Node* addNode(Node*, string);
Node* findInsertion(Node*, string);
void inOrder(Node*);
void locate(Node*, string);
void treePrint(Node*, int);
public:
BT();
sort ();
static const size_t numberofnames = 7;
void comeBineArray();
void addRootNode(string);
void inOrderTraverse();
void locateRoot(string);
void treePrintWrapper();
void findMiddlepoint(int,int,int,int,int);
};
BT::BT() { root = tree = NULL; count = 0;
//ask for input
} // BT::BT () {}
void BT::comeBineArray(){
for (int = 0; i < numberofnames; i++){
addRootNode(bArray[i]);
}
}
void BT::addRootNode(string v)
{
if (root == NULL)
{
root = tree = addNode(tree, v);
// cout << "ptr to root " << root << endl;
}
else
{
tree = root;
tree = findInsertion(tree, v);
}
}
Node* BT::findInsertion(Node* tree, string v)
{
string x;
x = tree->getVal();
cout << "comparing the values:" << nameinput << " and " << x << "\n" << endl;
if (v <= x) // v = nameinput; x = currentname
if (tree->getLH() != NULL)
{
tree = findInsertion(tree->getLH(), v);
return tree; // *** from Paul Pollack 11/13/13 ***new code to prevent
// a new node from being added twice
}
else
{
Node* temp = NULL;
temp = addNode(temp, v);
tree->putLH(temp);
}
else // if ( v > x )
if (tree->getRH() != NULL)
{
tree = findInsertion(tree->getRH(), v);
return tree; // *** from Paul Pollack 11/13/13 ***new code to prevent
// a new node from being added twice
}
else
{
Node* temp = NULL;
temp = addNode(temp, v);
tree->putRH(temp);
}
return tree;
}
Node* BT::addNode(Node* x, string v)
{
if (x = new Node)
{
x->putVal(v);
x->putLH(NULL);
x->putRH(NULL);
}
return x;
}
void BT::inOrderTraverse()
{ //tree = root;
cout << endl;
inOrder(root);
}
void BT::inOrder(Node* n, string array[])
{
if (n != NULL) {
inOrder(n->getLH());
n->write(n);
array[idx] = n;
idx ++;
cout << endl;
inOrder(n->getRH());
}
return;
}
void BT::findMiddlepoint(int a, int b, int level, int number, int round){
int mid;
if (abs(a-b)<2){
if(a==1){
array[idx] = listofnames[a-1];
idx++;
}
if (b==number){
array[idx] = listofnames[b-1];
idx++;
}
return;
}
if ((b+a)%2){
mid=(a+b+round)/2;
array[idx] = listofnames[mid-1];
idx++;
}
else{
mid = (a+b)/2;
array[idx]= listofnames[mid-1];
idx++;
}
}
findMiddlepoint(a,mid,(level+1),number,-1);
findMiddlepoint(mid,b,(level+1),number,1);
return;
}
void BT::locateRoot(string v) {
if (root == NULL)
cout << "Tree is empty" << endl;
else
locate(root, v);
}
void BT::locate(Node* y, string v) { // Bug mentioned by Dan Glade
string x;
x = y->getVal();
if (v == x)
{
cout << "Value " << v << " is in Node at address " << (int)y << endl;