خرید بک لینک

Vote count: -1

My problem is that I waa build a tree by giving a number N, which follows N lines .In the next N lines, each line will contain two numbers, and the first integer a will be the parent of second integer b, and the number in the n+1 line R represents the roots of the tree.

Here's an example.

6

1 2

2 3

3 4

2 5

2 7

2 8

1

The height of the tree will be 4 to this example.

How can I search for the first number of the line in the tree and insert the second number to be one of its children?

class Node {
public:
    Node (): data(0), childrens(NULL) { }
    Node (const int &num) : data(num), childrens(NULL) { }
    void insert(const int &num1, const int &num2)
    {
        Node *a, *b;
        a = new Node(num1); b = new Node(num2);
        if(data == 0 || data == num1){
            data = num1;
            childrens.push_back(b);
        } else {
            std::vector<Node*>::iterator it;
            it = find(childrens.begin(), childrens.end(), num1);
        }
    }
private:
    int data;
    std::vector <Node*> childrens;
};

This is my code, and I knew there's a big problem.

asked 3 mins ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 9 خرداد 1395 ساعت: 0:57

صفحه بندی