Question: C++ code code from assignment 7 -----------------------Code-------------------------- #include using namespace std; struct item{ int id; int value; string name; static int currId; item(){ } item(int
C++ code

code from assignment 7
-----------------------Code--------------------------
#include
using namespace std;
struct item{
int id;
int value;
string name;
static int currId;
item(){
}
item(int value){
this->value = value;
this->id = ++currId;
}
};
int item::currId = -1;
struct Node{
Node* parent;
Node* leftchild;
Node* rightchild;
item* it;
Node(int value){
it = new item(value);
}
};
struct arrayBinaryTree{
item** A; //pointer to array starting index
int size = 3;
int currInd = 0;
arrayBinaryTree(){
A = (item**)malloc(sizeof(item*)*size);
for(int i=0;i A[i] = new item(); A[i]->id = -1; } } // doubles the size of array void increaseSize(){ int i = size; size += size+1; A = (item**)realloc(A,sizeof(item*)*size); for(;i A[i] = new item(); A[i]->id = -1; } } void insertNode(int value){ // check if array is already full if(currInd==size) increaseSize(); A[currInd++] = new item(value); } // Inorder(0) will start in order traversal // from head which is at index 0 void Inorder(int i){ // Check if i is valid index and is not empty if(i>=size || A[i]->id==-1) return; int left = 2*i+1; int right = left+1; Inorder(left); cout value Inorder(right); } // Preorder(0) will start pre order traversal // from head which is at index 0 void Preorder(int i){ // Check if i is valid index and is not empty if(i>=size || A[i]->id==-1) return; int left = 2*i+1; int right = left+1; cout value Preorder(left); Preorder(right); } // Postorder(0) will start post order traversal // from head which is at index 0 void Postorder(int i){ // Check if i is valid index and is not empty if(i>=size || A[i]->id==-1) return; int left = 2*i+1; int right = left+1; Postorder(left); Postorder(right); cout value } // recursive implementation of bfs which takes in // leftmost and rightmost index as parameter // called as bfs(0,0) void bfs(int l, int r){ // print out all the valid values in current level for(int i=l;i if(A[i]->id!=-1) cout value } // get indices of leftmost and rightmost elements // in the next level int left = 2*l+1; int right = 2*r+2; if(left>=size) return; // recursively call bfs on new left and right indexes bfs(left,right); } // print out all traversals of binary tree void traversals(){ cout Inorder(0); cout cout Preorder(0); cout cout Postorder(0); cout cout bfs(0,0); cout } }; int main(){ // initialize new binary tree arrayBinaryTree arrB; int in; // read data till it is available // Ctrl+D to mark end of input data while(cin >> in) arrB.insertNode(in); // print out all traversals of binary tree arrB.traversals(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
