please explain each line of code! ( in python
)





1.
CODE
# Function to find sum of all the even element
def binary_tree_even_sum(root):
# Base case: If the root is empty, then return zero
if (root == None):
return 0
# if the root is even, then add it to the sum
if root.key % 2 == 0:
return (root.key + binary_tree_even_sum(root.left) +
binary_tree_even_sum(root.right))
else:
return (binary_tree_even_sum(root.left) +
binary_tree_even_sum(root.right))

NOTE: As per HOMEWORKLIB POLICY, I am allowed to answer only 1 coding question (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.
1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...
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...
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...
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;...
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);
Write a (void) method to traverse a binary tree in Breadth-First order, where you start from the root, print the node values of its children, (left and then right) (these are at level 1), then print all node values at level 2 from left to right, and then level 3 etc. For the example binary tree below, you should get the result A, B, E, C, F, D, G, H. Notice that this type of traversal is not the same...
2. Write a recursive algorithm which computes the number of nodes in a general tree. 3. Show a tree achieving the worst-case running time for algorithm depth. 4. Let T be a tree whose nodes store strings. Give an efficient algorithm that computes and prints, for every node v of T, the string stored at v and the height of the subtree rooted at v. Hin Consider 'decorating' the tree, and add a height field to each node (initialized to...
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...
You will be given several strings full of different keyboard
characters, some of which are letters of the alphabet.
Write a java program that creates a binary tree that uses
only the alphabetical characters (a-z, A-Z) as the value within the
leaf nodes using recursion and preorder traversal. All
other values within the tree (either the root node or internal
nodes) will be null. You may create any methods you see fit, so
long as the final binary tree is...
Previous code:
class BinarySearchTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def search(self, find_data):
if self.data == find_data:
return self
elif find_data < self.data and self.left != None:
return self.left.search(find_data)
elif find_data > self.data and self.right != None:
return self.right.search(find_data)
else:
return None
def get_left(self):
return self.left
def get_right(self):
return self.right
def set_left(self, tree):
self.left = tree
def set_right(self, tree):
self.right = tree
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def traverse(root,order):...
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...