Untitled

 avatar
unknown
plain_text
2 years ago
2.6 kB
4
Indexable
//Задача 27.Да се състави програма на С/С++, която използва функция за преброяване на всички преки наследници на определен елемент в дадено двоично дърво за търсене.
#include <iostream>
using namespace std;
struct elem{int key;elem *left, *right;}*root = NULL;

void add(int n, elem* &t);
void inorder(elem* t);
void postorder(elem* t);
void preorder(elem* t);
int countChildren(elem* t, int k);

int main() {
    int num, ch;
    do {
        cout << "Menu\n";
        cout << "1. Add \n";
        cout << "2.inorder\n";
        cout << "3. Preorder\n";
        cout << "4. Postorder\n";
        cout << "5. Count Children\n";
        cout << "6. Exit\n";
        cout << "Your choice:";
        cin >> ch;
        switch (ch) {
        case 1:
            cout << "num=";
            cin >> num;
            add(num, root);
            cout << endl;
            break;
        case 2:
            inorder(root);
            cout << endl;
            break;
        case 3:
            preorder(root);
            cout << endl;
            break;
        case 4:
            postorder(root);
            cout << endl;
            break;
        case 5:
            cout << "Num: ";
            cin >> num;
            int count = countChildren(root, num);
            cout << "Count: " << count << endl;
            break;
        }
    } while (ch != 6);
    system("pause");
    return 0;
}
void add(int n, elem*& t) {
    if (t == NULL) {
        t = new elem;
        t->key = n;
        t->left = t->right = NULL;
    }
    else {
        if (t->key < n) add(n, t->right);
        else add(n, t->left);
    }
}
void inorder(elem* t) {
    if (t) {
        inorder(t->left);
        cout << t->key << "  ";
        inorder(t->right);
    }
}
void postorder(elem* t) {
    if (t) {
        postorder(t->left);
        postorder(t->right);
        cout << t->key << "  ";
    }
}
void preorder(elem* t) {
    if (t) {
        cout << t->key << "  ";
        preorder(t->left);
        preorder(t->right);
    }
}

int countChildren(elem* t, int k) {
    if (t == NULL) return 0;
    else
    {
        if (t->key == k)
        {
            int count = 0;
            if (t->left != NULL) count++;
            if (t->right != NULL) count++;

            return count;
        }
        else if (t->key < k) return countChildren(t->right, k);
        else return countChildren(t->left, k);
    }
}
Editor is loading...