Question

Create a binary tree class. -Data array field (private) -Count field Add method(data) -The add method...

Create a binary tree class.

-Data array field (private)

-Count field

Add method(data)

-The add method takes a data point, add it to the count position in the array then increases the count.

Print method(void)

-Prints all the fields in the tree.

C++, no libraries are allowed (only )

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Binary tree in an array is stored as:-

if the node is at nth postion, then the left child will be at 2*n + 1 postion and the right child will be at 2*n + 2 position,

So if we enter data one by one into the array it will first go to root node then to its left child and then to its right child, and this process will repeat with left chid being the root node, and then right child and so on.

The code for above program is given below, I have commented inline for the explanation :-

#include <iostream>

using namespace std;

class Binary_tree
{
private:
long data; // private variable data
public:
int count=0;// public variable count
  
// method to add the data, it returns the added value
// so we can add it to the array
long add(long data)
{
this->data = data;// adds the data
this->count++;// after adding increaments the counter
return this->data; // returns the added value
}
};

// method to print the binary tree
void print(long arr[], int n)
{
cout<<"The Binary tree is:- \n";
  
for(int i=0;i<n;i++)
{
cout<<"At position "<<i<<" the data is: "<<arr[i]<<"\n";
}
}
  


int main()
{
int n;
cout<<"Enter number of data you want to enter: ";
cin>>n;
long arr[n];// array to store the binary tree
  
Binary_tree bt;// Binary_tree calss object
cout<<"enter the values:- \n";
for(int i=0;i<n;i++)
{
int val;
cin>>val;// takes the value
arr[bt.count]=bt.add(val);// adds in the array and increaments the counter
}
  
// print the Binary_tree array
print(arr,n);

return 0;
}

Output:-

Hope it clears your doubt

Add a comment
Know the answer?
Add Answer to:
Create a binary tree class. -Data array field (private) -Count field Add method(data) -The add method...
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
  • Extend the array based Binary Tree lab by adding these methods. (Remember 2i + 1 &...

    Extend the array based Binary Tree lab by adding these methods. (Remember 2i + 1 & 2i + 2) Preorder(int) -Recursive method that prints all the nodes in a VLR pattern. Inorder(int) -Recursive method that prints all the nodes in a LVR pattern. Postorder(int) -Recursive method that prints all the nodes in a LRV pattern. C++, no libraries are allowed

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • 1. Create a new class called ReversibleArray. 2. In the class header, add the code <T>...

    1. Create a new class called ReversibleArray. 2. In the class header, add the code <T> immediately to the right of the class name. 3. Give this class two private instance variables: T[] array and int count 4. Create a constructor which takes in one parameter of type T[] and assign that parameter's value into this.array. Set count as the length of the array. 5. Add a toString() method that outputs the array values in the format: elem0, elem1, elem2,...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

  • Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int...

    Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int virtualArrayLength; // the number of elements in the dynamic array // Throws an IndexOutOfBoundsException if i is not a valid index // for adding to the dynamic array, otherwise inserts s at index i. // Elements can be added from index 0 to this.size(). public void add(int i, String s) { // If there is no room for s in data, create a new...

  • Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

    Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields. Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 21...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

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
ADVERTISEMENT