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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:16:46+00:00 2026-05-16T11:16:46+00:00

I have a binary tree of some shape . I want to Convert it

  • 0

I have a binary tree of some shape. I want to Convert it to BST search tree of same shape. Is it possible?

I tried methods like –

  • Do In-order traversal of Binary Tree & put contents into an array. Then map this into a BST keeping in mind the condition (left val <= root <= right val). This works for some cases but faile for others.

P.S.: I had a look at this – Binary Trees question. Checking for similar shape. But It’s easy to compare 2 BST’s for similarity in shape.

  • 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-16T11:16:46+00:00Added an answer on May 16, 2026 at 11:16 am

    The short answer is: you can’t. A BST requires that the nodes follow the rule left <= current < right. In the example you linked: http://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg, if you try and build a BST with the same shap you’ll find that you can’t.

    However if you want to stretch the definition of a BST so that it allows left <= current <= right (notice that here current <= right is allowed, as apposed to the stricter definition) you can. Sort all the elements and stick them in an array. Now do an in-order traversal, replacing the values at nodes with each element in your array. Here’s some pseudo code:

    // t is your non-BST tree, a is an array containing the sorted elements of t, i is the current index into a
    index i = 0
    create_bst(Tree t, Array a)
    {
      if(t is NIL)
        return;
      create_bst(t->left, a)
      t->data = a[i]
      i++
      create_bst(t->right, a)
    }
    

    The result won’t be a true BST however. If you want a true BST that’s as close to the original shape as possible, then you again put the elements in a sorted array but this time insert them into a BST. The order in which you insert them is defined by the sizes of the subtrees of the original tree. Here’s some pseudo-code:

    // left is initially set to 0
    create_true_bst(Tree t, BST bt, array a, index left)
    {
      index i = left + left_subtree(t)->size
      bt->insert(a[i])
      if(left_subtree(t)->size != 0)
      {
        create_true_bst(t->left, bt, a, left)
        create_true_bst(t->right, bt, a, i + 1)
      }
    }
    

    This won’t guarantee that the shape is the same however.

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

Sidebar

Related Questions

I have been implementing binary tree search algorithm recently in R, and before that
Going through some excercises to hone my binary tree skills, I decided to implement
I have binary data in a file that I can read into a byte
If you have binary strings (literally String objects that contain only 1's and 0's),
I have a binary file that I have to parse and I'm using Python.
I have a binary file - Windows static library (*.lib). Is there a simple
I have a binary field in my database that is hard to describe in
I have 4 binary bits Bit 3 Bit 2 Bit 1 Bit 0 Normally
Say I have a binary file (generated with Java) containing a 32 bit int
I have to read a binary file in a legacy format with Java. In

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.