简体   繁体   English

在二叉树中删除

[英]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: 修改deleteNode()函数为:

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. 这样,您可以在单个遍历中获得父节点,现在可以相应地修改删除功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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