0

I implemented public BinarySearchTree<Node,T> chop(T x) that chops my tree into two parts at element x. The SSet this will contain elements < x, and the returned SSet is a SSet that contains elements >= x. This should work for all elements regardless of whether they are in this.

For example, suppose s={2,4,6,8}. Then s.chop(3) returns {4,6,8} and s becomes {2}. We would get the same result for s.chop(4).

The slowChop method is implemented, but it has a time complexity of O(n), but I need to reduce it to at least O(h) when the tree is balanced (where h is the height of the tree).

public BinarySearchTree<Node,T> slowChop(T x) {
    Node sample = super.newNode();
    BinarySearchTree<Node,T> other = new 
    BinarySearchTree<Node, T>(sample);

    // Iterate through the n nodes in-order.
    // When see value >=x, add to new BST in O(height) time, and
    // remove it from this BST (on next iteration) in O(height) time.
        Iterator<T> it = iterator();
    T prev = null;
    while( it.hasNext() ) {
      T curr = (T)(it.next());
      if( c.compare(curr, x) >= 0 ) { // we have our first >= x 
other.add(curr);
        if( prev != null ) {
          this.remove(prev);          // safe to remove now
        }
        prev = curr;
      }
    }
    if( prev != null ) {
      this.remove(prev); // edge case, get that last one!
    }
    return other; 
}

 public BinarySearchTree<Node,T> chop(T x) {
        Node sample = super.newNode();
        BinarySearchTree<Node,T> other = new 
        BinarySearchTree<Node, T>(sample);
    // TODO: Implement this method. It should match slowChop in
    // behaviour, but should be faster :-)
    

    
    return other;
  }

1 Answers1

2

Indeed, your algorithm is not making use of the efficiency that you can get from the fact you are dealing with a binary search tree. So iterating through the tree with an in-order traversal is not the way to go.

Instead, perform a binary search and cut the edges that link two nodes which should end up in different trees. While cutting you'll also need to reattach nodes to where a previous cut was performed. The complexity is the same as a binary search towards the bottom of the tree, and so it is O(logn).

Here is an implementation that assumes you have the regular getters and setters:

  • on the Node class: getLeft, setLeft, getRight, setRight, getValue, and
  • on the BinarySearchTree class: getRoot, setRoot
public BinarySearchTree<Node,T> chop(T x) {
    // Create two temporary dummy (sentinel) nodes to ease the process.
    Node rightRootParent = super.newNode();
    Node leftRootParent = super.newNode();
    
    // Set "cursors" at both sides
    Node rightParent = rightRootParent;
    Node leftParent = leftRootParent;
    
    // Start the binary search for x, cutting edges as we walk down
    Node node = this.getRoot();
    while (node != null) {
        // Decide for each node in the binary search path at which side it should go
        if (c.compare(node.getValue(), x) >= 0) {
            // Node should belong to the right-side tree
            rightParent.setLeft(node); // Establish edge
            rightParent = node; 
            node = node.getLeft(); // Move down
            rightParent.setLeft(null); // Cut next edge for now (it might get restored)
        } else { // Symmetric case
            leftParent.setRight(node);
            leftParent = node;
            node = node.getRight();
            leftParent.setRight(null);
        }
    }
    
    // Set the roots of both trees
    this.setRoot(leftRootParent.getRight());
    return BinarySearchTree<Node, T>(rightRootParent.getLeft());
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • but my function should look like this public BinarySearchTree chop(T x) { Node sample = super.newNode(); BinarySearchTree other = new BinarySearchTree(sample); // TODO: Implement this method. It should match slowChop in // behaviour, but should be faster :-) return other; } – Nihal Modaress Nov 28 '21 at 22:19
  • It says "implement this method". This is what I have done. I don't see what the issue is. – trincot Nov 28 '21 at 22:25
  • yes it is correct but we are asked to implement it and return other i have added the function that we are given above PLEASE if you could help me get done with this part – Nihal Modaress Nov 28 '21 at 22:29
  • Sorry, but you tried to edit my answer. I rejected it. I don't understand the issue. Is this really about *naming* variables, like `other`? I used different variable names, because `sample` really smells of *example* code, otherwise that name is a bad choice. – trincot Nov 28 '21 at 22:31
  • sorry i accidentally tried to edit yours while i was trying to edit mine my bad !! yes i am trying to follow the format and the variables that we have otherwise we dont really get the grade and it shows as a 0 – Nihal Modaress Nov 28 '21 at 22:32
  • Moreover, that template code is problematic, since it creates a "sample" node for the `other` tree, but there are cases where the returned tree should be *empty*, so that is counter productive. I really believe that code is just there as a "sample", i.e. as example code. Not necessary to be literally included. – trincot Nov 28 '21 at 22:37
  • Alright thank you so much though I’ll just work on the variable names and try to figure it out – Nihal Modaress Nov 28 '21 at 22:44
  • why are getRoot() and setLeft() and setRight() is undefined for the type BinarySearchTree i dont have the getters and setter in my case – Nihal Modaress Nov 29 '21 at 01:49
  • So what do you have then? You didn't provide the code of the two classes, so you I have no idea what you have there. Just look at what is public in those classes. If you cannot make it work, then please edit your question and include the code of both the `BinarySearchTree` class and the `Node` class. – trincot Nov 29 '21 at 06:45
  • Are you there ? – trincot Nov 29 '21 at 22:27
  • Hello, could you come back to me? You made a comment about the method names, but then didn't reply to my question for the class definition... – trincot Dec 03 '21 at 09:16