The deletenode (bitree * r, int e) function is:R's binaryDelete the node with the key value e in the search (SORT) tree. If the node is successfully deleted, the function returns 0; otherwise, the function returns-1. The binary search tree node type is defined: Typedef struct tnode { Int data; Struct tnode * lchild, * rchild; } * Bitree; When deleting a knot in the binary search tree, consider the following three situations: 1. If the node P to be deleted is a leaf node, delete it directly; 2. If the node P to be deleted has only one subnode, connect the subnode directly with the parent node of the node to be deleted, and then delete the node P; 3. if the node P to be deleted has two subnodes, search for the node s with the maximum key value in the middle order on its left subtree, and replace the value of node P with the value of node S, then delete node S. node S is one of the above ○ 1 and ○ 2 conditions. Int deletenode (bitree * r, int e ){ Bitree P = * r, PP, S, C; While (P & P-> data!= E) {/* Find the node with the key value e from the root node */ Pp = P; If (E <p-> data) P = p-> lchild; Else P = p-> rchild; } If (! P) Return-1;/* search failed */ If (p-> lchild & P-> rchild) {/* Processing status 3 */ S = p-> lchild; Pp = P; While (S-> rchild) { Pp = s; S = s-> rchild; } P-> DATA = s-> data; P = s; } /* Handling situations ○ 1 and ○ 2 */ If (P-> lchild) C = p-> lchild; Else C = p-> rchild; If (P = * r) * R = C; Else If (P = PP-> lchild) PP-> lchild = C; Else PP-> rchild = C; Free (P ); Return 0; } |