Recognize the knot from the tree with++



  • In fact, the sabge, the degree of the knot is the number of his supporters 0 or 1 or 2 etc., but I have a binary search tree that can't be more than 2 respectively!

    #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 &gt;&gt; x;
        add(x, root);
    }
    print(root); cout &lt;&lt; endl;
    stepen(6, root); cout &lt;&lt; endl;
    deletet(root);
    in.close();
    system("pause");
    return 0;
    

    }



  • If I understand the question correctly, it's just int(node->left != 0) + int(node->right != 0) - that's the number of daughter knots. ♪ ♪

    You seem to have a good idea, but what are you trying to do?

    void stepen(int x, tree *root)
    

    x - it's just a number, can it be? x->left! You have a knot. tree*♪ We need to find an interesting knot and watch the signs from him. ♪ ♪


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2