Package sort binary Tree & random number generation (C++11)
In this paper, I try to encapsulate a sort binary tree as a template class, and use the random number generation function given by c++11 to test this class.
Sort Binary Tree Package
The binary tree is characterized by that the value of the left child is certainly smaller than the parent node, and the value of the right subtree is certainly larger than the parent node. We are required to construct a binary tree according to this structural feature, and the final sequence traversal output is the ascending output we require.
We can complete the construction of the ordered binary tree according to the specific requirements, you can use a bool function to distinguish the different sort order, the default is ascending. The new node is then continuously insert with the recursive return. Finally, we can get the sort result we need by using the middle order traversal. If you want to change the visit output mode, you can also set a function pointer. However, because of encapsulation, it is also necessary to add internal access to node within the class.
////Main.cpp//Sort////Created by Yenze on 5/9/16.//Copyright 2016 Yenze. All rights reserved.//#include <iostream>#include <vector>#include <random>#include <time.h>#include <algorithm>#include <iomanip>using namespace STD;Template<TypeNameT>BOOLCMPs (t A, t B);Template<TypeNameT>classBinaryTree { Public:structNode {T value; Node* left; node* right; Node (T Vals =0, node* lefts = null, node* rights = NULL): Value (Vals), left (lefts), right (rights) {}}; BinaryTree (Const STD:: vector<T>& Orig,BOOL(*CMP) (t A, t B) = cmps<t>); ~binarytree ();voidPrint ();Private: node* root;voidInsert (node* root, T value,BOOL(*CMP) (t A, T b));voidClear (node* temp);voidVisit (node* root);};Template<TypeNameT>BOOLCMPs (t A, T b) {if(A <= B) {return true; }Else{return false; }}Template<TypeNameT>voidBinarytree<t>::insert (Binarytree::node *root, T value,BOOL(*CMP) (t A, T B)) {if(CMP (value, Root->value)) {if(Root->left = = NULL) {node* temp =NewNode (value); Root->left = temp; }Else{Insert (Root->left, value, CMP); } }Else{if(Root->right = = NULL) {node* temp =NewNode (value); Root->right = temp; }Else{Insert (Root->right, value, CMP); } }}Template<TypeNameT>binarytree<t>::binarytree (Const STD:: vector<T>& Orig,BOOL(*CMP) (t A, T B)) {root =NewNode (orig[0]); for(inti =1; I! = Orig.size (); i++) {Insert (root, orig[i], CMP); }}Template<TypeNameT>voidBinarytree<t>::clear (node* root) {if(Root = NULL) {Clear (Root->left); Clear (Root->right);DeleteRoot }}Template<TypeNameT>binarytree<t>::~binarytree () {Clear (root);}Template<TypeNameT>voidBinarytree<t>::visit (Binarytree::node *root) {if(Root = NULL) {Visit (root->left);STD::cout<< Root->value <<" "; Visit (root->right); }}Template<TypeNameT>voidBinarytree<t>::p rint () {visit (root);}
Test function:
intMain () {intRangeSTD::Cin>> range;intTotal_num = range;STD:: vector<double>InputSTD:: Random_device RAM;STD::uniform_real_distribution<> Dis (1, range);DoubleValue =0; while(total_num--) {value = DIS (RAM); Input.push_back (value); } binarytree<Double> tree (Input); Tree.print ();return 0;}
Random Number Generation Class
Next we discuss the method of generating random numbers.
In C++11, a new feature is given to complete the generation of random numbers.
1. Random_device
The standard library provides a non-deterministic random number generation device. In the implementation of Linux, is to read the/dev/urandom device; The implementation of Windows is actually using rand_s, here strongly condemned.
The Random_device provides a () operator that returns a number between min () and Max (). If it's Linux (like or Unix), you can use this to produce high-quality random numbers that can be interpreted as true random numbers.
#include <iostream>#include <random>int main() { std::random_device rd; for(int n=0; n<20000; ++n) std::coutstd::endl; return0; }
2. Random number engine
The standard abstracts random numbers into random number engines and distributes two parts. The engine is used to generate random numbers, distributions that produce a specific distribution of random numbers (such as average distribution, positive distribution, etc.).
The standard provides three commonly used primers
Linear_congruential_engine,mersenne_twister_engine and Subtract_with_carry_engine. The first is the linear congruence algorithm , the second is Mason rotation Algorithm , the third kind of linear congruence algorithm with carry . The first is the most common, and the speed is very fast; The second is called the best pseudo-random number generator; the third kind of useless ....
The random number engine takes a shaping parameter as a seed and, if not provided, uses the default value. It is recommended to use Random_device to generate a random number as a seed . (Windows love how the whole, who called Windows Random_device is called rand_s)
#include <iostream>#include <random>int main(){ std::random_device rd; std::mt19937 mt(rd()); for(int010; n++) std::coutstd::endl; return0;}
3. Random number Distributions
Standards provide a variety of distributions, but we often use less, such as the average distribution, is too distributed ... It's easy to use.
//average distribution # Include <random> #include <iostream> int Main () {std :: Random_device Rd; std :: mt19937 Gen (rd ()); std ::uniform_int_distribution<> Dis (1 , 6 ); for (int n=0 ; N<10 ; ++n) std :: cout << Dis (gen) << "; std :: cout << ;}
//Normal distribution#include <iostream>#include <iomanip>#include <string>#include <map>#include <random>#include <cmath>intMain () {STD:: Random_device Rd;STD:: mt19937 Gen (rd ());//Values near the mean is the most likely //Standard deviation affects the dispersion of generated values from the mean STD::normal_distribution<> D (5,2);STD:: map<int, int>hist for(intn=0; n<10000; ++n) {++hist[STD:: Round (d (gen))]; } for(AutoP:hist) {STD::cout<<STD:: Fixed <<STD:: Setprecision (1) <<STD:: SETW (2) << P.first <<"'<<STD::string(p.second/ $,' * ') <<' \ n '; }}
[C + +] Package sort binary Tree & random number generation (C++11)