Explanation of Solution
Given
//Add 1 to the value in each node of the tree
Void incrementAll(Node tree)
{
//Call the function to increment the left sub-tree
incrementAll (tree.left);
//Call the function to increment the right sub-tree
incrementAll (tree.right);
//Increment the value
tree.Value++;
}
The above program code snippet is used to increment value in each node of the tree.
Error in the program code:
In the above code segment, the function “incrementAll ()” will fail if the node “tree” is equal to “null”. It should be modified using “if” statement.
Corrected code:
The modified code is highlighted below.
//Add 1 to the value in each node of the tree
Void incrementAll(Node tree)
{
if (tree != null)
{
//Call the function to increment the left sub-tree
incrementAll (tree...
Want to see the full answer?
Check out a sample textbook solutionChapter 22 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
- please convert to C languange #include<bits/stdc++.h>using namespace std; class tree{ //tree node public: int data; tree *left; tree *right;}; bool hasRootToLeafSum(tree *root, int s){ bool path=false; //declare boolean variable path //base condition checking if(root==NULL && s==0) return true; s-=root->data; //subtract current root value //checking whether leaf node reached and remaining sum =0 if(s==0 && root->left==NULL && root->right==NULL) return true; //recursively done for both subtrees if(root->left){//for left subtree path=path||hasRootToLeafSum(root->left, s); } if(root->right){//for right subtree path=path||hasRootToLeafSum(root->right, s); } return path;} tree* newnode(int data){ //creating new nodes tree* node = (tree*)malloc(sizeof(tree)); node->data = data; node->left = NULL; node->right = NULL;…arrow_forwardA tree is implemented using a node structure defined as: struct node{ int data; struct node *left; strict node *right; }; Write a function whose prototype is: int smallest(struct node *); which accepts a tree (pointer to the root) and returns the smallest node in the right sub-tree of the root. If root is NULL, or if there is no right sub-tree, returm the number -999.arrow_forwardPlease help! // TreeNode.java class TreeNode<T> { T item; TreeNode<T> leftChild; TreeNode<T> rightChild; public TreeNode(T newItem) { // Initializes tree node with item and no children. item = newItem; leftChild = null; rightChild = null; } // end constructor public TreeNode(T newItem, TreeNode<T> left, TreeNode<T> right) { // Initializes tree node with item and // the left and right children references. item = newItem; leftChild = left; rightChild = right; } // end constructor } // end TreeNode // BinaryTreeBasis.java public abstract class BinaryTreeBasis<T> { protected TreeNode<T> root; public BinaryTreeBasis() { root = null; } // end default constructor public BinaryTreeBasis(T rootItem) { root = new TreeNode<T>(rootItem, null, null); } // end constructor public boolean isEmpty() { // Returns true if the tree is empty, else returns false. return root == null; } // end isEmpty public void makeEmpty() { // Removes all nodes from the…arrow_forward
- c++ code screenshot and output is mustarrow_forwardProgramming questions:typedef struct node { int data; struct node *left, *right;}BT;The node structure of the binary tree (BT) is shown above. There is a binary tree T, please complete the function: int degreeone(BT *T) to compute how many degree 1 node in the BT. The T is the root pointer, and the function shoule return the total number of degree 1 node.arrow_forwardjava binaryarrow_forward
- C++ DATA STRUCTURES Implement the TNode and Tree classes. The TNode class will include a data item name of type string,which will represent a person’s name. Yes, you got it right, we are going to implement a family tree!Please note that this is not a Binary Tree. Write the methods for inserting nodes into the tree,searching for a node in the tree, and performing pre-order and post-order traversals.The insert method should take two strings as input. The second string will be added as a child node tothe parent node represented by the first string. Hint: The TNode class will need to have two TNode pointers in addition to the name data member:TNode *sibling will point to the next sibling of this node, and TNode *child will represent the first child ofthis node. You see two linked lists here??? Yes! You’ll need to use the linked listsarrow_forwardclass Node{// attributesint ID;String name; Node left; // left child Node right; // right child}class BinarySearchTree{// attributesNode root;// methodssearch(int key, Node root);void insert(int key, Node root);void delete(int key, Node root);}Extend the BinarySearchTree class:1 Write code for the method search(int key, Node root)2 Write code for the method insert(int key, Node root)3 Write code for the method delete(int key, Node root)4 Write code for a main method to build a BSTTest your code with the input in the worked example.Input:// id, name41, notes11, personal61, work30, shopping5, recipes55, proposal70, thesis10, muffins43, draftarrow_forwardC programming I need help writing a code that uses a struct pointer into a binary tree and using the same pointer into an arrayarrow_forward
- 2. Write the code for preorder traversal of a binary tree. struct node{ int data; struct node *left, *right; } void preorder(node *x) { }arrow_forwardProgramming language C++ problemarrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education