Question

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

Write a function that will invert a LinkedBinaryTree in-place (no new nodes are created. You are simply changing the left and

4. A full binary tree (or proper binary tree) is a binary tree in which every node in the tree has 2 or 0 children. Implement

5. A complete binary tree is a binary tree in which every level of the tree contains all possible nodes. Implement the follow

6. Add the following method to the LinkedBinaryTree class. def preorder with stack Returns a generator function that iterates

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 recursive function that determines whether or not a value exists in a LinkedBinaryTree. Your function should take two parameters, a root node and a val, and return True if val exists or False if not. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function. def binary tree contains froot, val) Returns True if val exists in the binary tree and false if not''
Write a function that will invert a LinkedBinaryTree in-place (no new nodes are created. You are simply changing the left and right references.) 3. You will write two implementations, one recursive, and one non recursive Both functions will be given a parameter, root, which is the root node of the binary tree. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function. def invert binary treel (root) Inverts the binary tree using recursion def invert binary tree2 (root) .." Inverte the binasy tree vithout recursion .. Hint: For the non recursive implementation, you may want to use the breadth-first generator ex) will become 6 6
4. A full binary tree (or proper binary tree) is a binary tree in which every node in the tree has 2 or 0 children. Implement the following function, which takes in the root node of a LinkedBinaryTree and determines whether or not is is a full tree. Ihis function should be recursive You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function ǐs def full(root); - Returns True if the Binary Tree is ful1 and false if not ex)
5. A complete binary tree is a binary tree in which every level of the tree contains all possible nodes. Implement the following function, which takes in the root node of a LinkedBinaryTree and determines whether or not is is a full tree. This function should be iterative. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function. def is complete (root): Returns True if the Binary Tree is complete and false if not Hint: For the non recursive implementation, you should use the breadth-first search with an ArrayQueue ex) 10) 13 14) 5127)815 1 4
6. Add the following method to the LinkedBinaryTree class. def preorder with stack Returns a generator function that iterates through the tree using the preorder traversal''' The method is a generator function that when called, will iterate through the binary tree using preorder traversal without recursion. You will use one ArrayStack and Θ(1) extra space. Preorder is as followed: Data, Left, Right. When you're tracing the preorder traversal of the binary tree, imagine how you would place the nodes in the stack, and when you would pop the nodes from the stack. Think of recursion and the call stack! ex) Given the following code using the tree example below: t is a LinkedBinaryTree for item in t.preorder with stack): print() You should expect the following output 2 7 2 6 5 11 5 9 4 print(item, end 2 6 11) 4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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))

# Function to find sum of all the even element 2 def binary_tree_even_sum(root): # Base case: If the root is empty, then retu

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.

Add a comment
Know the answer?
Add Answer to:
1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...
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
  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    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...

    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...

    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...

    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);

  • 2. Write a recursive algorithm which computes the number of nodes in a general tree. 3....

    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...

  • You will be given several strings full of different keyboard characters, some of which are letters...

    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 de...

    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...

    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...

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

  • Using Python to solve the question def _put(self,key,val,currentNode): if key < currentNode.key: if currentNode.hasLeftChild(): self._put(key,val,currentNode.leftChild) else: currentNode.leftChild...

    Using Python to solve the question def _put(self,key,val,currentNode): if key < currentNode.key: if currentNode.hasLeftChild(): self._put(key,val,currentNode.leftChild) else: currentNode.leftChild = TreeNode(key,val,parent=currentNode) elif key > currentNode.key: if currentNode.hasRightChild(): self._put(key,val,currentNode.rightChild) else: currentNode.rightChild = TreeNode(key,val,parent=currentNode) Problem 5 Binary Search Trees (30 points) Modify the books implementation of the binary search tree described in chapters 6.10-6.13 so that it handles duplicate keys properly. The BinarySearchTree class implements a binary search tree where every node of the tree contains a key and a corresponding data item (the...

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