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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:36:42+00:00 2026-05-14T04:36:42+00:00

How to convert a binary tree to binary search tree in-place, i.e., we cannot

  • 0

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.

  • 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-14T04:36:43+00:00Added an answer on May 14, 2026 at 4:36 am

    You don’t give much to go on, but if the requirement is what I think it is, you have a binary tree already created and sitting in memory, but not sorted (the way you want it to be sorted, anyway).

    I’m assuming that the tree nodes look like

    struct tree_node {
        struct tree_node * left;
        struct tree_node * right;
        data_t data;
    };
    

    I’m also assuming that you can read C

    While we could just sit around wondering why this tree was ever created without having been created in sorted order that doesn’t do us any good, so I’ll ignore it and just deal with sorting it.

    The requirement that no extra space be used is odd. Temporarily there will be extra space, if only on the stack. I’m going to assume that it means that calling malloc or something like that and also that the resulting tree has to use no more memory than the original unsorted tree.

    The first and easiest solution is to do a preorder traversal of the unsorted tree removing each node from that tree and doing a sorted insertion into a new tree. This is O(n+nlog(n)), which is O(nlog(n)).

    If this isn’t what they want and you’re going to have to use rotations and stuff….. that’s horrible!

    I thought that you could do this by doing an odd version of a heap sort, but I ran into problems.
    Another thing that did come to mind, which would be horribly slow, would to do an odd version of bubble sort on the tree.

    For this each node is compared and possibly swapped with each of it’s direct children (and therefore also with its parent) repeatedly until you traverse the tree and don’t find any
    needed swaps. Doing a shaker sort (bubble sort that goes left to right and the right to left) version of this would work best, and after the initial pass you would not need to traverse down subtrees that did not look out of order with respect to it’s parent.

    I’m sure that either this algorthm was thought up by someone else before me and has a cool name that I just don’t know, or that it is fundamentally flawed in some way that I’m not seeing.

    Coming up with the run-time calculations for the second suggestion is a pretty complicated. At first I thought that it would simply be O(n^2), like bubble and shaker sorts, but I can’t satisfy myself that the subtree traversal avoidance might not win enough to make it a little bit better than O(n^2). Essentially bubble and shaker sorts get this optimization too, but only at the ends where total sortedness occurs early and you can chop down the limits. With this tree version you get oppurtunities to possibly avoid chunks in the middle of the set as well. Well, like I said, it’s probably fatally flawed.

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

Sidebar

Related Questions

Given a binary search tree, i need to convert it into a doubly linked
Objective: Convert binary to string Example: 0111010001100101011100110111010001100011011011110110010001100101 -> testCode (without space) I use a
I have a Binary tree for a mathematical expression(infix), i want to convert directly
I tried to convert a List from 3{1{,2{,}},5{4{,},6{,}}} to a Binary Tree like this
Given a Circularly doubly linked list... How can i convert it into Binary Search
Recently I wrote an algorithm to convert an infix expression to a binary tree
Ii have a problem. i have to convert a Binary Tree in its zigZag
I am implementing a Binary Search tree in C++ I have the following code
I have a binary tree of some shape . I want to Convert it
I am trying to convert binary data to pdf417 barcode using pdf417bean class of

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.