Question

Short Answers Section 10.4 Tree Traversals 9. Using the binary_tree_node from Section 10.3, Write a recursive...

Short Answers
Section 10.4
Tree
Traversals

9. Using the binary_tree_node from Section 10.3, Write a recursive function to meet the following specification. You do not need to check the precondition.

template <class Item>
void increase(binary_tree_node<Item>* root_ptr)
// Precondition: root_ptr is the root pointer of a binary tree.
// Postcondition: Every node of the tree has had its data increased by one.

10. Using the binary_tree_node from Section 10.3, write a recursive function to meet the following specification. You do not need to check the precondition.

template <class Item>
size_t many_nodes(binary_tree_node<Item>* root_ptr)
// Precondition: root_ptr is the root pointer of a binary tree.
// Postcondition: The return value is the number of nodes in the tree.
// NOTES: The empty tree has 0 nodes, and a tree with just a root has
// 1 node.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for Increasing the data value by one

void increase(binary_tree_node<Item>* root_ptr) {

if(root_ptr == NULL)

return;

else {

root_ptr->data = root_ptr->data+1;

increase(root_ptr->left);

increase(root_ptr->right);

}

}

Function to find the number of nodes in the tree

If the root is null then there are no nodes in the tree and we return 0.

If root is not null we initialize count to 1 and find the number of nodes in each sub tree recursively and add it to the count and finally return the total count.

size_t many_nodes(binary_tree_node<Item>* root_ptr) {

if(root_ptr == NULL)

return 0;

else {

size_t count = 1;

count+= many_nodes(root_ptr->left);

count+= many_nodes(root_ptr->right);

return count;

}

Add a comment
Know the answer?
Add Answer to:
Short Answers Section 10.4 Tree Traversals 9. Using the binary_tree_node from Section 10.3, Write a recursive...
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
  • Implement the function for the prototype below. Document your code. You do not have access to...

    Implement the function for the prototype below. Document your code. You do not have access to authors helper functions, only the binary_tree_node class. template size_t count(binary_tree_node * root_ptr, Item value) // Precondition: root_ptr is the root pointer of a binary tree (which may be empty). // Postcondition: The number of nodes that contain value in the tree is returned.

  • 9. Please implement the following function (recursively) 1. template < class Item> 2. void flip(binary_ tree_node...

    9. Please implement the following function (recursively) 1. template < class Item> 2. void flip(binary_ tree_node Item root ptr) 3. I/ Precondition: root_ptr is the root pointer of a non-empty binary tree .I/ Postcondition: The tree is now the mirror image of its original value. Example: 2 3 3 2 Answer: 1. template class Item> 2. void lip (binary_tree_node <Item root ptr) 3. binary_tree_node <Ittemp; assert (root ptr!-NULL) 4.

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes...

    Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes arranged as binary trees. For these questions, use the following struct definition for the nodes of the tree (we will not templatize the node here, so you do not have to write template functions for these questions, just assume trees of <int> values): struct BinaryTreeNode { int item; BinaryTreeNode* left; BinaryTreeNode* right; }; ---------------------------------------------- Given a node for a Binary Tree and an (integer)...

  • QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent...

    QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent parent sibling QUESTION 2 In a tree, the ____ is a measure of the distance from a node to the root. count degree branch height level QUESTION 3 Which of the following is not a characteristic of a binary search tree? Each node has zero, one, or two successors. The preorder traversal processes the node first, then the left subtree, and then the right...

  • Write a C++ program to validate computer user-ids and passwords. A list of valid ids and...

    Write a C++ program to validate computer user-ids and passwords. A list of valid ids and passwords (unsorted) is read from a file and stored in a Binary Search Tree (BST) of UserInfo objects. When user-ids and passwords are entered during execution, this BST is searched to determine whether they are legal. Input (file): UserInfo records for valid users Input (keyboard): Ids and passwords of users logging in Output (screen): Messages indicating whether user-ids and passwords are valid, as well...

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