Question

C++ Write a public member function incrementNodes for a BinaryTree class that will add 1 to...

C++

Write a public member function incrementNodes for a BinaryTree class that will add 1 to each item in the binary tree. If this function calls any other function, you must write the code for that function as well. Note that the class BinaryTree has a private member variable root which is a Node pointer.

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

using namespace std;

struct Node {
    int data;
    Node *left;
    Node *right;
};

class BinaryTree {
private:
   Node *root;
   void incrementNodes(Node *temp);
public:
   void incrementNodes();
};

// Below is the code that you need
void BinaryTree::incrementNodes() {
    incrementNodes(root);
}

void BinaryTree::incrementNodes(Node *temp) {
    if(temp != NULL) {
        temp->data += 1;    // increment data in node
        incrementNodes(temp->left); // recursive call to left subtree
        incrementNodes(temp->right);    // recursive call to right subtree
    }
}
// Above is the code that you need

int main() {
   return 0;
}

Hey\;let\;me\;know\;if\;you\;need\;me\;to\;change\;anything

Please\;upvote\;this\;answer..

Add a comment
Know the answer?
Add Answer to:
C++ Write a public member function incrementNodes for a BinaryTree class that will add 1 to...
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
  • Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings...

    Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings    private class Node {        private Node left;        private String data;        private Node right;        private Node parent; // reference to the parent node        // the parent is null for the root node        private Node(Node L, String d, Node r, Node p) {            left = L;            data...

  • Add a public member function to the BST class below that returns the size of the...

    Add a public member function to the BST class below that returns the size of the tree—the number of the nodes in the tree. For simplicity, the class contains only the key and not a value. You may add any helper functions you find necessary. (Hint: think recursion) template <typename T> struct Node {                 T key;                 Node<T>*              left;                 Node<T>*              parent;                 Node<T>*              right; }; template <typename T> class BST { private:         Node<T>* root; public:         BST():...

  • 3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

    3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...

  • java Description You are to create a class named BinaryTree. This will simulate a balanced and...

    java Description You are to create a class named BinaryTree. This will simulate a balanced and ordered binary tree. You also need to create a TreeNode class, which defines the data contained in each node of the tree. In this case, it is an integer. Remember that the node also needs a way to track back to its parent, as well as its left and right children. For the purpose of this assignment, each node should also contain an indication...

  • 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)...

  • You are to write well-formed C++ code that defines and implements a new public member function...

    You are to write well-formed C++ code that defines and implements a new public member function of the attached doubly linked list data structure, which is implemented using two dummy nodes. Implement your function using recursion. That is, your new public member function must call a new private, recursive helper function. A non-recursive version of the helper function will get no credit. The helper function should not have any loops at all. Do not use any global variables. Both new...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary...

    Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.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 Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...

  • Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public...

    Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function. he main program has been written...

  • part (C, F ) only. With the java code (write the sequence of method calls as...

    part (C, F ) only. With the java code (write the sequence of method calls as well) Trees and java code please provide me your email, ill send the code there Show how we visualize the binary search tree with root referenced by rootA (page 490) after each of the following changes. Also list the sequence of Binary- SearchTree method calls, both public and private, that would be made when executing the change. Use the original tree to answer each...

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