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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:34:25+00:00 2026-05-14T23:34:25+00:00

Is there a method to build a balanced binary search tree? Example: 1 2

  • 0

Is there a method to build a balanced binary search tree?

Example:

1 2 3 4 5 6 7 8 9

       5
      / \
     3   etc
    / \
   2   4
  /
 1

I’m thinking there is a method to do this, without using the more complex self-balancing trees. Otherwise I can do it on my own, but someone probably have done this already 🙂


Thanks for the answers! This is the final python code:

def _buildTree(self, keys):
    if not keys:
        return None

    middle = len(keys) // 2

    return Node(
        key=keys[middle],
        left=self._buildTree(keys[:middle]),
        right=self._buildTree(keys[middle + 1:])
        )
  • 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-14T23:34:25+00:00Added an answer on May 14, 2026 at 11:34 pm

    For each subtree:

    • Find the middle element of the subtree and put that at the top of the tree.
    • Find all the elements before the middle element and use this algorithm recursively to get the left subtree.
    • Find all the elements after the middle element and use this algorithm recursively to get the right subtree.

    If you sort your elements first (as in your example) finding the middle element of a subtree can be done in constant time.

    This is a simple algorithm for constructing a one-off balanced tree. It is not an algorithm for a self-balancing tree.

    Here is some source code in C# that you can try for yourself:

    public class Program
    {
        class TreeNode
        {
            public int Value;
            public TreeNode Left;
            public TreeNode Right;
        }
    
        TreeNode constructBalancedTree(List<int> values, int min, int max)
        {
            if (min == max)
                return null;
    
            int median = min + (max - min) / 2;
            return new TreeNode
            {
                Value = values[median],
                Left = constructBalancedTree(values, min, median),
                Right = constructBalancedTree(values, median + 1, max)
            };
        }
    
        TreeNode constructBalancedTree(IEnumerable<int> values)
        {
            return constructBalancedTree(
                values.OrderBy(x => x).ToList(), 0, values.Count());
        }
    
        void Run()
        {
            TreeNode balancedTree = constructBalancedTree(Enumerable.Range(1, 9));
            // displayTree(balancedTree); // TODO: implement this!
        }
    
        static void Main(string[] args)
        {
            new Program().Run();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you are looking for KeyedCollection<TKey, TItem>. Create a… May 16, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer $("#create-company").click(function () { $('#popupCreateCompany').dialog('open'); }); $("#create-service_line").click(function () { $('#popupCreateService_Line').dialog('open'); });… May 16, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer No, you shouldn't as you didn't increase retain count of… May 16, 2026 at 9:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there a method in Linq where you can use to build SQL strings
I was looking to create a pre-build method for a web application using SQLMetal
I found some examples about this subject. Some of the examples gived a method
I am attempting to build a string extension method to trim a string to
I'm trying build my application using REST and Spring MVC. For some entities I
I am using Reflection.Emit to build a mathematical expression parser (e.g. 2+2 ). A
One thing I want to do is build a personal database for myself at
As part of one of my projects, there are BeforeBuild tasks that ultimately generate
This is just a question out of curiosity since I have been needing to
i try to build an https client for android and i need do get

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.