Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- struct Node
- {
- int x;
- int count;
- Node *left, *right;
- };
- void Show(Node *&Tree)
- {
- if (Tree != NULL)
- {
- Show(Tree->left);
- cout << Tree->x << endl;
- Show(Tree->right);
- }
- }
- void Add_Node(int x, Node *&MyTree)
- {
- if (MyTree == NULL)
- {
- MyTree = new Node;
- MyTree->x = x;
- MyTree->left = MyTree->right = NULL;
- }
- if (x < MyTree->x)
- {
- if (MyTree->left != NULL) Add_Node(x, MyTree->left);
- else
- {
- MyTree->left = new Node;
- MyTree->left->left = MyTree->left->right = NULL;
- MyTree->left->x = x;
- }
- }
- if (x > MyTree->x)
- {
- if (MyTree->right != NULL) Add_Node(x, MyTree->right);
- else
- {
- MyTree->right = new Node;
- MyTree->right->right = MyTree->right->left = NULL;
- MyTree->right->x = x;
- }
- }
- }
- void Del(Node *&Tree)
- {
- if (Tree != NULL)
- {
- Del(Tree->left);
- Del(Tree->right);
- delete Tree;
- Tree = NULL;
- }
- }
- void Count(Node *Tree)
- {
- if (Tree != NULL)
- {
- int k;
- k++;
- Tree->count = k;
- Count(Tree->left);
- Count(Tree->right);
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL, "Russian");
- Node *Tree = NULL;
- int N;
- int number;
- cout << "Введите количество элементов "; cin >> N;
- cout << "Введите числа ";
- for (int i(0); i < N; i++)
- {
- cin >> number;
- Add_Node(number, Tree);
- }
- cout << "Полученное дерево" << endl;
- Show(Tree);
- Del(Tree);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement