remove method for generic type binary search tree result in stack overflow problemconvert Binary tree to Binary Search Tree inplace using CDifference between binary tree and binary search treeFinding if a Binary Tree is a Binary Search TreeBinary Search Tree in C: remove node functionTo find if a tree is a binary search tree or notThreaded Binary Search Trees AdvantageInsertion in Binary Search Tree vs Insertion in Binary TreeFind the smallest element greater than data, or the successor (Binary Search Tree)Binary Tree/ Binary Search TreeBinary Search Tree (BST) successor for nonexistent node

Paradox regarding phase transitions in relativistic systems

How often is duct tape used during crewed space missions?

Is it possible that the shadow of The Moon is a single dot during solar eclipse?

What is the maximum viable speed for a projectile within earth's atmosphere?

How far away from you does grass spread?

What is the word for a person who destroys monuments?

Microservices and Stored Procedures

Could the Orion project pusher plate model be used for asteroid deflection?

SQL Server Always-On Availability Groups Patching

What was the earliest microcomputer Logo language implementation?

Inquiry answerer

Abilities interrupting effects on a cast card

How do you determine which representation of a function to use for Newton's method?

Tips for remembering the order of parameters for ln?

Why are two-stroke engines nearly unheard of in aviation?

Hobby function generators

How long to wait to change jobs after closing on a home

Which block cipher parameters should be kept secret?

Is there an in-universe reason Harry says this or is this simply a Rowling mistake?

Applications of mathematics in clinical setting

Minimum number of lines to draw 111 squares

Problem of Induction: Dissolved

How could artificial intelligence harm us?

EU compensation - fire alarm at the Flight Crew's hotel



remove method for generic type binary search tree result in stack overflow problem


convert Binary tree to Binary Search Tree inplace using CDifference between binary tree and binary search treeFinding if a Binary Tree is a Binary Search TreeBinary Search Tree in C: remove node functionTo find if a tree is a binary search tree or notThreaded Binary Search Trees AdvantageInsertion in Binary Search Tree vs Insertion in Binary TreeFind the smallest element greater than data, or the successor (Binary Search Tree)Binary Tree/ Binary Search TreeBinary Search Tree (BST) successor for nonexistent node






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have come across this lab problem about implementing a remove method for generic type binary search tree.



I have implemented a binary search tree of generic type.



I have learnt the binary search tree remove algorithm, and I try to deal with 3 cases when the parent node has "0 child", " 1 child" or "2 children".



I implement the code to remove a node but keeps resulting in a stack overflow problem, and I cannot find where's wrong in my code.



Any help will be appreciated.



public class BinarySearchTree<T extends Comparable<T>> 

private Node<T> _root; //Root node

public class Node<T extends Comparable<T>>
public T get_value() return _value;

public void set_value(T _value) this._value = _value;

public Node<T> get_left() return _left;

public void set_left(Node<T> _left) this._left = _left;

public Node<T> get_right() return _right;

public void set_right(Node<T> _right) this._right = _right;

public Node<T> get_parent() return _parent;

public void set_parent(Node<T> _parent) this._parent = _parent;

T _value; // Node value
Node<T> _left; // Left child

Node<T> _right; // Right child
Node<T> _parent; // Parent node

public Node(T key, Node<T> parent, Node<T> left, Node<T> right)
_value = key;
_left = left;
_right = right;
_parent = parent;


// Remove a node from the BST
private Node<T> remove(BinarySearchTree<T> bst, Node<T> z)
Node<T> delNode = null;
if(bst._root == null)
delNode = null;

else
Node<T> current = bst._root;
// find the position to delete
while(current!=null)
int compare = z.get_value().compareTo(current.get_value());
if(compare < 0)
current = current.get_left();

if(compare > 0)
current = current.get_right();

else
// if node has two child,replace it with right minimum value
if (current._left!=null && current._right!=null)
delNode = current;
Node<T> successor = minimumKey(current.get_right());
current.set_value(successor.get_value());
remove(successor._value);

if (current._left!=null)
delNode = current;
remove(current._value);
current._left.set_parent(current._parent);

if (current._right!=null)
delNode = current;
remove(current._value);
current._right.set_parent(current._parent);

else
delNode = current;
remove(current._value);







return delNode;


// remove a node value
public void remove(T key)
Node<T> z, node;
if ((z = find(_root, key)) != null)
if ( (node = remove(this, z)) != null)
node = null;

public Node<T> minimumKey(Node<T> current)
while (current._left != null)
current = current._left;

return current;











share|improve this question
































    2















    I have come across this lab problem about implementing a remove method for generic type binary search tree.



    I have implemented a binary search tree of generic type.



    I have learnt the binary search tree remove algorithm, and I try to deal with 3 cases when the parent node has "0 child", " 1 child" or "2 children".



    I implement the code to remove a node but keeps resulting in a stack overflow problem, and I cannot find where's wrong in my code.



    Any help will be appreciated.



    public class BinarySearchTree<T extends Comparable<T>> 

    private Node<T> _root; //Root node

    public class Node<T extends Comparable<T>>
    public T get_value() return _value;

    public void set_value(T _value) this._value = _value;

    public Node<T> get_left() return _left;

    public void set_left(Node<T> _left) this._left = _left;

    public Node<T> get_right() return _right;

    public void set_right(Node<T> _right) this._right = _right;

    public Node<T> get_parent() return _parent;

    public void set_parent(Node<T> _parent) this._parent = _parent;

    T _value; // Node value
    Node<T> _left; // Left child

    Node<T> _right; // Right child
    Node<T> _parent; // Parent node

    public Node(T key, Node<T> parent, Node<T> left, Node<T> right)
    _value = key;
    _left = left;
    _right = right;
    _parent = parent;


    // Remove a node from the BST
    private Node<T> remove(BinarySearchTree<T> bst, Node<T> z)
    Node<T> delNode = null;
    if(bst._root == null)
    delNode = null;

    else
    Node<T> current = bst._root;
    // find the position to delete
    while(current!=null)
    int compare = z.get_value().compareTo(current.get_value());
    if(compare < 0)
    current = current.get_left();

    if(compare > 0)
    current = current.get_right();

    else
    // if node has two child,replace it with right minimum value
    if (current._left!=null && current._right!=null)
    delNode = current;
    Node<T> successor = minimumKey(current.get_right());
    current.set_value(successor.get_value());
    remove(successor._value);

    if (current._left!=null)
    delNode = current;
    remove(current._value);
    current._left.set_parent(current._parent);

    if (current._right!=null)
    delNode = current;
    remove(current._value);
    current._right.set_parent(current._parent);

    else
    delNode = current;
    remove(current._value);







    return delNode;


    // remove a node value
    public void remove(T key)
    Node<T> z, node;
    if ((z = find(_root, key)) != null)
    if ( (node = remove(this, z)) != null)
    node = null;

    public Node<T> minimumKey(Node<T> current)
    while (current._left != null)
    current = current._left;

    return current;











    share|improve this question




























      2












      2








      2


      1






      I have come across this lab problem about implementing a remove method for generic type binary search tree.



      I have implemented a binary search tree of generic type.



      I have learnt the binary search tree remove algorithm, and I try to deal with 3 cases when the parent node has "0 child", " 1 child" or "2 children".



      I implement the code to remove a node but keeps resulting in a stack overflow problem, and I cannot find where's wrong in my code.



      Any help will be appreciated.



      public class BinarySearchTree<T extends Comparable<T>> 

      private Node<T> _root; //Root node

      public class Node<T extends Comparable<T>>
      public T get_value() return _value;

      public void set_value(T _value) this._value = _value;

      public Node<T> get_left() return _left;

      public void set_left(Node<T> _left) this._left = _left;

      public Node<T> get_right() return _right;

      public void set_right(Node<T> _right) this._right = _right;

      public Node<T> get_parent() return _parent;

      public void set_parent(Node<T> _parent) this._parent = _parent;

      T _value; // Node value
      Node<T> _left; // Left child

      Node<T> _right; // Right child
      Node<T> _parent; // Parent node

      public Node(T key, Node<T> parent, Node<T> left, Node<T> right)
      _value = key;
      _left = left;
      _right = right;
      _parent = parent;


      // Remove a node from the BST
      private Node<T> remove(BinarySearchTree<T> bst, Node<T> z)
      Node<T> delNode = null;
      if(bst._root == null)
      delNode = null;

      else
      Node<T> current = bst._root;
      // find the position to delete
      while(current!=null)
      int compare = z.get_value().compareTo(current.get_value());
      if(compare < 0)
      current = current.get_left();

      if(compare > 0)
      current = current.get_right();

      else
      // if node has two child,replace it with right minimum value
      if (current._left!=null && current._right!=null)
      delNode = current;
      Node<T> successor = minimumKey(current.get_right());
      current.set_value(successor.get_value());
      remove(successor._value);

      if (current._left!=null)
      delNode = current;
      remove(current._value);
      current._left.set_parent(current._parent);

      if (current._right!=null)
      delNode = current;
      remove(current._value);
      current._right.set_parent(current._parent);

      else
      delNode = current;
      remove(current._value);







      return delNode;


      // remove a node value
      public void remove(T key)
      Node<T> z, node;
      if ((z = find(_root, key)) != null)
      if ( (node = remove(this, z)) != null)
      node = null;

      public Node<T> minimumKey(Node<T> current)
      while (current._left != null)
      current = current._left;

      return current;











      share|improve this question
















      I have come across this lab problem about implementing a remove method for generic type binary search tree.



      I have implemented a binary search tree of generic type.



      I have learnt the binary search tree remove algorithm, and I try to deal with 3 cases when the parent node has "0 child", " 1 child" or "2 children".



      I implement the code to remove a node but keeps resulting in a stack overflow problem, and I cannot find where's wrong in my code.



      Any help will be appreciated.



      public class BinarySearchTree<T extends Comparable<T>> 

      private Node<T> _root; //Root node

      public class Node<T extends Comparable<T>>
      public T get_value() return _value;

      public void set_value(T _value) this._value = _value;

      public Node<T> get_left() return _left;

      public void set_left(Node<T> _left) this._left = _left;

      public Node<T> get_right() return _right;

      public void set_right(Node<T> _right) this._right = _right;

      public Node<T> get_parent() return _parent;

      public void set_parent(Node<T> _parent) this._parent = _parent;

      T _value; // Node value
      Node<T> _left; // Left child

      Node<T> _right; // Right child
      Node<T> _parent; // Parent node

      public Node(T key, Node<T> parent, Node<T> left, Node<T> right)
      _value = key;
      _left = left;
      _right = right;
      _parent = parent;


      // Remove a node from the BST
      private Node<T> remove(BinarySearchTree<T> bst, Node<T> z)
      Node<T> delNode = null;
      if(bst._root == null)
      delNode = null;

      else
      Node<T> current = bst._root;
      // find the position to delete
      while(current!=null)
      int compare = z.get_value().compareTo(current.get_value());
      if(compare < 0)
      current = current.get_left();

      if(compare > 0)
      current = current.get_right();

      else
      // if node has two child,replace it with right minimum value
      if (current._left!=null && current._right!=null)
      delNode = current;
      Node<T> successor = minimumKey(current.get_right());
      current.set_value(successor.get_value());
      remove(successor._value);

      if (current._left!=null)
      delNode = current;
      remove(current._value);
      current._left.set_parent(current._parent);

      if (current._right!=null)
      delNode = current;
      remove(current._value);
      current._right.set_parent(current._parent);

      else
      delNode = current;
      remove(current._value);







      return delNode;


      // remove a node value
      public void remove(T key)
      Node<T> z, node;
      if ((z = find(_root, key)) != null)
      if ( (node = remove(this, z)) != null)
      node = null;

      public Node<T> minimumKey(Node<T> current)
      while (current._left != null)
      current = current._left;

      return current;








      java binary-search-tree stack-overflow generic-type-argument






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 16:39







      Sanny

















      asked Mar 28 at 13:37









      SannySanny

      466 bronze badges




      466 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          2
















          You have an issue with your conditions. They should be:



           if(compare < 0) 
          current = current.get_left();
          else if(compare > 0)
          current = current.get_right();
          else {
          ...


          As it is now, if compare < 0 is true, you execute both current = current.get_left(); and the else clause.



          I'm not sure that's the only issue with your remove method, though.






          share|improve this answer

























          • thank you for pointing that out about my else if condition! I just realise that I might be always executing get _left() and whatever inside the else conditions, however, it will still result in an overflow problem, I am thinking whether is something wrong with my while loop

            – Sanny
            Mar 28 at 13:46











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );














          draft saved

          draft discarded
















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399005%2fremove-method-for-generic-type-binary-search-tree-result-in-stack-overflow-probl%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2
















          You have an issue with your conditions. They should be:



           if(compare < 0) 
          current = current.get_left();
          else if(compare > 0)
          current = current.get_right();
          else {
          ...


          As it is now, if compare < 0 is true, you execute both current = current.get_left(); and the else clause.



          I'm not sure that's the only issue with your remove method, though.






          share|improve this answer

























          • thank you for pointing that out about my else if condition! I just realise that I might be always executing get _left() and whatever inside the else conditions, however, it will still result in an overflow problem, I am thinking whether is something wrong with my while loop

            – Sanny
            Mar 28 at 13:46
















          2
















          You have an issue with your conditions. They should be:



           if(compare < 0) 
          current = current.get_left();
          else if(compare > 0)
          current = current.get_right();
          else {
          ...


          As it is now, if compare < 0 is true, you execute both current = current.get_left(); and the else clause.



          I'm not sure that's the only issue with your remove method, though.






          share|improve this answer

























          • thank you for pointing that out about my else if condition! I just realise that I might be always executing get _left() and whatever inside the else conditions, however, it will still result in an overflow problem, I am thinking whether is something wrong with my while loop

            – Sanny
            Mar 28 at 13:46














          2














          2










          2









          You have an issue with your conditions. They should be:



           if(compare < 0) 
          current = current.get_left();
          else if(compare > 0)
          current = current.get_right();
          else {
          ...


          As it is now, if compare < 0 is true, you execute both current = current.get_left(); and the else clause.



          I'm not sure that's the only issue with your remove method, though.






          share|improve this answer













          You have an issue with your conditions. They should be:



           if(compare < 0) 
          current = current.get_left();
          else if(compare > 0)
          current = current.get_right();
          else {
          ...


          As it is now, if compare < 0 is true, you execute both current = current.get_left(); and the else clause.



          I'm not sure that's the only issue with your remove method, though.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 28 at 13:40









          EranEran

          308k40 gold badges521 silver badges597 bronze badges




          308k40 gold badges521 silver badges597 bronze badges















          • thank you for pointing that out about my else if condition! I just realise that I might be always executing get _left() and whatever inside the else conditions, however, it will still result in an overflow problem, I am thinking whether is something wrong with my while loop

            – Sanny
            Mar 28 at 13:46


















          • thank you for pointing that out about my else if condition! I just realise that I might be always executing get _left() and whatever inside the else conditions, however, it will still result in an overflow problem, I am thinking whether is something wrong with my while loop

            – Sanny
            Mar 28 at 13:46

















          thank you for pointing that out about my else if condition! I just realise that I might be always executing get _left() and whatever inside the else conditions, however, it will still result in an overflow problem, I am thinking whether is something wrong with my while loop

          – Sanny
          Mar 28 at 13:46






          thank you for pointing that out about my else if condition! I just realise that I might be always executing get _left() and whatever inside the else conditions, however, it will still result in an overflow problem, I am thinking whether is something wrong with my while loop

          – Sanny
          Mar 28 at 13:46









          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















          draft saved

          draft discarded















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399005%2fremove-method-for-generic-type-binary-search-tree-result-in-stack-overflow-probl%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript