Need computer science help with a Binary Tree C++

Dec 7th, 2015
RockCafe
Category:
Computer Science
Price: $150 USD

Question description

Use the binary balancing algorithm here: 

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;

void FindMiddle(int x, int y, int level, int nr,
  int round)
{ int mid;
  if (abs(x-y)<2)
  { if (x==1)
  cout << level << " " << setw(2) << x << endl;
  if (y==nr)
  cout << level << " " << setw(2) << y << endl;
  return;
  }
  if ((y+x)%2)
  { mid=(x+y+round)/2;
  cout << level << " " << setw(2) << mid << endl;
  }
 else
  { mid = (x+y)/2;
  cout << level << " " << setw(2) << mid << endl;
  }
  FindMiddle(x,mid,(level+1),nr,-1); // round down left
  FindMiddle(mid,y,(level+1),nr,1);  // round up right
  return;
}

int main()
{ int nr, level=0;
  cout << "Enter integer number of elements: ";
  if (cin >> nr)
  if (nr>2)
  FindMiddle(1,nr,(level+1),nr,1);
  return 0;
}

I want a program given a data set of these names: William, Ben, Edward, Kevin, Greer, Tim, Nate, John, Raziel, Rongjuan, Ofer, Steve, Ian, Tom.  The program must construct a binary tree which is as balanced as possible. There are 14 names in this list, it is one name short of the count which is required to produce a perfectly balanced binary tree structure which contains four levels. Nevertheless, the program must create a tree which contains these names and which is as balanced as possible by using the code above.


Here is an outline of what the program must do:

(1)  Enter the unsorted list of names into an array and sort this list in the array.  Be sure to retain the count of array elements.  For this problem you may use static arrays which are large enough to handle the sample test data given above.

(2)  Use the count of array elements and the findMiddle algorithm to rearrange the sorted array into a second array which contains the middle element in the first position, the elements in the first quarter and third quarter positions in the second and third positions, the elements in the first, third, fifth and seven-eighths positions into the fourth through seventh positions, etc.

(3)  Move each of the elements of the second array in succession into a binary tree structure.

(4)  Display the balanced tree using one of the available binary tree display algorithms.

Please copy and paste your source code directly to me and screen shot the output.

Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Purdue University
PREMIUM TUTOR

Studypool has helped 1,244,100 students

3 Reviews


Summary
Quality
Communication
On Time
Value
BlueOcean
Nov 13th, 2016
" Awesome! Exactly what I wanted. "
ashleyisgod
Oct 21st, 2016
" Top quality work from this guy! I'll be back! "
Molly_Moon
Sep 29th, 2016
" AMAZING as always! "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1824 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors