2

I was asked to implement a binary search tree with follow operation for each node v - the complexity should be O(1). The follow operation should return a node w (w > v).
I proposed to do it in O(log(n)) but they wanted O(1) Upd. It should be next greater node

6
  • 1
    And your question is... Commented Nov 29, 2013 at 7:33
  • You can only get amortized O(1) for this operation. Worst case is still O(log(n)). Did you try showing that? Commented Nov 29, 2013 at 7:36
  • I'm not sure if I am correct, but If you maintain pointers to parents for all left children, For every node v, if v->right exists then it shall be greater than v. Else, if it is a left child, then its parent shall be greater than the node. Commented Nov 29, 2013 at 7:45
  • I assume you still need insert/delete operations, i.e. tree is dynamic, right? Commented Nov 29, 2013 at 7:45
  • @ile Yes,insert and delete operations shall not be O(1), but follow operation shall be. Commented Nov 29, 2013 at 7:46

4 Answers 4

5

just keep the maximum element for the tree and always return it for nodes v < maximum.

Sign up to request clarification or add additional context in comments.

4 Comments

You are right. The description says they only want any greater node, not the next greater node. Seems like this is more of an interview-2.0 question.
@Domi - Excuse me - it is a next greater node
@Domi - what is 2.0 question
@Yakov then this is not your solution.
0

You can get O(1) if you store pointers to the "next node" (using your O(log(n) algorithm), given you are allowed to do that.

5 Comments

what if the node is a leaf?
I updated my answer. I was thinking of leaf nodes only, like a B+ - Tree.
I think you are right, if updates to the trees are no longer done in optimal time. For example, insertion and deletion are done in O(n)?
I believe it can be achieved with careful choosing of your "next nodes", otherwise with an unlucky deletion you would need to update all the nodes in the tree.
A single node deletion only requires extra work of updating the guy who pointed to you which is O(deletion + log(n)).
0

How about:

int tree[N];

size_t follow(size_t v) {
    // First try the right child
    size_t w = v * 2 + 1;
    if(w >= N) {
        // Otherwise right sibling
        w = v + 1;
        if(w >= N) {
            // Finally right parent
            w = (v - 1) / 2 + 1;
        }
    }
    return w;
}

Where tree is a complete binary tree in array form and v/w are represented as zero-based indices.

Comments

0

One idea is to literally just have a next pointer on each node.

You can update these pointers in O(height) after an insert or remove (O(height) is O(log n) for a self-balancing BST), which is as long as an insert or remove takes, so it doesn't add to the time complexity.

Alternatively, you can also have a previous pointer in addition to the next pointer. If you do this, you can update these pointers in O(1).

Obviously, in either case, if you have a node, you also have its next pointer, and you can simply get this value in O(1).

Pseudo-code

For only a next pointer, after the insert, you'd do:

if inserted as a right child:
   newNode.next = parent.next
   parent.next = newNode
else // left child
   predecessor(newNode)

For both next and previous pointers:

if inserted as a right child:
   parent.next.previous = newNode
   newNode.next = parent.next
   parent.next = newNode
else // left child
   parent.previous.next = newNode
   newNode.previous = parent.previous
   parent.previous = newNode

(some null checks are also required).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.