Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7636415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:40:26+00:00 2026-05-31T07:40:26+00:00

I have to make an implementation of the following class in C++: struct Node

  • 0

I have to make an implementation of the following class in C++:

struct Node {
  Node* parent;
  Node* left;
  Node* right;
  std::string name;
};

class BinaryTree {
 public:
  BinaryTree();
  std::string ToString() const;  
  void RotateLeft(Node* node);
  void RotateRight(Node* node);
  void MakePerfect();
 private:
  void CreateVine();
  void MakePerfectFromVine();
 private:
  Node *root;
};

This class implements the DSW algorithm for making a Binary tree perfect (all levels are full, except the last one)

I have done an implementation which works, I tested it and everything seem fine but I had to change the rotate functions thus:

  void RotateLeft(Node*& node);
  void RotateRight(Node*& node);

Unfortunately the person who set me this task said that I’m forbidden to change them. But the unaltered functions don’t work. I use them in CreateVine() and MakePerfectFromVine():

void CreateVine() {
    Node *current = root;
    while(current != NULL)
        if(current->left != NULL){
            current=current->left;
            rotateRight(current);               
        }
        else
            current = current->right;

}

In this fragment the pointer current points to a node in the tree. After the node is rotated, it will have changed its parent and/or left and/or right child, but current will point to the old node with the old information. In order for the changes made in the rotate function to be reflected outside of the rotate function, I have to use reference-to-pointer, pointer-to-pointer or the function itself has to return the node;

The only way I can think of to fix this problem is if I know that the binary tree doesn’t allow duplicate elements, after I rotate the node I search for its string value and set current to point to it, but I’m not sure if the binary tree doesn’t allow duplicate elements and that would increase the time complexity of the algorithm.

Is there any way of avoiding the use of reference-to-pointer and pointer-to-pointer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:40:28+00:00Added an answer on May 31, 2026 at 7:40 am

    Excluding the root of the tree, for which you might have to think something else, the pointer to a given node inside the tree can be obtained with an extra level of indirection:

    Node **pp;
    if (node->parent->left == node)
       pp = &node->parent->left;
    else
       pp = &node->parent->right;
    

    So you can actually maintain your algorithm and just resolve the reference doing this inside the function. Note that this is a sketch, you will have to check for the root (parent==0) and operate differently there

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
I have a class similar to the following: class SomeClass { public: template<typename... Args>
Let's say I have the following two classes: package example.model; public class Model {
I have the following code: #include <iostream> class Grandma { public: virtual void foo()
I have the following class public class classB : classA which contains function dostuff
I have an extension method with the following signature (in BuildServerExtensions class):: public static
I have the following POCO Class with its Repository Pattern Implementation. If my model
I have the following implementation of a clone_ptr in an attempt to make safe
I have to make a rudimentary FSM in a class, and am writing it
How would I make the following calculator Class work?: //Calculator.h #import <Cocoa/Cocoa.h> @interface Calculator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.