Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Node
- {
- Node* l;
- Node* r;
- int val;
- int count;
- Node(int val) :val(val), l(nullptr), r(nullptr), count(1) {};
- };
- Node *root = nullptr;
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- void add(int val, Node* cur) {
- if (cur == nullptr) {
- root = new Node(val);
- return;
- }
- if (val < cur->val) {
- if (cur->l == nullptr) {
- cur->l = new Node(val);
- return;
- }
- add(val, cur->l);
- }
- else if (val > cur->val) {
- if (cur->r == nullptr) {
- cur->r = new Node(val);
- return;
- }
- add(val, cur->r);
- }
- else
- cur->count++;
- }
- void print(Node* cur) {
- if (cur == nullptr) return;
- print(cur->l);
- fout << cur->val << " " << cur->count << "\n";
- print(cur->r);
- }
- int main() {
- int a; fin >> a;
- while (a != 0) {
- add(a, root);
- fin >> a;
- }
- print(root);
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement