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 6753619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:10:59+00:00 2026-05-26T13:10:59+00:00

I am trying to create a custom Binary Search Tree, and I have everything

  • 0

I am trying to create a custom Binary Search Tree, and I have everything done except for the rotate function, which seems to be not moving a node over. The rotate function only gets called when a node is searched and found, and if the node has a right child. For simplicity I will only add the functions that are used in this, to make it shorter:

#include <iostream>

using namespace std;

template <typename T>
class MRBST {
public:
    MRBST();
    ~MRBST();

    void push(const T &);
    bool search(const T &);
    void PrintPreorder();
private:
    struct Node {
        T data;
        Node *left;
        Node *right;

        Node (const T & theData, Node *lt, Node *rt) 
        : data(theData), left(lt), right(rt) {}
    };
    Node *root;

    void push(const T &, Node * &) const;
    void remove(const T &, Node * &) const;
    Node* findMin(Node *t) const {
        if (t == NULL) {
            return NULL;
        }
        if (t->left == NULL) {
            return t;
        }
        return findMin(t->left);
    }
    void preorder(Node * &);
    bool search(const T &, Node *);
    Node* findNode(const T & x, Node * t) {
        if (t == NULL) {
            return NULL;
        }
        if (x < t->data) {
            return findNode(x, t->left);
        } else if (x > t->data) {
            return findNode(x, t->right);
        } else if (x == t->data) {
            return t;
        }
        return NULL;
    }
    void rotate(Node *);
};

template <typename T>
void MRBST<T>::PrintPreorder() {
    preorder(root);
    cout << endl;
}

template <typename T>
void MRBST<T>::preorder(Node * & t) {
    if (t != NULL) {
        cout << t->data << endl;
        preorder(t->left);
        preorder(t->right);
    }
}

template <typename T>
bool MRBST<T>::search(const T & x) {
    if (search(x, root)) {
        Node *temp = findNode(x, root);
        rotate(temp);
        return true;            
    } else {
        return false;
    }
}

template <typename T>
void MRBST<T>::rotate(Node * k1) {
    if (k1->right == NULL) {
        return;
    } else {
        Node *temp = k1->right;
        k1->right = temp->left;
        temp->left = k1;
    }
}


template <typename T>
bool MRBST<T>::search(const T & x, Node *t) {
    if (t == NULL) {
        return false;
    } else if (x < t->data) {
        return  search(x, t->left);
    } else if (x > t->data) {
        return search(x, t->right);
    } else {
        return true;
    }
}

I have a simple testing file that just adds some numbers to the tree, and then searches, followed by a print out in Preordering.

#include <iostream>
#include "MRBST.h"

using namespace std;

int main() {
    MRBST<int> binaryTree;
    binaryTree.push(5);
    binaryTree.push(20);
    binaryTree.push(3);
    binaryTree.push(3);
    binaryTree.push(4);
    binaryTree.push(22);
    binaryTree.push(17);
    binaryTree.push(18);
    binaryTree.push(8);
    binaryTree.push(9);
    binaryTree.push(1);
    binaryTree.PrintPreorder();
    cout << endl;
    binaryTree.search(17);
    binaryTree.PrintPreorder();
    cout << endl;

}

With the output looking a something like this:

5
3
1
4
20
17
8
9
18
22

5
3
1
4
20
17
8
9
22

If anyone could give some insight into what is going wrong with my rotate function, that would be a great help.

  • 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-26T13:10:59+00:00Added an answer on May 26, 2026 at 1:10 pm

    Why are you doing rotate on search? It should be read-only.

    You’re losing the node because of that.

    Look at your rotate:

        Node *temp = k1->right;
        k1->right = temp->left;
        temp->left = k1;
    

    Assume k1=x has right=y and left=z, look step by step:

        Node *temp = k1->right;
    

    temp =k1->right = y, k1 = x, k1->left = z

        k1->right = temp->left;
    

    k1->right = ?, k1->left = z, temp = y

        temp->left = k1;
    

    k1->right = ?, k1->left = z, temp->left = x.

    Now – where did Y go? You lost it.

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

Sidebar

Related Questions

I'm trying to create a custom control - a button - which will have
I am trying to create custom bullet points and it is not working. This
Im trying to create a custom formatter in Symfony 1.4. I have embedded form
I'm trying to create a custom search but getting stuck. What I want is
I'm trying to create custom attributes to my button but I dont know which
I am trying to create a custom event which listens and responses to the
I'm trying to create a custom function that unbinds and then binds an event.
Have you ever seen a good reason to create a custom binary rest protocol
I'm trying to create a custom search interface for Apache Solr using Drupal ,
I am trying to create a custom CStatic control in vc++ and have a

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.