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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?