Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct TREE{
- int num;
- int count;
- TREE *left, *right;
- };
- TREE* root = NULL;
- TREE* create(int n){
- TREE *t = new TREE();
- t->num = n;
- t->count = 0;
- t->left = t->right = NULL;
- return t;
- }
- void addson(TREE* t, TREE* p){
- if(t->num == p->num){
- t->count++;
- return;
- }
- if(t->num > p->num){
- if(p->right == NULL){
- p->right = t;
- return;
- }
- else
- addson(t, p->right);
- }else{
- if(p->left == NULL){
- p->left = t;
- return;
- }
- else
- addson(t, p->left);
- }
- }
- TREE* finder(TREE* node, int n){
- if(node == NULL)
- return NULL;
- if(n == node->num)
- return node;
- if(n > node->num)
- return finder(node->right, n);
- else
- return finder(node->left, n);
- }
- void add(int n){
- TREE* index = create(n);
- if(root == NULL){
- root = index;
- return;
- }
- addson(index, root);
- }
- void print(TREE* cur){
- if(cur != NULL){
- print(cur->left);
- printf("%d ", cur->num);
- print(cur->right);
- }
- }
- int main(){
- int n, num;
- scanf("%d", &n);
- for(int i = 0; i < n; i++){
- scanf("%d", &num);
- add(num);
- }
- print(root);
- scanf("%d", &num);
- TREE* m = finder(root, num);
- printf("%d ", m->count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement