Binary Search Tree II Aizu - ALDS1_8_B
Write a program which performs the following operations to a binary search tree by adding the find operation to A: Binary Search Tree I.
insert : Insert a node containing as key into .
find : Report whether has a node containing .
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 is given. In the following lines, operations represented by insert , find or print are given.
Output
For each find operation, print “yes” if has a node containing , “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
The number of print operations .
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;
}