A binary tree is a complete binary tree if all the internal nodes (including the root node) have exactly two child nodes and all the leaf nodes are at level 'h' corresponding to the height of the tree.
Consider the code for the binary tree given to you for this question. Add code in the blank space provided for the member function checkCompleteBinaryTree( ) in the BinaryTree class. This member function should check whether the binary tree input by the user (in the form of the edge information stored in a file) is a complete binary tree.
To test your code, come up with two binary trees of at least 12 vertices: one, a complete binary tree and another, a binary tree that is not a complete tree.
Prepare the input file for the two binary trees and input them to the code for this question. Capture the screenshots of the outputs.
Give the entire code (including the implementation of the function to check whether a binary tree is complete)
Code Given:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring> // for string tokenizer and c-style string processing
#include <algorithm> // max function
using namespace std;
class BTNode{
private:
int nodeid;
int data;
int levelNum;
BTNode* leftChildPtr;
BTNode* rightChildPtr;
public:
BTNode(){}
void setNodeId(int id){
nodeid = id;
}
int getNodeId(){
return nodeid;
}
void setData(int d){
data = d;
}
int getData(){
return data;
}
void setLevelNum(int level){
levelNum = level;
}
int getLevelNum(){
return levelNum;
}
void setLeftChildPtr(BTNode* ptr){
leftChildPtr = ptr;
}
void setRightChildPtr(BTNode* ptr){
rightChildPtr = ptr;
}
BTNode* getLeftChildPtr(){
return leftChildPtr;
}
BTNode* getRightChildPtr(){
return rightChildPtr;
}
int getLeftChildID(){
if (leftChildPtr == 0)
return -1;
return leftChildPtr->getNodeId();
}
int getRightChildID(){
if (rightChildPtr == 0)
return -1;
return rightChildPtr->getNodeId();
}
};
class Node{
private:
int data;
Node* nextNodePtr;
Node* prevNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return data;
}
void setNextNodePtr(Node* nodePtr){
nextNodePtr = nodePtr;
}
Node* getNextNodePtr(){
return nextNodePtr;
}
void setPrevNodePtr(Node* nodePtr){
prevNodePtr = nodePtr;
}
Node* getPrevNodePtr(){
return prevNodePtr;
}
};
class Queue{
private:
Node* headPtr;
Node* tailPtr;
public:
Queue(){
headPtr = new Node();
tailPtr = new Node();
headPtr->setNextNodePtr(0);
tailPtr->setPrevNodePtr(0);
}
Node* getHeadPtr(){
return headPtr;
}
Node* getTailPtr(){
return tailPtr;
}
bool isEmpty(){
if (headPtr->getNextNodePtr() == 0)
return true;
return false;
}
void enqueue(int data){
Node* newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(0);
Node* lastNodePtr = tailPtr->getPrevNodePtr();
if (lastNodePtr == 0){
headPtr->setNextNodePtr(newNodePtr);
newNodePtr->setPrevNodePtr(0);
}
else{
lastNodePtr->setNextNodePtr(newNodePtr);
newNodePtr->setPrevNodePtr(lastNodePtr);
}
tailPtr->setPrevNodePtr(newNodePtr);
}
int dequeue(){
Node* firstNodePtr = headPtr->getNextNodePtr();
Node* nextNodePtr = 0;
int poppedData = -100000; //empty queue
if (firstNodePtr != 0){
nextNodePtr = firstNodePtr->getNextNodePtr();
poppedData = firstNodePtr->getData();
}
else
return poppedData;
if (nextNodePtr != 0){
nextNodePtr->setPrevNodePtr(0);
headPtr->setNextNodePtr(nextNodePtr);
}
else{
headPtr->setNextNodePtr(0);
tailPtr->setPrevNodePtr(0);
}
return poppedData;
}
int peek(){
Node* firstNodePtr = headPtr->getNextNodePtr();
if (firstNodePtr != 0)
return firstNodePtr->getData();
else
return -100000; //empty queue
}
};
class BinaryTree{
private:
int numNodes;
BTNode* arrayOfBTNodes;
public:
BinaryTree(int n){
numNodes = n;
arrayOfBTNodes = new BTNode[numNodes];
for (int id = 0; id < numNodes; id++){
arrayOfBTNodes[id].setNodeId(id);
arrayOfBTNodes[id].setLevelNum(-1);
arrayOfBTNodes[id].setLeftChildPtr(0);
arrayOfBTNodes[id].setRightChildPtr(0);
}
}
void setLeftLink(int upstreamNodeID, int downstreamNodeID){
arrayOfBTNodes[upstreamNodeID].setLeftChildPtr(&arrayOfBTNodes[downstreamNodeID]);
}
void setRightLink(int upstreamNodeID, int downstreamNodeID){
arrayOfBTNodes[upstreamNodeID].setRightChildPtr(&arrayOfBTNodes[downstreamNodeID]);
}
void printLeafNodes(){
for (int id = 0; id < numNodes; id++){
if (arrayOfBTNodes[id].getLeftChildPtr() == 0 && arrayOfBTNodes[id].getRightChildPtr() == 0)
cout << id << " ";
}
cout << endl;
}
bool isLeafNode(int nodeid){
if (arrayOfBTNodes[nodeid].getLeftChildPtr() == 0 && arrayOfBTNodes[nodeid].getRightChildPtr() == 0)
return true;
return false;
}
int getNodeHeight(int nodeid){
if (nodeid == -1 || isLeafNode(nodeid) )
return 0;
int leftChildID = arrayOfBTNodes[nodeid].getLeftChildID(); // -1 if not exist
int rightChildID = arrayOfBTNodes[nodeid].getRightChildID(); // -1 if not exist
return max(getNodeHeight(leftChildID), getNodeHeight(rightChildID)) + 1;
}
int getTreeHeight(){
return getNodeHeight(0);
}
void assignLevelNumbers(){
Queue queue;
queue.enqueue(0);
arrayOfBTNodes[0].setLevelNum(0);
while (!queue.isEmpty()){
int firstNodeInQueue = queue.dequeue();
int leftChildID = arrayOfBTNodes[firstNodeInQueue].getLeftChildID();
if (leftChildID != -1){
queue.enqueue(leftChildID);
arrayOfBTNodes[leftChildID].setLevelNum(arrayOfBTNodes[firstNodeInQueue].getLevelNum()+1);
}
int rightChildID = arrayOfBTNodes[firstNodeInQueue].getRightChildID();
if (rightChildID != -1){
queue.enqueue(rightChildID);
arrayOfBTNodes[rightChildID].setLevelNum(arrayOfBTNodes[firstNodeInQueue].getLevelNum()+1);
}
}
}
int getDepth(int nodeid){
return arrayOfBTNodes[nodeid].getLevelNum();
}
bool checkCompleteBinaryTree(){
// Need to do three things
// (1) Get the height of the tree
// (2) Assign the level numbers for each node
// (3) If a node is not a leaf node (i.e., an internal node), check if its has two child nodes
// If a node is a leaf node, check if its level number equals the height of the tree
}
};
int main(){
string filename;
cout << "Enter a file name: ";
cin >> filename;
int numNodes;
cout << "Enter number of nodes: ";
cin >> numNodes;
BinaryTree binaryTree(numNodes);
ifstream fileReader(filename);
if (!fileReader){
cout << "File cannot be opened!! ";
return 0;
}
int numCharsPerLine = 10;
char *line = new char[numCharsPerLine];
// '10' is the maximum number of characters per line
fileReader.getline(line, numCharsPerLine, '\n');
// '\n' is the delimiting character to stop reading the line
while (fileReader){
char* cptr = strtok(line, ",: ");
string upstreamNodeToken(cptr);
int upstreamNodeID = stoi(upstreamNodeToken);
cptr = strtok(NULL, ",: ");
int childIndex = 0; // 0 for left child; 1 for right child
while (cptr != 0){
string downstreamNodeToken(cptr);
int downstreamNodeID = stoi(downstreamNodeToken);
if (childIndex == 0 && downstreamNodeID != -1)
binaryTree.setLeftLink(upstreamNodeID, downstreamNodeID);
if (childIndex == 1 && downstreamNodeID != -1)
binaryTree.setRightLink(upstreamNodeID, downstreamNodeID);
cptr = strtok(NULL, ",: ");
childIndex++;
}
fileReader.getline(line, numCharsPerLine, '\n');
}
if (binaryTree.checkCompleteBinaryTree())
cout << "The binary tree is a complete binary tree " << endl;
else
cout << "The binary tree is not a complete binary tree " << endl;
system("pause");
return 0;
}
Please find the program below, copy and paste the program in any C++ IDE:
Step 1. compose the file which has contents in the format below:
0,1,2
1,3,4
2,5,6
3,7,8
4,9,10
5,11,12
6,13,14
Step 2. Run the program and provide:
1. the filename as input when prompt
2. the number of nodes, the example here has 15 nodes so provide 15
See the output, this example is a complete Binary Tree, you can change the file content to get an incomplete Binary Tree.
Program starts here:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring> // for string tokenizer and c-style string processing
#include <algorithm> // max function
using namespace std;
class BTNode {
private:
int nodeid;
int data;
int levelNum;
BTNode *leftChildPtr;
BTNode *rightChildPtr;
public:
BTNode() {
}
void setNodeId(int id) {
nodeid = id;
}
int getNodeId() {
return nodeid;
}
void setData(int d) {
data = d;
}
int getData() {
return data;
}
void setLevelNum(int level) {
levelNum = level;
}
int getLevelNum() {
return levelNum;
}
void setLeftChildPtr(BTNode *ptr) {
leftChildPtr = ptr;
}
void setRightChildPtr(BTNode *ptr) {
rightChildPtr = ptr;
}
BTNode* getLeftChildPtr() {
return leftChildPtr;
}
BTNode* getRightChildPtr() {
return rightChildPtr;
}
int getLeftChildID() {
if (leftChildPtr == 0)
return -1;
return leftChildPtr->getNodeId();
}
int getRightChildID() {
if (rightChildPtr == 0)
return -1;
return rightChildPtr->getNodeId();
}
};
class Node {
private:
int data;
Node *nextNodePtr;
Node *prevNodePtr;
public:
Node() {
}
void setData(int d) {
data = d;
}
int getData() {
return data;
}
void setNextNodePtr(Node *nodePtr) {
nextNodePtr = nodePtr;
}
Node* getNextNodePtr() {
return nextNodePtr;
}
void setPrevNodePtr(Node *nodePtr) {
prevNodePtr = nodePtr;
}
Node* getPrevNodePtr() {
return prevNodePtr;
}
};
class Queue {
private:
Node *headPtr;
Node *tailPtr;
public:
Queue() {
headPtr = new Node();
tailPtr = new Node();
headPtr->setNextNodePtr(0);
tailPtr->setPrevNodePtr(0);
}
Node* getHeadPtr() {
return headPtr;
}
Node* getTailPtr() {
return tailPtr;
}
bool isEmpty() {
if (headPtr->getNextNodePtr() == 0)
return true;
return false;
}
void enqueue(int data) {
Node *newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(0);
Node *lastNodePtr = tailPtr->getPrevNodePtr();
if (lastNodePtr == 0) {
headPtr->setNextNodePtr(newNodePtr);
newNodePtr->setPrevNodePtr(0);
}
else {
lastNodePtr->setNextNodePtr(newNodePtr);
newNodePtr->setPrevNodePtr(lastNodePtr);
}
tailPtr->setPrevNodePtr(newNodePtr);
}
int dequeue() {
Node *firstNodePtr = headPtr->getNextNodePtr();
Node *nextNodePtr = 0;
int poppedData = -100000; //empty queue
if (firstNodePtr != 0) {
nextNodePtr = firstNodePtr->getNextNodePtr();
poppedData = firstNodePtr->getData();
}
else
return poppedData;
if (nextNodePtr != 0) {
nextNodePtr->setPrevNodePtr(0);
headPtr->setNextNodePtr(nextNodePtr);
}
else {
headPtr->setNextNodePtr(0);
tailPtr->setPrevNodePtr(0);
}
return poppedData;
}
int peek() {
Node *firstNodePtr = headPtr->getNextNodePtr();
if (firstNodePtr != 0)
return firstNodePtr->getData();
else
return -100000; //empty queue
}
};
class BinaryTree {
private:
int numNodes;
BTNode *arrayOfBTNodes;
public:
BinaryTree(int n) {
numNodes = n;
arrayOfBTNodes = new BTNode[numNodes];
for (int id = 0; id < numNodes; id++) {
arrayOfBTNodes[id].setNodeId(id);
arrayOfBTNodes[id].setLevelNum(-1);
arrayOfBTNodes[id].setLeftChildPtr(0);
arrayOfBTNodes[id].setRightChildPtr(0);
}
}
void setLeftLink(int upstreamNodeID, int downstreamNodeID) {
arrayOfBTNodes[upstreamNodeID].setLeftChildPtr(
&arrayOfBTNodes[downstreamNodeID]);
}
void setRightLink(int upstreamNodeID, int downstreamNodeID) {
arrayOfBTNodes[upstreamNodeID].setRightChildPtr(
&arrayOfBTNodes[downstreamNodeID]);
}
void printLeafNodes() {
for (int id = 0; id < numNodes; id++) {
if
(arrayOfBTNodes[id].getLeftChildPtr() == 0
&&
arrayOfBTNodes[id].getRightChildPtr() == 0)
cout << id << " ";
}
cout << endl;
}
bool isLeafNode(int nodeid) {
if
(arrayOfBTNodes[nodeid].getLeftChildPtr() == 0
&&
arrayOfBTNodes[nodeid].getRightChildPtr() == 0)
return true;
return false;
}
int getNodeHeight(int nodeid) {
if (nodeid == -1 || isLeafNode(nodeid))
return 0;
int leftChildID = arrayOfBTNodes[nodeid].getLeftChildID(); // -1 if not exist
int rightChildID = arrayOfBTNodes[nodeid].getRightChildID(); // -1 if not exist
return max(getNodeHeight(leftChildID), getNodeHeight(rightChildID)) + 1;
}
int getTreeHeight() {
return getNodeHeight(0);
}
void assignLevelNumbers() {
Queue queue;
queue.enqueue(0);
arrayOfBTNodes[0].setLevelNum(0);
while (!queue.isEmpty()) {
int firstNodeInQueue = queue.dequeue();
int leftChildID = arrayOfBTNodes[firstNodeInQueue].getLeftChildID();
if (leftChildID != -1) {
queue.enqueue(leftChildID);
arrayOfBTNodes[leftChildID].setLevelNum(
arrayOfBTNodes[firstNodeInQueue].getLevelNum() + 1);
}
int
rightChildID =
arrayOfBTNodes[firstNodeInQueue].getRightChildID();
if (rightChildID != -1) {
queue.enqueue(rightChildID);
arrayOfBTNodes[rightChildID].setLevelNum(
arrayOfBTNodes[firstNodeInQueue].getLevelNum() + 1);
}
}
}
int getDepth(int nodeid) {
return arrayOfBTNodes[nodeid].getLevelNum();
}
bool checkCompleteBinaryTree() {
int height = getTreeHeight();
assignLevelNumbers();
for (int i = 0; i < numNodes;
i++) {
if
(isLeafNode(arrayOfBTNodes[i].getNodeId())) {
if (arrayOfBTNodes[i].getLevelNum() != height)
{
return false;
}
} else {
if (arrayOfBTNodes[i].getLeftChildID() ==
-1
||
arrayOfBTNodes[i].getRightChildID() == -1) {
return false;
}
}
}
return true;
}
};
int main() {
string filename;
cout << "Enter a file name: ";
cin >> filename;
int numNodes;
cout << "Enter number of nodes: ";
cin >> numNodes;
BinaryTree binaryTree(numNodes);
ifstream fileReader(filename);
if (!fileReader) {
cout << "File cannot be opened!! ";
return 0;
}
int numCharsPerLine = 10;
char *line = new char[numCharsPerLine];
// '10' is the maximum number of characters per line
fileReader.getline(line, numCharsPerLine, '\n');
// '\n' is the delimiting character to stop reading the line
while (fileReader) {
char *cptr = strtok(line, ",: ");
string upstreamNodeToken(cptr);
int upstreamNodeID = stoi(upstreamNodeToken);
cptr = strtok(NULL, ",: ");
int childIndex = 0; // 0 for left child; 1 for right child
while (cptr != 0) {
string downstreamNodeToken(cptr);
int downstreamNodeID = stoi(downstreamNodeToken);
if (childIndex == 0 && downstreamNodeID != -1)
binaryTree.setLeftLink(upstreamNodeID, downstreamNodeID);
if (childIndex == 1 && downstreamNodeID != -1)
binaryTree.setRightLink(upstreamNodeID, downstreamNodeID);
cptr = strtok(NULL, ",: ");
childIndex++;
}
fileReader.getline(line, numCharsPerLine, '\n');
}
if (binaryTree.checkCompleteBinaryTree())
cout << "The binary tree is a complete binary tree " << endl;
else
cout << "The binary tree is not a complete binary tree " << endl;
system("pause");
return 0;
}
Program screenshot of the method checkCompleteBinaryTree:

Screenshot of the file content along with the output of and in-complete Binary tree:

Screenshot of the file content along with the output of a complete Binary tree:

Please provide a feedback by clicking on the feedback button
TJI 432 - 433 bool checkCompleteBinaryTree() { int height = getTreeHeight(); assignLevel Numbers(); for (int i = 0; i < numNodes; i++) { if (isLeaf Node(arrayOfBTNodes[i].getNodeId())) { if (arrayOfBTNodes[i].getLevel Num() != height) { return false; 435 436 437 438 439 440- 441 442 443 444 445 446 447 448 449 450 } else { if (arrayOfBTNodes[i].getLeftChildIDO) == -1 || arrayOfBTNodes[i].getRightChildID() == -1) { return false; return true;
main.cpp testfile.txt 1 0,1,2 2 1,3,4 3 2,5,6 4 3,7,8 5 4,9,10 6 5,11,12 7 6,13 input Enter a file name: testfile.txt Enter number of nodes: 14 The binary tree is not a complete binary tree sh: 1: pause: not found ... Program finished with exit code o Press ENTER to exit consa
main.cpp testfile.txt 1 0,1,2 2 1,3,4 3 2,5,6 4 3,7,8 5 4,9,10 6 5,11,12 7 6,13,14 input Enter a file name: testfile.txt Enter number of nodes: 15 The binary tree is a complete binary tree sh: 1: pause: not found - ... Program finished with exit code o Press ENTER to exit console.
A binary tree is a complete binary tree if all the internal nodes (including the root...
You are given the code for a singly linked list-based implementation of the List ADT. You will add a function called isUnique( ) to the List class such that the function will return true if all the integer elements in the List are unique and return false otherwise. The main function is written in such a way that it will create 1000 Lists (each of size listSize) of random integers in a range [1...maxValue] and will call the isUnique function...
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Node{
private:
int data;
Node* nextNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return
data;
}
void setNextNodePtr(Node*
nodePtr){
nextNodePtr = nodePtr;
}
...
PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...
in
c++ please
program for this code
#include <iostream>
#include <fstream>
#include <string>
#include <cstring> // for string tokenizer and c-style
string processing
#include <algorithm> // max function
#include <stdlib.h>
#include <time.h>
using namespace std;
// Extend the code here as needed
class BTNode{
private:
int nodeid;
int data;
int levelNum;
BTNode* leftChildPtr;
BTNode* rightChildPtr;
public:
BTNode(){}
void setNodeId(int id){
nodeid = id;
}
int getNodeId(){
return nodeid;
}
void setData(int d){
data = d;
}
int getData(){
return data;...
In this assignment, you will use a Hashtable to determine the
common elements in all the lists. If an element appears more than
once in one or more lists, the algorithm should capture the
instances the element is present in all the lists.
You are given a code that implements a Hashtable as an array of
linked lists. You are also given a main function that will create
an array of Lists using the input variable values that you enter....
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...
Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....
Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...