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

  • Home
  • SEARCH
  • 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 7086623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:34:21+00:00 2026-05-28T07:34:21+00:00

I tried to convert a List from 3{1{,2{,}},5{4{,},6{,}}} to a Binary Tree like this

  • 0

I tried to convert a List from 3{1{,2{,}},5{4{,},6{,}}}
to a Binary Tree like this

               3
           1       5
             2    4  6

I thought it would be easier to use recursion but I get stuck.

public void ListToTree (ArrayList al) {
    Iterator it = al.iterator(); 
    // n is the Tree's root
    BSTnode n = new BSTnode(it.next());
    recurse(al,it,n); 

}

void recurse (ArrayList al, Iterator it, BSTnode n) {
    if(!it.hasNext()) return;
    Object element = it.next();
    if(element=="{"){
            recurse(al,it,n.left());
            return;
    } else if (element==",") {
            recurse(al,it,n.right());
            return;
    } else if (element =="}") {

    }

}

I don’t know how to proceed and was wondering if it’s the right track. Please give me some hints how to solve it. Moreover, I realize I often get stuck on recursive questions. Is it because I always want to break it down? Should I just think top-down and double-check if it’s correct? Thanks in advance !

  • 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-28T07:34:22+00:00Added an answer on May 28, 2026 at 7:34 am

    Firstly: are you bound to that terrible list representation? You can easily build a BST based on the BST rules with this code:

    void insert(Node n, int value) {
         if(n == null) {
               n = new Node(value);
         } else if(value < n.value) {
               if(n.left == null) {
                   n.left = new Node(value);
                   return;
               }
               insert(n.left, value);
         } else if(value > n.value) {
               if(n.right == null) {
                    n.right = new Node(value);
                    return;
               }
               insert(n.right, value);
         }
    }
    

    You really don’t have to pass the iterator. Just use the values from the list. Also it is usually unadvised to use implementation types in method signatures. (i.e. ArrayList -> List).
    Another big mistake here is that you don’t use == for value comparison, that is for reference comparison. Use equals instead, but you should downcast the Object after an instanceof test e.g.:

    if( element instanceof String) {
        String seperator = (String)element;
        if("{".equals(separator))
             //do sth...
    

    Btw the thing you are missing from the code is the actual insertion and the backwards navigation.
    After you found the right subtree by navigating with the {-s and ,-s, check whether the element is an Integer then set it as a value for the current node. Backwards navigation should be in the } branch by either returning one level from the recusion and some tricks or calling the method on the parent of the actual node.

    But I don’t suggest you to follow this direction, it is much easier to just use the values from the list and the simple insertion method.

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

Sidebar

Related Questions

I tried many things but I always get cannot convert string to membershipuser from
I want to convert a primitive to a string, and I tried: myInt.toString(); This
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
I am trying to use JSON.stringify() (from json2.js of json[dot]org ) to convert a
I am trying to translate this: SELECT IDNum, Max(Convert(varchar(20), (TimeOut - TimeIn), 108)) FROM
I've tried OutputDebugString function and most of the time I get error like :
In my interface I have a list of text boxes, something like this :
I want to get all items from a specific list in recurring meeting workspace.
From bool[] to byte[]: Convert bool[] to byte[] But I need to convert 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.