Binary Search Tree III Aizu

Write a program which performs the following operations to a binary search tree TT by adding delete operation to B: Binary Search Tree II.

insert kk: Insert a node containing kk as key into TT.
find kk: Report whether TT has a node containing kk.
delete kk: Delete a node containing kk.
print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.
The operation delete kk for deleting a given node zz containing key kk from TT can be implemented by an algorithm which considers the following cases:

If zz has no children, we modify its parent z.pz.p to replace zz with NIL as its child (delete zz).
If zz has only a single child, we “splice out” zz by making a new link between its child and its parent.
If zz has two children, we splice out zz's successor yy and replace zz's key with yy's key.

Input

In the first line, the number of operations mm is given. In the following mm lines, operations represented by insert kk, find kk, delete kk or print are given.

Output

For each find kk operation, print “yes” if TT has a node containing kk, “no” if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

Constraints

The number of operations 500,000leq 500,000
The number of print operations 10leq 10.
2,000,000,000key2,000,000,000-2,000,000,000 leq key leq 2,000,000,000
The height of the binary tree does not exceed 100 if you employ the above pseudo code.
The keys in the binary search tree are all different.

Sample Input 1

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print

Sample Output 1

yes
yes
yes
no
no
no
yes
yes
1 2 3 7 8 22
8 2 1 3 7 22
1 2 8 22
8 2 1 22

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Code

/*
                                ^....0
                               ^ .1 ^1^
                               ..     01
                              1.^     1.0
                             ^ 1  ^    ^0.1
                             1 ^        ^..^
                             0.           ^ 0^
                             .0            1 .^
                             .1             ^0 .........001^
                             .1               1. .111100....01^
                             00                 11^        ^1. .1^
                             1.^                              ^0  0^
                               .^                                 ^0..1
                               .1                                   1..^
                             1 .0                                     ^  ^
                              00.                                     ^^0.^
                              ^ 0                                     ^^110.^
                          0   0 ^                                     ^^^10.01
                   ^^     10  1 1                                      ^^^1110.1
                   01     10  1.1                                      ^^^1111110
                   010    01  ^^                                        ^^^1111^1.^           ^^^
                   10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                    11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                    1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                   10   00 11                                               ^^^^^   1 0           1.
                   0^  ^0  ^0                                                ^^^^    0            0.
                   0^  1.0  .^                                               ^^^^    1 1          .0
                   ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                   1 ^      11                             1.                ^^^     ^ ^        ..^
                  ^..^      ^1                             ^.^               ^^^       .0       ^.0
                  0..^      ^0                              01               ^^^       ..      0..^
                 1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                ^  1.        00                              0.             ^^^        ^.0 ^.1
                . 0^.        ^.^                             ^.^            ^^^         ..0.0
               1 .^^.         .^                  1001        ^^            ^^^         . 1^
               . ^ ^.         11                0.    1         ^           ^^          0.
                0  ^.          0              ^0       1                   ^^^          0.
              0.^  1.          0^             0       .1                   ^^^          ..
              .1   1.          00            .        .1                  ^^^           ..
             1      1.         ^.           0         .^                  ^^            ..
             0.     1.          .^          .         0                                  .
             .1     1.          01          .        .                                 ^ 0
            ^.^     00          ^0          1.       ^                                 1 1
            .0      00           .            ^^^^^^                                   .
            .^      00           01                                                    ..
           1.       00           10                                                   1 ^
          ^.1       00           ^.                                            ^^^    .1
          ..        00            .1                                        1..01    ..
         1.1         00           1.                                       ..^      10
        ^ 1^         00           ^.1                                      0 1      1
        .1           00            00                                       ^  1   ^
         .           00            ^.^                                        10^  ^^
       1.1           00             00                                              10^
       ..^           1.             ^.                                               1.
      0 1            ^.              00                 00                            .^
        ^            ^.              ^ 1                00   ^0000^     ^               01
     1 0             ^.               00.0^              ^00000   1.00.1              11
     . 1              0               1^^0.01                      ^^^                01
      .^              ^                1   1^^                                       ^.^
    1 1                                                                              0.
    ..                                                                              1 ^
     1                                                                               1
   ^ ^                                                                             .0
   1                                                                             ^ 1
   ..                                                          1.1            ^0.0
  ^ 0                                                           1..01^^100000..0^
  1 1                                                            ^ 1 ^^1111^ ^^
  0 ^                                                             ^ 1      1000^
  .1                                                               ^.^     .   00
  ..                                                                1.1    0.   0
  1.                                                                  .    1.   .^
  1.                                                                 1    1.   ^0
 ^ .                                                                 ^.1 00    01
 ^.0                                                                  001.     .^
 */
// Virtual_Judge —— Binary Search Tree III Aizu - ALDS1_8_C.cpp created by VB_KoKing on 2019-05-10:08.
/* Procedural objectives:

 Variables required by the program:

 Procedural thinking:

 Functions required by the program:
 
 Determination algorithm:
 
 Determining data structure:
 

*/
/* My dear Max said:
"I like you,
So the first bunch of sunshine I saw in the morning is you,
The first gentle breeze that passed through my ear is you,
The first star I see is also you.
The world I see is all your shadow."

FIGHTING FOR OUR FUTURE!!!
*/
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

struct Node {
    int key;
    Node *right, *left, *parent;
};

Node *root, *NIL;

Node *tree_minimum(Node *x) {
    while (x->left != NIL)
        x = x->left;
    return x;
}

Node *find(Node *u, int k) {
    while (u != NIL && k != u->key) {
        if (k<u->key) u=u->left;
        else u=u->right;
    }
    return u;
}

Node *tree_successor(Node *x){
    if (x->right!=NIL) return tree_minimum(x->right);
    Node *y=x->parent;
    while(y!=NIL&&x==y->right){
        x=y;
        y=y->parent;
    }
    return y;
}

void tree_delete(Node *z){
    Node *x,*y;
    if (z->left==NIL||z->right==NIL) y=z;
    else y=tree_successor(z);

    if (y->left!=NIL) x=y->left;
    else x=y->right;

    if (x!=NIL) x->parent=y->parent;

    if (y->parent==NIL) root=x;
    else {
        if (y==y->parent->left) y->parent->left=x;
        else y->parent->right=x;
    }

    if (y!=z) z->key=y->key;

    free(y);
}

void insert(int k) {
    Node *y = NIL;
    Node *x = root;
    Node *z;

    z = (Node *) malloc(sizeof(Node));
    z->key = k;
    z->left = NIL;
    z->right = NIL;

    while (x != NIL) {
        y = x;
        if (z->key < x->key)
            x = x->left;
        else
            x = x->right;
    }

    z->parent = y;
    if (y == NIL)
        root = z;
    else {
        if (z->key < y->key)
            y->left = z;
        else y->right = z;
    }
}

//前序遍历
void pre_parse(Node *u) {
    if (u == NIL) return;
    cout << " " << u->key;
    pre_parse(u->left);
    pre_parse(u->right);
}

//中序遍历
void in_parse(Node *u) {
    if (u == NIL) return;
    in_parse(u->left);
    cout << " " << u->key;
    in_parse(u->right);
}

int main() {
    int n;
    string com;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> com;
        if (com == "insert") {
            int x;
            cin >> x;
            insert(x);
        } else if (com == "print") {
            in_parse(root);
            cout << endl;
            pre_parse(root);
            cout << endl;
        } else if (com=="find"){
            int x;
            cin>>x;
            Node *t=find(root,x);
            if (t!=NIL) cout<<"yes"<<endl;
            else cout<<"no"<<endl;
        } else if (com=="delete"){
            int x;
            cin>>x;
            tree_delete(find(root,x));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AlexKing007/p/12338319.html