Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #define SIZE 5;
- using namespace std;
- struct Node
- {
- int data;
- Node *next; //Указатель на соседа
- Node *head; // Голова списка дочерних узлов
- };
- void generate(Node*& tree, int count)
- {
- queue<Node*> crap;
- tree->data=rand()%100;
- count--;
- crap.push(tree);
- while(!crap.empty() && count)
- {
- Node *curr = crap.front(); //Родительский узел, для которого генерируются дочерние узлы
- crap.pop();
- Node *temp = new Node; // Голова списка дочерних узлов
- memset(temp, 0, sizeof(Node*));
- // int val = rand() % SIZE + 1;
- int val = 5;
- temp->data = rand()%100;
- curr->head=temp;
- crap.push(temp);
- val--;
- count--;
- Node *prev = temp;
- temp=temp->next;
- while(val && count)
- {
- Node *temp = new Node;
- memset(temp, 0, sizeof(Node));
- temp->data = rand()%100;
- crap.push(temp);
- val--;
- count--;
- prev->next=temp;
- prev = temp;
- temp=temp->next;
- }
- }
- }
- void print(Node* tree)
- {
- cout << tree->data << endl;
- queue<Node*> ptr;
- ptr.push(tree);
- while(!ptr.empty())
- {
- Node *curr = ptr.front();
- ptr.pop();
- Node *temp = curr->head;
- while(temp)
- {
- cout << temp->data << " ";
- ptr.push(temp);
- temp=temp->next;
- }
- cout << endl;
- }
- }
- void main()
- {
- Node *kal = new Node;
- memset(kal, 0, sizeof(Node));
- generate(kal, 65);
- print(kal);
- cout << "yay";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement