Microsoft Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

root=deleteV(data,root);

///////////
Node* BSTree::deleteV(int data, Node* n){
if (n == NULL){
return NULL;
}
else if (n -> compare(data) > 0) {
n->setLeft(deleteV(data,n->getLeft()));
}
else if (n -> compare(data) < 0) {
n->setRight(deleteV(data,n->getRight()));
}
else {
Node* tmp=NULL;
if (n ->isLeftNull()){
tmp=n;
n=n->getRight();
delete tmp;

}
else if (n->isRightNull()){
tmp=n;
n=n->getLeft();
delete tmp;
}
else {


tmp=n->getLeft();
while(!tmp->isRightNull())
tmp=tmp->getRight();
n->setData(tmp->getData());
n->setLeft(deleteV(tmp->getData(),n->getLeft()));

}

}
return n;
}

- Ajay February 17, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

clear solution

node *remove(node *root, node *z) {

    if(z == 0)
        return root;

    node *del = z->right;
    if(del != 0 && z->left != 0) {
        // here z has both children: find it's inorder successor
        while(del->left != 0)
            del = del->left;
        z->key = del->key; // rewrite it's key
    } else
        del = z;

    // del is a node to be removed (has at most 1 child)
    node *p = del->parent, *child = del->left;
    if(child == 0)
        child = del->right;

    if(child != 0)
        child->parent = p;
    
    if(p != 0) {
        if(del == p->left) 
            p->left = child;
        else 
            p->right = child;
    } else  
        root = child;

    delete del;
    return root;
}

- pavel.em June 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

asm,

I don't think there will be a link to parent from any node. Your logic is right. But, you should keep track of the parent too while traversing the tree.

- noviceprog September 04, 2008 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More