(Python)
Write a python function that takes a binary search T and a range [a, b] with a ≤ b, where a,b are two integers, and has to decide whether there are two different keys k1,k2 in T whose sum belongs to the given range [a, b] or not. For instance, if the input to the function is some BST T and the range [−1, 4], the function has to decide whether there are two distinct keys k1 , k2 in T whose sum is in the range [−1, 4], where −1 and 4 are included in the range. As an another example, if the input to the function is some BST T and the range [0, 0], the function has to decide whether or not there are two distinct keys k1 , k2 in T whose sum is 0. The function has to return TRUE if there are two distinct keys whose sum belongs to the given range. And, the function has to return FALSE otherwise. The function has to be started as the following:
def BSTRangeSumSearch(T: BinaryTree, a: int, b: int) -> bool:
Write a python function that takes a binary search T and a range [a, b] with a ≤ b, where a,b are two integers, and has to decide whether there are two different keys k1,k2 in T whose sum belongs to the given range [a, b] or not. For instance, if the input to the function is some BST T and the range [−1, 4], the function has to decide whether there are two distinct keys k1 , k2 in T whose sum is in the range [−1, 4], where −1 and 4 are included in the range. As an another example, if the input to the function is some BST T and the range [0, 0], the function has to decide whether or not there are two distinct keys k1 , k2 in T whose sum is 0. The function has to return TRUE if there are two distinct keys whose sum belongs to the given range. And, the function has to return FALSE otherwise. The function has to be started as the following:
def BSTRangeSumSearch(T: BinaryTree, a: int, b: int) -> bool:
# A binary tree node
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# The function prints all the keys in the gicven range
# [k1..k2]. Assumes that k1 < k2
def Print(root, k1, k2):
# Base Case
if root is None:
return
# Since the desired o/p is sorted, recurse for
left
# subtree first. If root.data is greater than k1,
then
# only we can get o/p keys in left subtree
if k1 < root.data :
Print(root.left, k1, k2)
# If root's data lies in range, then prints root's
data
if k1 <= root.data and k2 >= root.data:
print root.data,
# If root.data is smaller than k2, then only we can
get
# o/p keys in right subtree
if k2 > root.data:
Print(root.right, k1, k2)
# Driver function to test above function
k1 = 10 ; k2 = 25 ;
root = Node(20)
root.left = Node(8)
root.right = Node(22)
root.left.left = Node(4)
root.left.right = Node(12)
Print(root, k1, k2)
Output:
12 20 22
Time Complexity: O(n) where n is the total number of keys in tree.
(Python) Write a python function that takes a binary search T and a range [a, b]...
In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...
1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines whether two distinct elements of the array add up to some specified value, "key". If so, return 1. Otherwise, return 0. The function signature is: int canMakeSum(int *array, int n, int key);
Write a python function called "get_sma", that takes two input arguments, similar to the example provided. First argument is the original prices of type list, and the 2nd input argument being the number of days we want to calculate SMA, like a 8 day sma or 35 day sma. This function should return a new list of sma price, based on the input day range, and should be able to handle any SMA calculation, like: sma200 for 200 days moving...
C++ Binary Search Tree question. I heed help with the level 2
question please, as level 1 is already completed. I will rate the
answer a 100% thumbs up. I really appreciate the help!. Thank
you!
searching.cpp
#include <getopt.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
// global variable for tree operations
// use to control tree maintenance operations
enum Mode { simple, randomised, avl } mode; // tree type
// returns size of tree
//...
C++ Binary Search Tree question. I heed help with the level 2
question please, as level 1 is already completed. I will rate the
answer a 100% thumbs up. I really appreciate the help!. Thank
you!
searching.cpp
#include <getopt.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
// global variable for tree operations
// use to control tree maintenance operations
enum Mode { simple, randomised, avl } mode; // tree type
// returns size of tree
//...
1. Write a Python function that implements the following algorithm that computes the sum of the first n non-negative powers of 2: 20 + 21 + ... + 2n-1, where n ≥ 0: 1. Input n (assume n ≥ 0) 2. Set sum to 1 3. Set value to 2 4. Do the following n-1 times: a. Add value to sum b. Multiply value by 2 5. Output sum 2.Use turtle to draw two flags of two countries...
Python: Write a function named "sort_by_average_rating" that takes a list/array of key-value stores as a parameter where each key-value store has keys "ratings", "budget", and "box_office" where budget and box_office are integers and ratings is a list of integers. Sort the input based on the average of the values in "ratings"
1. Write a function that takes as input a directory. The function will search through that directory and its full subdirectory tree, building a count of the files by their extensions. As in the lecture, use a dictionary to keep track of the counts. The return-value of this function will be a dictionary of (key,value) pairs where the key will be the file extension and the value will be the number of files with that extension. For example, for the...
Python Programing : Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a...
Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom. We have defined the following node C++ Node class for you: class Node { public: ...