Please explain this C++ program, it doesn't have to be long as long as you explain the important parts of the code do. You can explain it line by line and I will give you a good rating. Thank you Activity name: Trinode Restructuring

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
100%

Please explain this C++ program, it doesn't have to be long as long as you explain the important parts of the code do. You can explain it line by line and I will give you a good rating. Thank you

Activity name: Trinode Restructuring

#include "node.h"
#include <iostream>
using namespace std;
class BSTree {
    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;
    }

    bool search(node* curr, int num) {
        if (curr == NULL) {
            return false;
        }
        
        if (num == curr->element) {
            return true;
        }

        if (num < curr->element) {
            return search(curr->left, num);
        }
        return search(curr->right, num);
    }

    node* search_node(node* curr, int num) {
        if (num == curr->element) {
            return curr;
        }

        if (num < curr->element) {
            if (curr->left != NULL) {
                return search_node(curr->left, num);
            }
            return curr;
        }
        if (curr->right != NULL) {
            return search_node(curr->right, num);
        }
        return curr;
    }

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

    bool search(int num) {
        return search(root, num);
    }

    bool insert(int num) {
        if (root == NULL) {
            root = create_node(num, NULL);
            size++;
            return true;
        } else {
            node* parent = search_node(root, num);
            if (parent->element != num) {
                node* newest = create_node(num, parent);
                if (parent->element < num) {
                    parent->right = newest;
                } else {
                    parent->left = newest;
                }
                size++;
                return true;
            }
        }
        return false;
    }
    
    // TODO implementation of rotate operation of x where
    //  |
    //  y
    //   \
    //    x <- curr
    void zigleft(node* curr) {
      node* x = curr;
      node* y = curr->parent;
      node* z= y->parent;
      
      y->right = x->left;
      if(x->left!=NULL){
        x->left->parent=y;
      }
      
      if(y!=root){
        x->parent = y->parent;
        z = y->parent;
        if(y==z->right){
          z->right=x;
        }else{
          z->left = x;
        }
      } else {
        x->parent = NULL;
        root = x;
      }
      y->parent = x;
      x->left = y;
     
    }
    
    // TODO implementation of rotate operation of x where
    //   |
    //   y
    //  /
    // x <- curr
    void zigright(node* curr) {
      node* x = curr;
      node* y= curr->parent;
      
      y->left = x->right;
      if(x->right!=NULL){
        x->right->parent = y;
      }
      
      if(y!=root){
        curr->parent = y->parent;
        node* z = y->parent;
        if(y==z->right){
          z->right=curr;
        }else{
          z->left=curr;
        }
      }else{
        curr->parent=NULL;      
        root=curr;
      }
      
      y->parent = x;
      x->right = y;
    }
    
    bool restructure(int child) {
      node* curr = search_node(root, child);
      node* par = curr->parent;
      if (!par) {
        return false;
      }
      bool ptoc_right = false;
      if (par->right == curr) {
        ptoc_right = true;
      }
      
      node* gp = par->parent;
      if (!gp) {
        // y <- root
        //  \
        //   x
        if (ptoc_right) {
          // TODO call to either zigleft or zigright
          zigleft(curr);
        } 
        //   y <- root
        //  /
        // x
        else {
          // TODO call to either zigleft or zigright
          zigright(curr);
        }
        return true;
      }
      
      bool gtop_right = false;
      if (gp->right == par) {
        gtop_right = true;
      }
      
      // z
      //  \
      //   y
      //    \
      //     x
      if (gtop_right && ptoc_right) {
        // TODO call to either zigleft or zigright or both
        zigleft(par);
      } 
      
      // z
      //   \
      //     y
      //    /
      //   x
      else if (gtop_right && !ptoc_right) {
        // TODO call to either zigleft or zigright or both
       
        zigright(curr);
         zigleft(curr);
      } 
      
      //     z
      //    /
      //   y
      //  /
      // x
      else if (!gtop_right && !ptoc_right) {
        // TODO call to either zigleft or zigright or both
        zigright(par);
      } 
      
      //      z
      //    /
      //  y
      //   \
      //    x
      else {
        // TODO call to either zigleft or zigright or both
        
        zigleft(curr);
        zigright(curr);
      }
      
      return true;
    }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar 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