Tested, There are no bugs now. Thank you for your advice.
// Main. cpp /////////////////////////////////////// //////////// topic description: delete the given node of the given binary sorting tree // Author: k_eckel (Wei furu) // time: 2005-09-30 /////////////////////////////////// ////////////// # include <iostream> using namespace std; template <class T> struct node {node <T> * _ lchild; node <T> * _ rchild; T _ key; node (const T & key ): _ lchild (NULL), _ rchild (NULL), _ key (key) {}}; template <class T> void Insert (node <T> * & root, const T & key) {if (NU LL = root) {node <T> * n = new node <T> (key); root = n; return;} if (root-> _ key> key) insert (root-> _ lchild, key); elseInsert (root-> _ rchild, key);} template <class T> void Delete (node <T> * & root, const T & key) {if (root = NULL) return; node <T> * ptr = root, * pre = NULL; while (ptr! = NULL & ptr-> _ key! = Key) {pre = ptr; if (ptr-> _ key> key) ptr = ptr-> _ lchild; elseptr = ptr-> _ rchild ;} if (ptr = NULL) return; if (ptr-> _ lchild = NULL & ptr-> _ rchild = NULL) {if (ptr = root) {root = NULL; delete ptr; return;} else {if (ptr = pre-> _ lchild) pre-> _ lchild = NULL; elsepre-> _ rchild = NULL; delete ptr ;}} else if (ptr-> _ rchild = NULL) {if (ptr = root) root = ptr-> _ lchild; else if (pre-> _ lchild = ptr) pre-> _ lchild = ptr-> _ lchil D; elsepre-> _ rchild = ptr-> _ lchild; delete ptr;} else if (ptr-> _ lchild = NULL) {if (ptr = root) root = ptr-> _ rchild; else if (pre-> _ lchild = ptr) pre-> _ lchild = ptr-> _ rchild; elsepre-> _ rchild = ptr-> _ rchild; delete ptr;} else {node <T> * tmp = ptr; pre = ptr; ptr = ptr-> _ rchild; while (ptr-> _ lchild! = NULL) {pre = ptr; ptr = ptr-> _ lchild;} tmp-> _ key = ptr-> _ key; if (ptr-> _ rchild! = NULL) {if (pre-> _ lchild = ptr) pre-> _ lchild = ptr-> _ rchild; elsepre-> _ rchild = ptr-> _ rchild ;} delete ptr ;}} template <class T> void InorderPrint (const node <T> * root, const char * info) {if (NULL = root) return; if (info) cout <info <endl; if (NULL! = Root) {InorderPrint (root-> _ lchild, NULL); cout <root-> _ key <""; InorderPrint (root-> _ rchild, NULL) ;}} int main (int argc, char * argv []) {// test code node <int> * r = NULL; // create a sorting binary tree for testing: Insert <int> (r, 5); Insert (r, 2); Insert (r, 1); Insert (r, 3); Insert (r, 4); Insert (r, 7); Insert (r, 6); Insert (r, 8); Insert (r, 9 ); inorderPrint <int> (r, "before delete"); cout <endl; Delete <int> (r, 25 ); // delete a nonexistent node InorderPrint <int> (r, "after Delete"); cout <endl; // delete <int> (r, 5 ); // left and right subtree, root node // Delete <int> (r, 7); // left and right subtree, non-root node // Delete <int> (r, 1); // No left and right nodes // Delete <int> (r, 8); // right subtree, no left subtree // Delete <int> (r, 8); // right subtree, no left subtree Delete <int> (r, 3); Delete <int> (r, 4); Delete <int> (r, 1); // at this time, there is a left subtree, and no right subtree InorderPrint <int> (r, "after delete"); cout <endl; Delete <int> (r, 7); InorderPrint <int> (r, "after delete"); cout <endl; Delete <int> (r, 9); Delete <int> (r, 8); Delete <int> (r, 2); InorderPrint <int> (r, "after delete"); cout <endl; Delete <int> (r, 5); InorderPrint <int> (r, "after delete"); cout <endl; Delete <int> (r, 6); // only the root node is available at this time, inorderPrint (r, "after delete"); cout <endl; system ("pause"); return 0 ;}