I am trying to use the Mbed compiler and Mbed.h library. (PLEASE USE C LANGUAGE
ID: 3817691 • Letter: I
Question
I am trying to use the Mbed compiler and Mbed.h library.
(PLEASE USE C LANGUAGE PREFERRABLY, OR C++)
In one competition, the robot was required to use an infrared phototransistor to find an infrared beacon flashing at 10 kHz and ignore beacons flashing at other frequencies. Assume that a circuit exists using the phototransistor which provides your KL46Z microprocessor with a square wave signal at the frequency of the infrared beacon if it is correctly pointing at a beacon. This signal is available on pin PTA13. You are to write a program, including any necessary subroutines or interrupts, that will update a global variable called "beacon_ok" at least 10 times per second. If the signal frequency is 10 kHz +/- 10%, then beacon_ok should have the value of 1. If the signal frequency is less than 100 Hz, assume that a beacon has not been found and beacon_ok should have the value of 0. If there is any frequency found greater than 100 Hz but not 10 kHz +/- 10%, the value of beacon_ok should be -1.Explanation / Answer
package com.cheggtest;/*
* Java Program to Implement Binary Tree
*/
import java.util.Scanner;
class BTNode {
BTNode leftNode, rightNode;
int data;
/* Constructor */
public BTNode() {
leftNode = null;
rightNode = null;
data = 0;
}
public BTNode(int n) {
leftNode = null;
rightNode = null;
data = n;
}
public void setleftNode(BTNode n) {
leftNode = n;
}
public void setrightNode(BTNode n) {
rightNode = n;
}
public BTNode getleftNode() {
return leftNode;
}
/* Function to get rightNode node */
public BTNode getrightNode() {
return rightNode;
}
}
/* Class BT */
class BT {
private BTNode root;
public BT() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
public void insert(int data) {
root = insert(root, data);
}
/* Function to insert data recursively */
private BTNode insert(BTNode node, int data) {
if (node == null)
node = new BTNode(data);
else {
if (node.getrightNode() == null)
node.rightNode = insert(node.rightNode, data);
else
node.leftNode = insert(node.leftNode, data);
}
return node;
}
/* Function to count number of nodes */
public int countNodes() {
return countNodes(root);
}
/* Function to count number of nodes recursively */
private int countNodes(BTNode r) {
if (r == null)
return 0;
else {
int l = 1;
l += countNodes(r.getleftNode());
l += countNodes(r.getrightNode());
return l;
}
}
/* Function to search for an element */
public boolean search(int val) {
return search(root, val);
}
/* Function to search for an element recursively */
private boolean search(BTNode r, int val) {
if (r.getData() == val)
return true;
if (r.getleftNode() != null)
if (search(r.getleftNode(), val))
return true;
if (r.getrightNode() != null)
if (search(r.getrightNode(), val))
return true;
return false;
}
/* Function for inorder traversal */
public void inorder() {
inorder(root);
}
private void inorder(BTNode r) {
if (r != null) {
inorder(r.getleftNode());
System.out.print(r.getData() + " ");
inorder(r.getrightNode());
}
}
/* Function for preorder traversal */
public void preorder() {
preorder(root);
}
private void preorder(BTNode r) {
if (r != null) {
System.out.print(r.getData() + " ");
preorder(r.getleftNode());
preorder(r.getrightNode());
}
}
/* Function for postorder traversal */
public void postorder() {
postorder(root);
}
private void postorder(BTNode r) {
if (r != null) {
postorder(r.getleftNode());
postorder(r.getrightNode());
System.out.print(r.getData() + " ");
}
}
}
/* Class BinaryTree */
public class BinaryTreeTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/* Creating object of BT */
BT bt = new BT();
/* Perform tree operations */
System.out.println("Binary Tree Test ");
char ch;
do {
System.out.println(" Binary Tree Operations ");
System.out.println("1. insert ");
System.out.println("2. search");
System.out.println("3. count nodes");
System.out.println("4. check empty");
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter integer element to insert");
bt.insert(scan.nextInt());
break;
case 2:
System.out.println("Enter integer element to search");
System.out.println("Search result : "
+ bt.search(scan.nextInt()));
break;
case 3:
System.out.println("Nodes = " + bt.countNodes());
break;
case 4:
System.out.println("Empty status = " + bt.isEmpty());
break;
default:
System.out.println("Wrong Entry ");
break;
}
/* Display tree */
System.out.print(" Post order : ");
bt.postorder();
System.out.print(" Pre order : ");
bt.preorder();
System.out.print(" In order : ");
bt.inorder();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y' || ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.