Actually the subject, the degree of the node is the number of its subtrees 0 or 1 or 2, etc., but I have a binary search tree, respectively, they can not be more than 2!

#include <fstream> #include <iostream> using namespace std; struct tree { int data; tree *left,*right; }; tree *root; void add(int x, tree *&root) { if (!root) { root = new tree; root->data = x; root->left = root->right = NULL; } else if (x < root->data) add(x, root->left); else if (x > root->data) add(x, root->right); } void stepen(int x, tree *root) { if (x->left != NULL && x->right = NULL) cout << "stepen 1\n"; if (x->left == NULL && x->right != NULL) cout << "stepen 1\n"; if (x->left != NULL && x->right != NULL) cout << "stepen 2\n" ; } void deletet(tree *&root) { if (root) { delete(root->left); delete(root->right); delete root; root = NULL; } } void print(tree *root) { if (root) { if (root->left == NULL && root->right == NULL) cout << root->data << ' '; else { print(root->left); print(root->right); } } } int main() { ifstream in("input.txt"); int x; while (in.peek() != EOF) { in >> x; add(x, root); } print(root); cout << endl; stepen(6, root); cout << endl; deletet(root); in.close(); system("pause"); return 0; } 
  • Ummm ... to store this degree and to install it when filling it out? - gil9red
  • I do not understand, can I have more details? - Frams
  • well, add a numeric field to the tree and fill it in - gil9red
  • I don’t know how I can accomplish this, and with this realization of the problem - Frams

2 answers 2

If I understand the question correctly, then it is just int(node->left != 0) + int(node->right != 0) - i.e. number of child nodes ...

Your idea seems to be correct, but what are you trying to do?

 void stepen(int x, tree *root) 

x is just a number, what could be x->left ?! Your node is a tree* . You need to find the node that interests you, and look at the pointers from him ...

  • I just don `t know how to ask correctly - Frams

Copy-and-bin bin search from add . If not found, return -1, for example. If found, then

 return !!root->left + !!root->right;