#How do I print all the nodes of the BST

1 messages · Page 1 of 1 (latest)

fervent pathBOT
#

This post has been reserved for your question.

Hey @floral fulcrum! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

maiden bolt
#

There is what we call percolate down and up.

floral fulcrum
#

How do I solve it?

vital hemlock
#

do you have any code you can show?

floral fulcrum
# vital hemlock do you have any code you can show?
private void recursion( BSTNode bstRoot, BSTNode newNode) {

        if (newNode.getC().getUserName().compareTo(bstRoot.getC().
                getUserName()) < 0) {
            if(bstRoot.getL() == null) {
                bstRoot.setL(newNode);
//                if (bstRoot == root) {
//                    root = newNode;
//                }
            } else {
                recursion(bstRoot.getL(),newNode);
            }
        } else {
            if(bstRoot.getR() == null) {
                bstRoot.setR(newNode);
//                if (bstRoot == root) {
//                    root = newNode;
//                }
            } else {
                recursion(bstRoot.getR(),newNode);
            }
        }
    }
#

so i am comparing between two nodes and setting it left or right. In the main question I just gave an example of x,y and z as an example .

vital hemlock
#

if your goal here is to print the tree (presumably by a traversal) why are you modifying it?

floral fulcrum
# vital hemlock if your goal here is to print the tree (presumably by a traversal) why are you m...

i am comparing the username of the colleague and lets suppose 'x' is the first username of the colleague and it is set as the root as it is the first one. Now depending on the next colleague username assuming 'y' i am comparing with the root username's colleague. so in lexicographically as 'y' is bigger than 'x' so it should set 'y' as the right child of 'x'. if there is a new username again assuming 'a' then it does compare with the 'x' and sets to left or right depending on comparison order. But now as the tree is growing I want 'a' to compare with the 'y' which is new root after 'x'.

x

/
y
/
a
but if I try to set y as the new root to compare for the next upcoming username it does set it to left or right but when i try to print the whole tree i get print out jus the a because i set as the new root after 'y'. So basically nodes are not being linked i guess.

#
- Idk if you understood what i was trying to explain. Incase you didn't sorry for that and thanks for the help
restive steeple
# floral fulcrum i am comparing the username of the colleague and lets suppose 'x' is the first u...

I think I understand what you are trying to do, however you seem to have misunderstood how to Binary Search Trees work. In your non-balancing tree you should never reassign the root node at all. Your code looks like it should work if you remove the reassignment of the root. I will try to provide pseudocode what your method should look like:

private void recursion( BSTNode currentNode, BSTNode newNode) {
  if (newNode.value < currentNode.value) {
      if (currentNode.leftChild == null) {
        //Assign newNode as the left child. New Node therefore become a leaf node if left child is not null go another level deeper
      }
  } else {
    if(currentNode.rigthChild == null {
      //Assign newNode as the right child. New Node therefore become a leaf node. If rightChild is not null go another level deeper
    }
  }

}

Please let me know if I understood your problem correctly

floral fulcrum
fervent pathBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.