0

I am stuck on one question of Data Structure and algorithm. The question is about how to construct a Binary Search Tree (BST) of minimum height from a given BST. For more clarity, here is the link to the question

Convert a normal BST to Balanced BST

Here is my solution:

#include<iostream>
#include<vector>
using namespace std;

struct TreeNode{
    int val;
    TreeNode *left, *right;

    TreeNode(int data)
    {
        this->val = data;
        this->right = NULL;
        this->left = NULL;
    }
};

void inorder_traversal(TreeNode *root, vector<TreeNode*> &v1)// Inorder traversal of tree
{
    if(!root)
        return;
    inorder_traversal(root->left, v1);
    v1.push_back(root);
    inorder_traversal(root->right, v1);
}

TreeNode* conversion(vector<TreeNode*> &v1, int start, int end) //
{
   if(start > end)
       return NULL;
   int mid = (start + end) / 2;
   //-----------Doubt Part----------//
  v1[mid]->left = conversion(v1, start, mid - 1);// I think this part is not working well
  v1[mid]->right = conversion(v1, mid + 1, end);
  return v1[mid];
  //-------------------------------//
}

void solve(TreeNode *root) // main function 
{
    vector<TreeNode*> v1;
    inorder_traversal(root, v1);// function to get the inorder traversal of the given tree
    int n = v1.size();
    cout << v1.size();
    TreeNode *l = conversion(v1, 0, n);// function to convert given tree to min height BST
    for(auto itr : v1)
        cout << itr->val << " ";
}

int height(TreeNode *root) // function to calculate the height of the BST tree
{
    if(!root)
        return 0;
    int hl = height(root->left);
    int hr = height(root->right);
    return 1 + max(hl, hr);
}

int main()
{
    TreeNode *root = new TreeNode(10);
    root->left = new TreeNode(8);
    root->left->left = new TreeNode(7);
    root->left->left->left = new TreeNode(6);
    root->left->left->left->left = new TreeNode(5);
    int height1 = height(root);
    solve(root);
    int height2 = height(root);
    cout << "Initial height before the conversion :" << height1 << "\n";
    cout << "Final height after the conversion :" << height2 << "\n";

    return 0;
}

Basically, I am not getting any output after executing the current code, and I figured that the problem has occurred because of the Doubt part of my code.

Can anyone please help me with this code?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Unrelated to your question, but please [never include that header](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Jul 15 '22 at 11:23
  • More related to your question, have you tried to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your program? For example to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch any possible crashes, and locate when and where in your code it happens? – Some programmer dude Jul 15 '22 at 11:24
  • Use a debugger to pinpoint the problem instead of asking people to find out the problem for you. – Jason Jul 15 '22 at 11:26
  • @Someprogrammerdude I am sure that the conflicting part of my code is the doubt part but I not know how to solve it – Kalash Jain Jul 15 '22 at 11:27
  • 1
    After some recursions, `start` and `end` are both `5` resulting in `mid` also being `5` and `v1[mid]` has undefined behaviour as `v1` only has 5 elements – Alan Birtles Jul 15 '22 at 11:27
  • 4
    By the way, that site (geeksforgeeks), isn't really a good teaching or learning resource, despite what it and others might claim. It teaches bad habits, bad code, and sometimes even *invalid* code. If you really want to learn C++ please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take computer-science classes. – Some programmer dude Jul 15 '22 at 11:29
  • @AlanBirtles thanks for your suggestion . Also can you tell me some good debuggers – Kalash Jain Jul 15 '22 at 11:34
  • @Someprogrammerdude thanks man for your wonderful advice .I will surely try them – Kalash Jain Jul 15 '22 at 11:34
  • On windows I use visual studio, on other platforms lldb or gdb usually via VS code – Alan Birtles Jul 15 '22 at 12:45
  • The traditional method is working perfectly well and is doing exactly what you are telling it to do. It is not the method. – n. m. could be an AI Jul 15 '22 at 19:25

1 Answers1

0

One problem is that you don't know what end means in your code. You need to decide. Is it the index of the last element of the range, or one past that?

If it the index of the last element of the range, then conversion(v1, 0, n) is wrong (should be conversion(v1, 0, n-1)).

If it is one past that, then if (start > end) is wrong (should be if (start >= end)), and conversion(v1, start, mid - 1) is wrong too (left as an exercise).

Note in C++ end by convention means "one past the range". If you want the "last index" meaning, call your indices first and last.

Another problem is that what was the root before the conversion is no longer the root after the conversion. solve has the new root for a brief moment, but then throws it away and returns nothing, so main has the wrong root.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243