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 21 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- True or False: Any node in a tree can be viewed as being the root of its own tree.arrow_forwardCO LL * Question Completion Status: QUESTION 3 Write a recursive function, OnlyChild(..), that returns the number of nodes in a binary tree that has only one child. Consider binaryTreeNode structure is defined as the following. struct binaryTreeNode int info; binaryTreeNode *llink: binaryTreeNode *rlink; The function is declared as the following. You must write the function as a recursive function. You will not get any credits if a non-recursive solution is used. int OnlyChild(binaryTreeNode *p); For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Paragraph Arial 10pt B. ^三へ三 三山 三Ex? X2 = E E E 9 Click Save and Submit to save and submit. Click Save All Answers to save all ansuwers. Is E English (United States) Focus || 15 stv MacBook Air D00 O00 F4 F5 F8 64arrow_forwardtypedef struct node{int data;struct node *left,*right;}BST;The node structure of BST is shown above. Please design an efficient algorithm to return the maximum keyword value in BST. Completion function: int findmax (BST *T)arrow_forward
- A 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_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_forward
- java binaryarrow_forwardC++ 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_forward
- (* First argument is a BST. The second argument is matched with the largest value in the tree. If the tree is empty, the second argument is matched with -1 OR the predicate may fail. *) getMax/2 IN PROLOG PROGRAMMING LANGUAGEarrow_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_forward2. Write the code for preorder traversal of a binary tree. struct node{ int data; struct node *left, *right; } void preorder(node *x) { }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