Explain the c++ code below

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

C++ PROGRAMMING
Topic: Binary Search Trees

Explain the c++ code below.: SEE ATTACHED PHOTO FOR THE PROBLEM INSTRUCTIONS 

It doesn't have to be long, as long as you explain what the important parts of the code do. (The code is already implemented and correct, only the explanation needed) 

#include "node.h"
#include <iostream>
using namespace std;
class BTree {
    node* root;
    int size;

    node* create_node(int num, node* parent) {
        node* n = (node*) malloc( sizeof(node) );
        n->element = num;
        n->parent = parent;
        n->right = NULL;
        n->left = NULL;
        return n;
    }

    public:
    BTree() {
        root = NULL;
        size = 0;
    }

    node* left(node* p) {    
        return p->left;
    }

    node* right(node* p) {
        return p->right;
    }

    node* sibling(node* p){
        if(p != root){
            node* P = p->parent;
            if(P->left != NULL && P->right != NULL){
                if(P->left->element == p->element){
                    return P->right;
                }
                if(P->right->element == p->element){
                    return P->left;
                }
            }
        }
        return NULL;
    }

    node* addRoot(int e) {
        if(size != 0){
            cout<<"Error"<<endl;
            return NULL;
        }
        root = create_node(e,NULL);
        size++;
        return root;
    }

    node* addLeft(node* p, int e) {
        if(p->left == NULL){
            node* newLeft = create_node(e,NULL);
            newLeft->parent = p;
            p->left = newLeft;
            size++;
            return p->left;
        }
        cout << "Error" << endl;
        return NULL;
    }

    node* addRight(node* p, int e) {
        
         if(p->right == NULL){
            node* newRight = create_node(e,NULL);
            newRight->parent = p;
            p->right = newRight;
            size++;
            return p->right;
        }
        cout << "Error" << endl;
        return NULL;
        
    }

    int _size() {
        return size;
    }

    bool isEmpty() {
        return (size==0);
    }

    int childCount(node* p) {
        return 0;
    }

    int set(node* p, int e) {
        return 0;
    }

    node* addSibling(node* p, int e) {
        return NULL;
    }

    void clear(){

    }

    void attach(node* p, BTree* t1, BTree* t2) {

    }

    int remove(node* p) {
        return 0;
    }

    // WARNING. Do not modify this method.
    // Doing so will nullify your score for this activity.
    void print() {
        if (isEmpty()) {
            cout << "EMPTY";
            return;
        }
        print_inorder(root);
        cout << endl << "STATUS: " << check_health(root, NULL);
    }


    // WARNING. Do not modify this method.
    // Doing so will nullify your score for this activity.
    void print_inorder(node* curr) {
        if (curr->left != NULL) {
            print_inorder(curr->left);
        }
        cout << curr->element << " ";
        if (curr->right != NULL) {
            print_inorder(curr->right);
        }
    }

    // WARNING. Do not modify this method.
    // Doing so will nullify your score for this activity.
    bool check_health(node* curr, node* parent) {
        bool health = curr->parent == parent;
        if (curr->left != NULL) {
            health &= check_health(curr->left, curr);
        }
        if (curr->right != NULL) {
            health &= check_health(curr->right, curr);
        }
        return health;
    }
};



 

•node* left(node* p): Returns the position of the left child of p (or NULL if p has no left child).
•node* right(node* p): Returns the position of the right child of p (or NULL if p has no right child).
•node* sibling(node* p): Returns the position of the sibling of p (or NULL if p has no sibling).
•node* addRoot(int e): Creates a root for an empty tree, storing e as the element, and returns the position of that root; an error occurs if the tree is not
empty wherein you are going to simply print "Error" with an end line (endl) and return NULL. Do this procedure for all the errors mentioned in this activity.
•node* addLeft(node* p, int e): Creates a left child of position p, storing element e, and returns the position of the new node; an error occurs if p already has
a left child.
•node* addRight(node* p, int e): Creates a right child of position p, storing element e, and returns the position of the new node; an error occurs if p already
has a right child.
•int _size(): Returns the size of the tree.
•bool isEmpty(): Returns true if the tree is empty.
Transcribed Image Text:•node* left(node* p): Returns the position of the left child of p (or NULL if p has no left child). •node* right(node* p): Returns the position of the right child of p (or NULL if p has no right child). •node* sibling(node* p): Returns the position of the sibling of p (or NULL if p has no sibling). •node* addRoot(int e): Creates a root for an empty tree, storing e as the element, and returns the position of that root; an error occurs if the tree is not empty wherein you are going to simply print "Error" with an end line (endl) and return NULL. Do this procedure for all the errors mentioned in this activity. •node* addLeft(node* p, int e): Creates a left child of position p, storing element e, and returns the position of the new node; an error occurs if p already has a left child. •node* addRight(node* p, int e): Creates a right child of position p, storing element e, and returns the position of the new node; an error occurs if p already has a right child. •int _size(): Returns the size of the tree. •bool isEmpty(): Returns true if the tree is empty.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY