You are given two classes for implementing a simple binary tree capable of storing number, count the number of leaves and computes the height of a binary tree.

You can add on additional parameters or functions as you wish, but the program must apply the chapter objectives.

// Node for the binary tree items
class BTreeNode
{
public:
BTreeNode(double x, BTreeNode *leftp = NULL, BTreeNode *rightp = NULL)
{
value = x;
left = leftp;
right = rightp;
}

private:
double value;
BTreeNode *left, *right;
friend class BST; // BST has friend status
};
// Binary tree class itself
class BST
{
public:
int height() { return height(tree); } // return the tree height
void insert(double x); // insert numbers into the binary tree
void inorder(vector< double>& v) { inorder(v, tree); } // appens all nodes in tree
int leafCounter() { return leafCounter(tree); } // count the number of leaves
BST() { tree = NULL; }

private:
void inorder(vector< double>& tlist, BTreeNode *t); // storing nodes in subtree
int leafCounter(BTreeNode *t); // count number of leaves
static int height(BTreeNode *t); // calculate the height of the tree
BTreeNode *tree;
};

Sample Output:

This program allows you to insert some numbers in binary search tree.
It prints out the numbers in the tree in inorder.
It computes and prints the number of leaves in the tree.
and the height of the tree.
Enter 10 numbers:
8 20 6 4 23 30 29 33 7 45

The items in the tree inorder are:
4 6 7 8 20 23 29 30 33 45
The height of the tree is 6
The number of leaves is 4
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.