• Binary Search Tree II Aizu


    Write a program which performs the following operations to a binary search tree TT by adding the find operation to A: Binary Search Tree I.

    insert kk: Insert a node containing kk as key into TT.
    find kk: Report whether TT has a node containing kk.
    print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

    Input

    In the first line, the number of operations mm is given. In the following mm lines, operations represented by insert kk, find 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

    10
    insert 30
    insert 88
    insert 12
    insert 1
    insert 20
    find 12
    insert 17
    insert 25
    find 16
    print

    Sample Output 1

    yes
    no
    1 12 17 20 25 30 88
    30 12 1 20 17 25 88

    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 I Aizu - ALDS1_8_A.cpp created by VB_KoKing on 2019-05-09:18.
    /* 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>
    
    using namespace std;
    struct Node {
        int key;
        Node *right, *left, *parent;
    };
    
    Node *root, *NIL;
    
    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;
    }
    
    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;
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    图论-桥/割点/双连通分量/缩点/LCA
    未解决的问题
    hdu2586(LCA最近公共祖先)
    LCA最近公共祖先(least common ancestors)
    111
    poj1703 Find them, Catch them 并查集
    关于背包的注意事项
    codeforces343A A. Rational Resistance
    hdu(1171)多重背包
    HTML5事件—visibilitychange 页面可见性改变事件
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338323.html
Copyright © 2020-2023  润新知