2
votes

My tree class:

public class BT<E>{
   E value;
   BT<E> left, right;

       public BT(E value)
       {
          this.value=value;
       }   

       public BT (E value, BT left, BT right) 
       {
          this.value = value;
          this.left = left;
          this.right = right;
       }
  public Tree getLeft(){
      return left;}
   public Tree getRight(){
      return right;}
   public void setLeft(Tree ln){
      left = ln;}
   public void setRight(Tree rn){
      right = rn;}
   public Tree getParent(){
      return this;}

I recursively generate two trees in my main method. T1 the main tree and T2 a random subtree. I am able to randomly select a node in T1 however I am not able to replace T2 AT the randomly selected node in T1.

Since at a given node I'm only able to setLeft() and setRight() I do not know how to set at the current node I am at.

Even a nudge in the right direction would be appreciated.

[EDIT] To clarify, based on the structure of my tree, how would one go about replacing a specific node with another seperate tree (sub-tree).

2
You have a method called getParent. I assume it should return the parent of the current node (and not what it currently does). After getting the parent, you can change the left or right of the parent as you wish. - tmrlvi
@tmrlvi I do not want to change the left or right. I want to change the current node. So I need a setParent(Tree p) - but I'm not sure how to go about replacing a node with an entire tree. - RK2015
A node is a tree, I don't see the problem - Dici
Like tmrlvi already stated, you need a pointer to the parent. Once you have it, you replace the appropriate link (left or right) with the root node of your subtree. - mzc
You don't even necessarily need a pointer to the parent. It may just require changing the algorithm to have a lookahead on the children while keeping a reference on their parent. Before changing the data structure, try adapting the algorithm. If it is not easy or not elegant, adapt the data structure - Dici

2 Answers

1
votes

You either have to fix your getParent method, or to fix your algorithm. Since I don't have your algo, I will show you the first option (which is not my favourite one) :

public class BT<E> {
   E value;
   BT<E> left, right, parent;

   public BT(E value) {
      this.value = value;
      this.parent = null;
   }   

   public BT (E value, BT left, BT right) {
      this.value = value;
      setLeft(left);
      setRight(right);
   }

   private static void setParentIfNotNull(BT<E> child, BT<E> parent) {
       if (child != null) child.setParent(parent);
   }

   public void setLeft(BT<E> left) {
       this.left = left;
       setParentIfNotNull(left, this);
   }

   public void setRight(BT<E> right) {
       this.right= right;
       setParentIfNotNull(right, this);
   }
   // getters
}
0
votes

To remove (or replace) an arbitrary node, you need to update its parent. So first call .getParent(), then you need to figure out which side your target node resides, left or right, by calling parent.getLeft() == nodeToRemove (and the same for .getRight(), if you want). Once you know if it's left or right, call the appropriate .set...() method.

I didn't initially notice that your .getParent() method returns the current node (this). That is incorrect; the parent node is the node that points to this. If you cannot fix your .getParent() implementation you'll need to change your algorithm to keep track of the parent node while you traverse the tree, like @Dici suggests.