简体   繁体   中英

Deletion in a Binary Tree

I've been trying to implement Deletion in a Binary Tree. I know that the three steps are:

  • Identifying the node to be deleted and the deepest node.
  • Replacing its data with the data of the deepest node.
  • Deleting the deepest node.

I had to traverse the entire tree to find the deepest node. In order to delete that node, I need to find its parent. Is there any other way to find its parent without having to traverse the entire tree the second time?

This is my code.

tnode* searchNode(Tree &T, int data) {
    tnode* temp = nullptr;
    Queue Q;

    if(!T.root)
        return nullptr;

    enqueue(Q, T.root);
    while(!isEmptyQueue(Q)) {
        temp = dequeue(Q);

        if(temp->data == data) {
            return temp;
        }
        if(temp->left) {
            enqueue(Q, temp->left);
        }
        if(temp->right) {
            enqueue(Q, temp->right);
        }
    }
    return nullptr;
}

tnode* findDeepestNode(Tree &T) {
    tnode *temp = nullptr;
    Queue Q;

    if(!T.root)
        return nullptr;

    enqueue(Q, T.root);
    while(!isEmptyQueue(Q)) {
        temp = dequeue(Q);

        if(temp->left)
            enqueue(Q, temp->left);

        if(temp->right)
            enqueue(Q, temp->right);
    }
    return temp;
}

void removeNode(Tree &T, tnode *search) {
    tnode *temp = nullptr;
    tnode *del = nullptr;
    Queue Q;

    if(!T.root || T.root == search)
        return;

    enqueue(Q, T.root);
    while (!isEmptyQueue(Q)) {
        temp = dequeue(Q);

        if(temp->left) {
            if(temp->left == search) {
                del = temp->left;
                temp->left = nullptr;
                delete del;
                return;
            }
            else
                enqueue(Q, temp->left);
        }

        if(temp->right) {
            if(temp->right == search) {
                del = temp->right;
                temp->right = nullptr;
                delete del;
                return;
            }
            else
                enqueue(Q, temp->right);
        } 
    }
    return;
}

void deleteNode(Tree &T, int data) {
    tnode *search = searchNode(T, data);
    tnode *deepestnode = findDeepestNode(T);

    if(search) {
        search->data = deepestnode->data;
        removeNode(T, deepestnode);
    }
}

I've just started learning Data Structures. The code I wrote seems way too long. How can I shorten this code? Also please correct me if I'm following any bad coding practices.

In this function only, you can find the deepest node as well as parent node too by passing a double pointer to the deepest node and it's parent:

tnode* searchNode(Tree &T, int data, tnode** deepest, tnode **parent) {
    tnode* temp = nullptr;
    tnode* searchNode = nullptr;
    Queue Q,parentQ;

    if(!T.root)
        return nullptr;

    enqueue(Q, T.root);
    enqueue(parentQ, nullptr);
    while(!isEmptyQueue(Q)) {
        temp = dequeue(Q);
        *parent = dequeue(parentQ);
        if(temp->data == data) {
            searchNode = temp;
        }
        if(temp->left) {
            enqueue(Q, temp->left);
            enqueue(parentQ, temp);
        }
        if(temp->right) {
            enqueue(Q, temp->right);
            enqueue(parentQ, temp);
        }
    }
    *deepest = temp;
    return searchNode;
}

Modify the deleteNode() function as:

void deleteNode(Tree &T, int data) {
    tnode *deepestnode,*parent;
    tnode *search = searchNode(T, data, &deepestnode, &parent);

    if(search) {
        search->data = deepestnode->data;
        removeNode(T, deepestnode);
    }

}

This way, you can get the parent node in a single traversal and now you can modify your remove function accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM