Question

#include <iostream> using namespace std; class BinarySearchTree{    public:        int size;        int...

#include <iostream>

using namespace std;

class BinarySearchTree{
   public:
       int size;
       int *array;
      

       BinarySearchTree(int size){
           int newSize = extendSize(size);
           array = new int[newSize];
           for(int x = 0; x < size; ++x){
               array[x] = NULL;
           }
          
       }
}

Given the code above implement the following:

5. Write a function that displays all the left sub
roots of that tree.
6. Write a function that counts and displays the
number of leafs in the tree.

7. Write a function that displays only the leafs of
the tree.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

class BinarySearchTree{
   public:
       int size;
       int *array;
      

       BinarySearchTree(int size){
           int newSize = extendSize(size);
           array = new int[newSize];
           for(int x = 0; x < size; ++x){
               array[x] = NULL;
           }
                   this->size = newSize;
       }
           
           void printLeftSubroots() {
                        int start = 0;
                        
                        while(start < size) {
                                cout << array[start];
                                start = 2*start + 1;
                        }          
           }
           
           
           void countLeafs() {
                        int count = 0;
                        for(int i=size-1; i>=0; i++) {
                                if(array[i] != NULL) {
                                        int l = 2*i + 1;
                                        int r = 2*i + 2;
                                        
                                        // if no left and right child present, then it is a leaf
                                        if(l >= size && r>= size) {
                                                count++;
                                        }
                                }                       
                        }
                        cout << "num Leafs: " << count << endl;
           }
           
           void printLeafs() {
                        int count = 0;
                        for(int i=size-1; i>=0; i++) {
                                if(array[i] != NULL) {
                                        int l = 2*i + 1;
                                        int r = 2*i + 2;
                                        
                                        if(l >= size && r>= size) {
                                                cout << array[i] << endl;
                                        }
                                }                       
                        }
           }
}
Add a comment
Know the answer?
Add Answer to:
#include <iostream> using namespace std; class BinarySearchTree{    public:        int size;        int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left;...

    #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left; // left child Node* right; // right child Node* p; // parent Node(int data) { value = data; left = NULL; right = NULL; p = NULL; } ~Node() { } int d() { return value; } void print() { std::cout << value << std::endl; } }; int main(int argc, const char * argv[]) { } function insert(Node *insert_node, Node *tree_root){ //Your code here...

  • #include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main()...

    #include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main() { int size = 10; int * A = newZeroArray(size); for(int i = 0; i < size; i++) cout << A[i] << " "; cout << endl; }Write a function that takes a size, creates a new array of that size with all zeros, and returns the array. If the size is not a valid size for an array, do not return a valid...

  • #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode...

    #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode * left; TNode * right; TNode * parent; }; class BTree { public: //constructors and destructor BTree(); BTree(TNode *r);// initialize BTree with the root r. r is the only node. BTree(const BTree & t); // copy constructor BTree(const int *p, const int n);// similar to the copy constructor, but your input is of the array form. // input is given an array that denotes...

  • Analyze the following code. #include <iostream> using namespace std; class B { public: B() { };...

    Analyze the following code. #include <iostream> using namespace std; class B { public: B() { }; int k; }; int main() { B b; cout << b.k << endl; return 0; } The program has a compile error because b.k cannot be accessed. The program has a runtime error because b.k does not have a value. The program displays unpredictable number. The program displays 1.

  • use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void...

    use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void setSize() { cout << "Enter an integer larger than 2: "; cin >> size; } int getSize() { return size; } void fibTerms(int x) // Write a recursive function { } explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion { setSize(); int *arr; arr = new int[getSize()]; arr[0] = 1; arr[1] = 1; // Call recursive function here...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • What is the output of this program? #include <iostream>   using namespace std;   class rect   {       int...

    What is the output of this program? #include <iostream>   using namespace std;   class rect   {       int x, y;       public:       void val (int, int);       int area ()       {           return (x * y);       }   };   void rect::val (int a, int b)   {       x = a;       y = b;   }   int main ()   {       rect rect;       rect.val (3, 4);       cout << "rect area: " << rect.area();       return 0; } a) rect area:7 b) rect area: 12 c) rect area:24 d) none of the...

  • What is printed by the following program? (read the code carefully) #include <iostream> using namespace std;...

    What is printed by the following program? (read the code carefully) #include <iostream> using namespace std; class A { public: int x; }; int main() { A *a = new A; A *b = new A; a->x=1; b->x=1; if (a == b) { cout << "black"; } else { cout << "white"; } delete a; delete b; return 0; }

  • (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std;...

    (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT