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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:36:23+00:00 2026-05-27T08:36:23+00:00

var m_root : Node = root private def insert(key: Int, value: Int): Node =

  • 0
  var m_root : Node = root
  private def insert(key: Int, value: Int): Node = {
        if(m_root == null) {
            m_root = Node(key, value, null, null)
        }
        var t : Node = m_root
        var flag : Int = 1
        while (t != null && flag == 1) {
            if(key == t.key) {
                t
            }
            else if(key < t.key) {
                if(t.left == null) {
                    t.left = Node(key, value, null, null)
                    flag = 0
                } else {
                    t = t.left
                }

            } else {
                if(t.right == null) {
                    t.right = Node(key, value, null, null)
                    flag = 0
                } else {
                    t = t.right
                }
            }
        }
      t
 }

I wrote iterative version insert a node to binary search tree. I want to terminate when node is created, but it doesn’t stop, because I think I didn’t assign terminating condition. How to I edit my code to terminate when a node inserted in?

  • 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-27T08:36:24+00:00Added an answer on May 27, 2026 at 8:36 am

    I’m not sure exactly what behaviour you want, but the cause is quite clear.

    Your loop is a while condition, which will loop until t is null. So while t is non-null the loop will continue.

    You only ever assign t to non-null values – in fact you’re specifically checking for the null case and stopping it happening by creating a new node.

    So either you need to reconsider your loop condition, or ensure t does in fact become null in some cases, depending on what your actual algorithm requirements are.

    And since you’re returning t at the bottom, I suggest the while condition is wrong; the only possible way this could terminate is if t is null at this point, so it would be pointless to return this anyway.

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

Sidebar

Related Questions

I'm having trouble accessing the value of the root node of an XML variable
Currently I'm using var x = dict.ContainsKey(key) ? dict[key] : defaultValue I'd like some
I'm doing a check in an iPhone application - int var; if (var !=
I'm running a CentOS 5.0 server with root access. The /var/www folder was originally
I've configured my store as: var store = new Ext.data.JsonStore({ url: 'gridData.php', root: 'movies',
Given this code (C#, .NET 3.5 SP1): var doc = new XmlDocument(); doc.LoadXml(<?xml version=\1.0\?><root>
Based on this , how would you make ignoring parameters more succint? var m
I'm logging output with log = /var/log/mysql/query.log and this works ok, but I only
I'm trying at this: var cur_border = $('id_grid_pic_'+cur_id).attr('border'); alert(cur_border); But the alert sends back
I'm trying to do this: using(var context = new SampleEntities()) { User user =

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.