二叉排序树的基本操作
#include <bits/stdc++.h>
using namespace std;
const int N = 509;
int n, k, x, root, idx;
struct node {
int val, l, r;
}a[N];
void insert (int &u, int x) {
if (!u) {
u = ++ idx;
a[idx].val = x;
return;
}
if (x < a[u].val) insert(a[u].l, x);
else insert(a[u].r, x);
}
bool query(int &u, int x) {
if (!u) return false;
if (x == a[u].val) return true;
if (x < a[u].val) return query(a[u].l, x);
if (x > a[u].val) return query(a[u].r, x);
}
int main() {
cin >> n >> k;
while (n --) {
cin >> x;
insert(root, x);
}
while (k --) {
cin >> x;
if (query(root, x)) cout << 1 << ' ';
else cout << 0 << ' ';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 509;
int n, k, x;
struct node {
int val;
node *l, *r;
node (int x) {
this->val = x;
l = r = NULL;
}
};
node *root;
void insert (node* &u, int x) {
if (u == NULL) {
u = new node(x);
return;
}
if (x < u->val) insert(u->l, x);
else insert(u->r, x);
}
bool query(node* u, int x) {
if (u == NULL) return false;
if (x == u->val) return true;
if (x < u->val) return query(u->l, x);
if (x > u->val) return query(u->r, x);
}
int main() {
cin >> n >> k;
while (n --) {
cin >> x;
insert(root, x);
}
while (k --) {
cin >> x;
if (query(root, x)) cout << 1 << ' ';
else cout << 0 << ' ';
}
return 0;
}
二叉排序树
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, x, idx, root;
struct BST {
int val, l, r;
}tr[N];
void insert(int& u, int x) {
if (!u) {
u = ++ idx;
tr[u].val = x;
return;
}
if (x >= tr[u].val) insert(tr[u].r, x);
else insert(tr[u].l, x);
}
void inorder(int u) {
if (!u) return;
inorder(tr[u].l);
cout << tr[u].val << ' ';
inorder(tr[u].r);
}
void postorder(int u) {
if (!u) return;
postorder(tr[u].l);
postorder(tr[u].r);
cout << tr[u].val << ' ';
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(root, x);
}
inorder(root);
cout << endl;
postorder(root);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, x;
struct node {
int val;
node *l, *r;
node (int x) {
this->val = x;
l = r = NULL;
}
};
node* root;
void insert(node* &u, int x) {
if (u == NULL) {
u = new node(x);
return;
}
if (x >= u->val) insert(u->r, x);
else insert(u->l, x);
}
void inorder(node* u) {
if (u == NULL) return;
inorder(u->l);
cout << u->val << ' ';
inorder(u->r);
}
void postorder(node* u) {
if (u == NULL) return;
postorder(u->l);
postorder(u->r);
cout << u->val << ' ';
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(root, x);
}
inorder(root);
cout << endl;
postorder(root);
return 0;
}
二叉排序树的删除
#include <bits/stdc++.h>
using namespace std;
int n, m, x, idx, root;
struct node {
int val, l, r;
}a[109];
void insert(int &u, int x) {
if (!u) {
u = ++ idx;
a[u].val = x;
return;
}
if (x > a[u].val) insert(a[u].r, x);
else insert(a[u].l, x);
}
void remove(int &u, int x) {
if (!u) return;
if (x < a[u].val) remove(a[u].l, x);
else if (x > a[u].val) remove(a[u].r, x);
else {
if (!a[u].l && !a[u].r) u = 0;
else if (!a[u].l) u = a[u].r;
else if (!a[u].r) u = a[u].l;
else {
int v = a[u].l;
while (a[v].r) v = a[v].r;
a[u].val = a[v].val;
remove(a[u].l, a[v].val);
}
}
}
void inorder(int u) {
if (!u) return;
inorder(a[u].l);
cout << a[u].val << ' ';
inorder(a[u].r);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(root, x);
}
cin >> m;
remove(root, m);
inorder(root);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n, m, x;
struct node {
int val;
node *l, *r;
node (int x) {
this->val = x;
l = r = NULL;
}
};
node* root;
void insert(node* &u, int x) {
if (u == NULL) {
u = new node(x);
return;
}
if (x > u->val) insert(u->r, x);
else insert(u->l, x);
}
void remove(node* &u, int x) {
if (u == NULL) return;
if (x < u->val) remove(u->l, x);
else if (x > u->val) remove(u->r, x);
else {
if (u->l == NULL && u->r == NULL) u = NULL;
else if (u->l == NULL) u = u->r;
else if (u->r == NULL) u = u->l;
else {
node* v = u->l;
while (v->r) v = v->r;
u->val = v->val;
remove(u->l, v->val);
}
}
}
void inorder(node* u) {
if (u == NULL) return;
inorder(u->l);
cout << u->val << ' ';
inorder(u->r);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(root, x);
}
cin >> m;
remove(root, m);
inorder(root);
return 0;
}
进击的二叉查找树
#include <bits/stdc++.h>
using namespace std;
int n, x;
struct node {
int val;
node *l, *r;
node (int x) {
this->val = x;
l = r = NULL;
}
};
node *ra, *rb;
void insert(node* &u, int x) {
if (u == NULL) {
u = new node(x);
return;
}
if (x > u->val) insert(u->r, x);
else insert(u->l, x);
}
bool dfs(node* u, node* v) {
if (u == NULL && v != NULL) return false;
if (u != NULL && v == NULL) return false;
if (u == NULL && v == NULL) return true;
return dfs(u->l, v->l) && dfs(u->r, v->r);
}
void postOrder(node* u) {
if (u == NULL) return;
postOrder(u->l);
postOrder(u->r);
cout << u->val << ' ';
}
void layerOrder(node* u) {
queue <node*> q;
q.push(u);
while (q.size()) {
node* ft = q.front();
q.pop();
cout << ft->val << ' ';
if (ft->l != NULL) q.push(ft->l);
if (ft->r != NULL) q.push(ft->r);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(ra, x);
}
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(rb, x);
}
if (dfs(ra, rb)) cout << "YES" << endl;
else cout << "NO" << endl;
postOrder(ra);
cout << endl;
layerOrder(ra);
return 0;
}