C++ Data Structures and Algorithms
Binary Trees: Implementation
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;
};
Write a recursive function that sums up all of the values in the binary tree. The base case is that the sum is 0 for an empty tree (or when you reach a NULL node), otherwise you add the item value of the current node to the sum you get for the left and right subtrees of this node.
The function signature for the recursive function should be the following:
/** sum tree values
* Sum up the values in all of the nodes of the given tree and return
* this sum.
* * @param node The node of the tree we are to calculate the sum for. The root * of a tree can be given initally to calculate the sum for the whole tree.
* * @returns int Returns the sum of the tree given as input for this function.
*/
int sumOfTree(BinaryTreeNode* node)
{
}
Solution:
Contains only the required function...
Code:
/*
* Sum tree values
* Sum up the values in all of the nodes of the given tree and
return
* this sum.
*
* @param node The node of the tree we are to calculate the sum
for.
* The root of a tree can be given initally to calculate the
sum
* for the whole tree.
*
* @returns: int Returns the sum of the tree given as input
for
* this function.
*/
struct BinaryTreeNode
{
int item; BinaryTreeNode* left;
BinaryTreeNode* right;
};
int sumOfTree(BinaryTreeNode* node)
{
if (root == NULL) //Checking the base
case..
return
0;
//Recursive function call..
return (root->data +
sumOfTree(root->left) + sumOfTree(root->right))
;
}
//Adding the Root->data with the all the nodes->
data..
//At each recursion call root->left and root->right will
become the
//root node for that current iteration..
//Above program is checked and executed successfully..
//Gives the exact sum as output as expected..
-----------------------------------------------------------------------
Code screenshots:

------------------------------------------------------------------------
Please comment if you have any queries or changes required in this solution..
Kindly up-vote if you are satisfied with this solution..
C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions...
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)...
Write a recursive function that compares two given binary trees. It returns 1 if the two trees are different, and it returns 0 otherwise. The function signature is: int treeDiff (node "a, node *b); Write a recursive function that counts how many nodes in a tree have a single child. The function signature is: int countoneChild(node "root);
java data structures and algorithms binary
trees part
thank you very much
Dagger Given a binary tree and a sum, the method has PathSum determines if the tree has a squareroot-to-leaf path such that adding up all the key values along the path equals the given sum. It uses an auxiliary method which takes the squareroot of the given tree to do its job as follows: public boolean hasPathSum(int sum){ return hasPathSum(root, sum); } Given the following tree where the...
/* * struct for a single node in a binary tree. data contains
the int
* stored in this node. left and right contain pointers to the
left and
* right subtrees respectively. *
* All of the ints stored in the left subtree is smaller than
data.
* All of the ints stored in the right subtree is larger than
data.
*/
struct node {
int data;
struct node *left;
struct node *right;
};
typedef struct node node;
Write...
1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...
In Java. How would this method look?
LinkedBinaryTree.java
import java.util.Iterator;
public class LinkedBinaryTree implements BinaryTreeADT {
private BinaryTreeNode root;
/**
* Creates an empty binary tree.
*/
public LinkedBinaryTree() {
root = null;
}
/**
* Creates a binary tree from an existing root.
*/
public LinkedBinaryTree(BinaryTreeNode root) {
this.root = root;
}
/**
* Creates a binary tree with the specified element...
C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...
PROMPT:
Consider a binary tree (not necessarily a binary search tree)
with node structure.
QUESTION:
Prove that findMax works by mathematical induction.
struct Node int val; struct Node * left; struct Node* right; The following function findMax returns the largest value 'val in the tree; and returns -1 if the tree is empty. You may assume that all the values 'val' in the tree are nonnegative. struct Node * findMax(struct Node root) if (rootNULL) return -1; maxval = root->val; maxval...
Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...
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...