a. Write a code that will add three BCD numbers each of size 3 digits. b. Write
ID: 3597682 • Letter: A
Question
a. Write a code that will add three BCD numbers each of size 3 digits. b. Write a program that will read 10 numbers from location 30H in RAM, find the sum, and store it in location 60H in RAM. Find also the maximum number of the 10-numbers stored in RAM and store it at B register. Write a code to solve the following problem: Suppose I have two switches connected as follow: Switch is connected to port 3, pin 5. Switch 2 is connected to port 3, pin 7. A light bulb is connected to port 1, pin 1. When you turn any switch on, the light bulb should be on; otherwise the light bulb should be off. c.Explanation / Answer
import java.io.*;
import java.util.*;
class BTNode{
private int nodeid;
private int data;
private int levelNum;
private BTNode leftChildPtr;
private BTNode rightChildPtr;
public BTNode(){}
public void setNodeId(int id){
nodeid = id;
}
public int getNodeId(){
return nodeid;
}
public void setData(int d){
data = d;
}
public int getData(){
return data;
}
public void setLevelNum(int level){
levelNum = level;
}
public int getLevelNum(){
return levelNum;
}
public void setLeftChildPtr(BTNode ptr){
leftChildPtr = ptr;
}
public void setRightChildPtr(BTNode ptr){
rightChildPtr = ptr;
}
public BTNode getLeftChildPtr(){
return leftChildPtr;
}
public BTNode getRightChildPtr(){
return rightChildPtr;
}
public int getLeftChildID(){
if (leftChildPtr == null)
return -1;
return leftChildPtr.getNodeId();
}
public int getRightChildID(){
if (rightChildPtr == null)
return -1;
return rightChildPtr.getNodeId();
}
}
class BinaryTree{
private int numNodes;
private BTNode arrayOfBTNodes[];
public BinaryTree(int n){
numNodes = n;
arrayOfBTNodes = new BTNode[numNodes];
for (int id = 0; id < numNodes; id++){
arrayOfBTNodes[id] = new BTNode();
arrayOfBTNodes[id].setNodeId(id);
arrayOfBTNodes[id].setLevelNum(-1);
arrayOfBTNodes[id].setLeftChildPtr(null);
arrayOfBTNodes[id].setRightChildPtr(null);
}
}
public void setLeftLink(int upstreamNodeID, int downstreamNodeID){
arrayOfBTNodes[upstreamNodeID].setLeftChildPtr(arrayOfBTNodes[downstreamNodeID]);
}
public void setRightLink(int upstreamNodeID, int downstreamNodeID){
arrayOfBTNodes[upstreamNodeID].setRightChildPtr(arrayOfBTNodes[downstreamNodeID]);
}
public void printLeafNodes(){
for (int id = 0; id < numNodes; id++){
if (arrayOfBTNodes[id].getLeftChildPtr() == null && arrayOfBTNodes[id].getRightChildPtr() == null)
System.out.print(id + " ");
}
System.out.println();
}
public boolean isLeafNode(int nodeid){
if (arrayOfBTNodes[nodeid].getLeftChildPtr() == null && arrayOfBTNodes[nodeid].getRightChildPtr() == null)
return true;
return false;
}
public int getNodeHeight(int nodeid){
if (nodeid == -1)
return -1;
if (isLeafNode(nodeid) )
return 0;
int leftChildID = arrayOfBTNodes[nodeid].getLeftChildID(); // -1 if not exist
int rightChildID = arrayOfBTNodes[nodeid].getRightChildID(); // -1 if not exist
int heightOfLeftChild = getNodeHeight(leftChildID);
int heightOfRightChild = getNodeHeight(rightChildID);
return Math.max(heightOfLeftChild, heightOfRightChild) + 1;
}
public int getTreeHeight(){
return getNodeHeight(0);
}
public boolean checkHeightBalancedTree(){
// Implement this function to determine whether for each internal node, the absolute difference
// between the height of the left child and the right child is at most 1. If it is true for each internal ndoe, the
// binary tree is said to be height-balanced. Even if one internal node is not height-balanced, the
// whole binary tree is considered not height-balanced.
}
}
class BinaryTreeImplementation{
public static void main(String[] args){
try{
Scanner input = new Scanner(System.in);
String filename;
System.out.print("Enter a file name: ");
filename = input.next();
int numNodes;
System.out.print("Enter number of nodes: ");
numNodes = input.nextInt();
BinaryTree binaryTree = new BinaryTree(numNodes);
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ( (line = br.readLine()) != null){
StringTokenizer stk = new StringTokenizer(line, ",: ");
int upstreamNodeID = Integer.parseInt(stk.nextToken());
int childIndex = 0;
while (stk.hasMoreTokens()){
int downstreamNodeID = Integer.parseInt(stk.nextToken());
if (childIndex == 0 && downstreamNodeID != -1)
binaryTree.setLeftLink(upstreamNodeID, downstreamNodeID);
if (childIndex == 1 && downstreamNodeID != -1)
binaryTree.setRightLink(upstreamNodeID, downstreamNodeID);
childIndex++;
}
}
System.out.println("Tree Height: " + binaryTree.getTreeHeight() );
if (binaryTree.checkHeightBalancedTree())
System.out.println("The tree is height balanced..");
else
System.out.println("The tree is not height balanced..");
}
catch(Exception e){e.printStackTrace();}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.