In the previous blog, the implementation of the delete function is based on the number of child nodes of the deleted node nodes, divided into No child nodes, a child node and two sub-nodes are considered separately.
And this time the code is based on the implementation of the introduction of the algorithm in C + + literal translation, the code is as follows:
voidBinarysearchtree::D elete (Constint32_t&value) {Auto Node=Search (value); if(node = =nullptr) {Cerr<<"There is no such value!\n"; return; } Ptreenode successor=(Node->leftchild = = Nullptr | | Node->rightchild = =nullptr)?node:successor (node); Ptreenode Successorchild=(successor->rightchild! =nullptr)? Successor->Leftchild:successor-Rightchild; if(Successorchild! =nullptr) {Successorchild->parent = successor->parent; } if(successor->parent.expired ()) {Root=Successorchild; } Else{Auto Successorparent= Successor->parent.Lock (); if(successor = = Successorparent->leftchild) {successorparent->leftchild =Successorchild; } Else{successorparent->rightchild =Successorchild; } } if(Node! =successor) {Node->key = successor->key; }}
Implementation of Delete function in two-tree linked list in the introduction of algorithm