Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include "iostream"
- #include <algorithm>
- #include <list>
- #include <vector>
- using namespace std;
- struct Node{
- int value;
- Node* left;
- Node* right;
- Node(int num):value(num), left(NULL), right(NULL) {}
- };
- Node* Add(int num, Node* root){
- if(root == nullptr){
- root = new Node(num);
- }
- if(num < root->value){
- root->left = Add(num, root->left);
- }
- if(num > root->value) {
- root->right = Add(num, root->right);
- }
- return root;
- }
- Node* search(int num, Node* root){
- if(num == root->value){
- return root;
- }
- if(num < root->value){
- return search(num, root->left);
- }
- if(num > root->value) {
- return search(num, root->right);
- }
- return nullptr;
- }
- int height(Node *root)
- {
- Node *tmp = root;
- int h1=0, h2=0;
- if(root==nullptr)
- return 0;
- if(root->left)
- h1=height(root->left);
- if(root->right)
- h2=height(root->right);
- return (max(h1,h2)+1);
- }
- void straight(Node *root)
- {
- if(root==nullptr) return;
- cout <<root->value<< endl;
- straight(root->left);
- straight(root->right);
- }
- void por(vector <int> &TAGIR, int size)
- {
- for (int i=0;i<size;i++)
- {
- for (int j=1;j<(size);j++)
- {
- if (TAGIR[j]<TAGIR[j-1])
- {
- int b=TAGIR[j];
- TAGIR[j]=TAGIR[j-1];
- TAGIR[j-1]=b;
- }
- }
- }
- for (int i=0;i<size;i++)
- {
- //if (TAGIR[i]==0) {continue;}
- cout <<TAGIR[i]<<endl;
- }
- return ;
- }
- int main(){
- Node* root = NULL;
- int n;
- int i=0;
- int size;
- cin >> size;
- cout<<endl;
- vector<int>TAGIR(500);
- while(true){
- cin >>n;
- if (n!=0){
- root = Add(n,root);
- TAGIR[i]=n;
- i++;
- }
- else break;
- }
- int h=height(root);
- cout << endl;
- cout << h << endl;
- cout << endl;
- straight(root);
- cout << endl;
- por(TAGIR,size);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement