Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct bst {
- struct node {
- int k;
- node *l, *r;
- node(int x = 0): k(x), l(nullptr), r(nullptr) {}
- }*root;
- //constructor
- bst(): root(nullptr) {}
- //insert
- void _insert(int x, node* cur) {
- //border
- if (!root) {root = new node(x); return;}
- //common
- if (x < cur->k) {
- //go left
- if (cur->l) _insert(x, cur->l);
- else cur->l = new node(x);
- } else {
- if (cur->r) _insert(x, cur->r);
- else cur->r = new node(x);
- }
- }
- void insert(int x) {
- _insert(x, root);
- }
- //print
- void _print(node* cur) {
- if (!cur) return;
- _print(cur->l);
- cout << cur->k << ' ';
- _print(cur->r);
- }
- void print() {
- _print(root);
- }
- };
- inline void solve() {
- bst tree;
- for (auto && i : {1, 5, 4, 2, 0, 3, 6, 9, 7, 8})
- tree.insert(i);
- tree.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment