1.B树
# include <iostream>
# include <algorithm>
# include <vector>
using namespace std;
class BTreeNode
{
int * keys;
int t;
BTreeNode * * C;
int n;
bool leaf;
public :
BTreeNode ( int _t, bool _leaf) ;
void insertNonFull ( int k) ;
void splitChild ( int i, BTreeNode * y) ;
void traverseleft ( BTreeNode * y) ;
void traverseright ( BTreeNode * y) ;
friend class BTree ;
} ;
class BTree
{
BTreeNode * root;
int t;
public :
BTree ( int _t)
{ root = NULL ; t = _t; }
void traverseleft ( )
{ if ( root != NULL ) root-> traverseleft ( root) ; }
void traverseright ( )
{ if ( root != NULL ) root-> traverseright ( root) ; }
void insert ( int k) ;
} ;
BTreeNode :: BTreeNode ( int t1, bool leaf1)
{
t = t1;
leaf = leaf1;
keys = new int [ 2 * t- 1 ] ;
C = new BTreeNode * [ 2 * t] ;
n = 0 ;
}
void BTreeNode :: traverseleft ( BTreeNode * y)
{
if ( y-> leaf)
{
cout<< y-> keys[ 0 ] ;
}
else {
cout<< y-> keys[ 0 ] << " " ;
traverseleft ( y-> C[ 0 ] ) ;
}
}
void BTreeNode :: traverseright ( BTreeNode * y)
{
if ( y-> leaf)
{
cout<< y-> keys[ ( y-> n) - 1 ] ;
}
else {
cout<< y-> keys[ ( y-> n) - 1 ] << " " ;
traverseright ( y-> C[ ( y-> n) ] ) ;
}
}
void BTree :: insert ( int k)
{
if ( root == NULL )
{
root = new BTreeNode ( t, true ) ;
root-> keys[ 0 ] = k;
root-> n = 1 ;
}
else
{
if ( root-> n == 2 * t- 1 )
{
BTreeNode * s = new BTreeNode ( t, false ) ;
s-> C[ 0 ] = root;
s-> splitChild ( 0 , root) ;
int i = 0 ;
if ( s-> keys[ 0 ] < k)
i++ ;
s-> C[ i] -> insertNonFull ( k) ;
root = s;
}
else
root-> insertNonFull ( k) ;
}
}
void BTreeNode :: insertNonFull ( int k)
{
int i = n- 1 ;
if ( leaf == true )
{
while ( i >= 0 && keys[ i] > k)
{
keys[ i+ 1 ] = keys[ i] ;
i-- ;
}
keys[ i+ 1 ] = k;
n = n+ 1 ;
}
else
{
while ( i >= 0 && keys[ i] > k)
i-- ;
if ( C[ i+ 1 ] -> n == 2 * t- 1 )
{
splitChild ( i+ 1 , C[ i+ 1 ] ) ;
if ( keys[ i+ 1 ] < k)
i++ ;
}
C[ i+ 1 ] -> insertNonFull ( k) ;
}
}
void BTreeNode :: splitChild ( int i, BTreeNode * y)
{
BTreeNode * z = new BTreeNode ( y-> t, y-> leaf) ;
z-> n = t - 1 ;
for ( int j = 0 ; j < t- 1 ; j++ )
z-> keys[ j] = y-> keys[ j+ t] ;
if ( y-> leaf == false )
{
for ( int j = 0 ; j < t; j++ )
z-> C[ j] = y-> C[ j+ t] ;
}
y-> n = t - 1 ;
for ( int j = n; j >= i+ 1 ; j-- )
C[ j+ 1 ] = C[ j] ;
C[ i+ 1 ] = z;
for ( int j = n- 1 ; j >= i; j-- )
keys[ j+ 1 ] = keys[ j] ;
keys[ i] = y-> keys[ t- 1 ] ;
n = n + 1 ;
}
int main ( )
{
int T, n;
cin>> T>> n;
BTree t ( T) ;
for ( int i= 1 ; i<= n; i++ ) t. insert ( i) ;
t. traverseleft ( ) ;
cout<< endl;
t. traverseright ( ) ;
return 0 ;
}
2.BST
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
typedef struct Node {
Node * lchild;
Node * rchild;
char c;
} Node, * Tree;
char str1[ 30 ] ;
char str2[ 30 ] ;
Node * createNode ( ) {
Tree node = ( Tree) malloc ( sizeof ( Node) ) ;
if ( node) {
node-> lchild = node-> rchild = nullptr ;
return node;
}
else {
printf ( "无法分配节点" ) ;
exit ( 0 ) ;
}
}
void postOrder ( Node * T) {
if ( T-> lchild != NULL ) {
postOrder ( T-> lchild) ;
}
if ( T-> rchild != NULL ) {
postOrder ( T-> rchild) ;
}
printf ( "%c" , T-> c) ;
}
void preOrder ( Tree T) {
printf ( "%c" , T-> c) ;
if ( T-> lchild != NULL ) {
preOrder ( T-> lchild) ;
}
if ( T-> rchild != NULL ) {
preOrder ( T-> rchild) ;
}
}
void midOrder ( Tree T) {
if ( T-> lchild != NULL ) {
midOrder ( T-> lchild) ;
}
printf ( "%c" , T-> c) ;
if ( T-> rchild != NULL ) {
midOrder ( T-> rchild) ;
}
}
Node * build ( int s1, int e1, int s2, int e2) {
Node * ret = createNode ( ) ;
ret-> c = str1[ s1] ;
int rootIdx;
for ( int i = s2; i <= e2; i++ ) {
if ( str2[ i] == str1[ s1] ) {
rootIdx = i;
break ;
}
}
if ( rootIdx != s2) {
ret-> lchild = build ( s1 + 1 , s1 + ( rootIdx - s2) , s2, rootIdx - 1 ) ;
}
if ( rootIdx != e2) {
ret-> rchild = build ( s1 + ( rootIdx - s2) + 1 , e1, rootIdx + 1 , e2) ;
}
return ret;
}
Node * buildTree ( ) {
Node * root = ( Tree) malloc ( sizeof ( Node) ) ;
char a;
scanf_s ( "%c" , & a) ;
if ( a == '#' ) {
root = nullptr ;
}
else {
root-> c = a;
root-> lchild = buildTree ( ) ;
root-> rchild = buildTree ( ) ;
}
return root;
}
int high_Tree ( Tree T) {
if ( T == nullptr ) {
return 0 ;
}
else {
int lh = high_Tree ( T-> lchild) ;
int rh = high_Tree ( T-> rchild) ;
return lh> rh ? lh + 1 : rh + 1 ;
}
}
int main ( ) {
while ( scanf_s ( "%s" , str1) != EOF ) {
scanf_s ( "%s" , str2) ;
int L1 = strlen ( str1) ;
int L2 = strlen ( str2) ;
Node * T = build ( 0 , L1 - 1 , 0 , L2 - 1 ) ;
postOrder ( T) ;
printf ( "%d\n" , high_Tree ( T) ) ;
printf ( "\n" ) ;
}
return 0 ;
}
3.二叉树的三种遍历输出
typedef struct _TreeNode
{
int _data;
struct _TreeNode * _left;
struct _TreeNode * _right;
} TreeNode;
void preOrderTraverse ( TreeNode * root) {
if ( root)
{
cout << root-> _data << " " ;
preOrderTraverse ( root-> _left) ;
preOrderTraverse ( root-> _right) ;
}
}
void midOrderTraverse ( TreeNode * root) {
if ( root)
{
midOrderTraverse ( root-> _left) ;
cout << root-> _data << " " ;
midOrderTraverse ( root-> _right) ;
}
}
void postOrderTraverse ( TreeNode * root) {
if ( root)
{
postOrderTraverse ( root-> _left) ;
postOrderTraverse ( root-> _right) ;
cout << root-> _data << " " ;
}
}
# include <stdio.h>
# include <iostream>
using namespace std;
typedef struct _TreeNode
{
int _data;
struct _TreeNode * _left;
struct _TreeNode * _right;
} TreeNode;
void preOrderTraverse ( TreeNode * root) {
if ( root)
{
cout << root-> _data << " " ;
preOrderTraverse ( root-> _left) ;
preOrderTraverse ( root-> _right) ;
}
}
void midOrderTraverse ( TreeNode * root) {
if ( root)
{
midOrderTraverse ( root-> _left) ;
cout << root-> _data << " " ;
midOrderTraverse ( root-> _right) ;
}
}
void postOrderTraverse ( TreeNode * root) {
if ( root)
{
postOrderTraverse ( root-> _left) ;
postOrderTraverse ( root-> _right) ;
cout << root-> _data << " " ;
}
}
int main ( int argc, char const * argv[ ] )
{
TreeNode a, b, c, d, e, f;
TreeNode* root = & a;
a. _data = 1 ;
b. _data = 2 ;
c. _data = 3 ;
d. _data = 4 ;
e. _data = 5 ;
f. _data = 6 ;
a. _left = & b;
a. _right = & c;
b. _left = & d;
b. _right = & e;
c. _left = NULL ;
c. _right = & f;
d. _left = d. _right = NULL ;
e. _left = e. _right = NULL ;
f. _left = f. _right = NULL ;
preOrderTraverse ( root) ;
cout << endl;
midOrderTraverse ( root) ;
cout << endl;
postOrderTraverse ( root) ;
cout << endl;
return 0 ;
}
4.根据先序和中序遍历确定二叉树的后序遍历
# include <iostream>
# include <string>
# include <cstring>
using namespace std;
const int M = 1024 ;
char pr[ M] ;
char in[ M] ;
struct node
{
char data;
node * l;
node * r;
} ;
void create ( node * & t, int preL, int preR, int inL, int inR) {
if ( preL > preR)
{
t = NULL ;
return ;
}
t = new node ( ) ;
t-> data = pr[ preL] ;
int index;
for ( index = inL; index <= inR; index++ ) {
if ( in[ index] == pr[ preL] ) break ;
}
int numLeft = index - inL;
create ( t-> l, preL + 1 , preL + numLeft, inL, index - 1 ) ;
create ( t-> r, preL + numLeft + 1 , preR, index + 1 , inR) ;
}
void post_display ( const node * t)
{
if ( t == NULL )
return ;
post_display ( t-> l) ;
post_display ( t-> r) ;
printf ( "%c " , t-> data) ;
}
int main ( )
{
memset ( pr, '\0' , sizeof ( pr) ) ;
memset ( in, '\0' , sizeof ( in) ) ;
string a, b;
getline ( cin, a) ;
int k = 0 ;
for ( int i = 0 ; i < a. size ( ) ; i++ ) {
if ( a[ i] != ' ' ) {
pr[ k] = a[ i] ;
k++ ;
}
}
int y = 0 ;
getline ( cin, b) ;
for ( int j = 0 ; j < b. size ( ) ; j++ ) {
if ( b[ j] != ' ' ) {
in[ y] = b[ j] ;
y++ ;
}
}
node * tree = NULL ;
create ( tree, 0 , k - 1 , 0 , y - 1 ) ;
post_display ( tree) ;
cout << endl;
memset ( pr, '\0' , sizeof ( pr) ) ;
memset ( in, '\0' , sizeof ( in) ) ;
system ( "pause" ) ;
}
5.构建二叉树并实现访问操作
class Node ( object ) :
def __init__ ( self, lchild= None , rchild= None , elem= 0 ) :
self. _elem, self. _lchild, self. _rchild = elem, lchild, rchild
@property
def elem ( self) :
if self. _elem is '#' :
return None
return self. _elem
@property
def lchid ( self) :
return self. _lchild
@property
def rchild ( self) :
return self. _rchild
@elem. setter
def elem ( self, elem) :
self. _elem = elem
@lchid. setter
def lchild ( self, lchild) :
self. _lchild = lchild
@rchild. setter
def rchild ( self, rchild) :
self. _rchild = rchild
class BinTree ( object ) :
def __init__ ( self, elem= None ) :
if elem is not None :
self. root = Node( None , None , elem)
else :
self. root = None
def append ( self, elem) :
if self. root is None :
self. root = Node( None , None , elem)
else :
queue = [ self. root]
while len ( queue) > 0 :
cur = queue. pop( 0 )
if cur. lchild is None :
cur. lchild = Node( None , None , elem)
return
elif cur. rchild is None :
cur. rchild = Node( None , None , elem)
return
else :
queue. append( cur. lchild)
queue. append( cur. rchild)
def layer_order ( self) :
if self. root is None :
print ( 'None' )
else :
queue = [ self. root]
while queue:
node = queue. pop( 0 )
print ( node. elem)
if node. lchild is not None :
queue. append( node. lchild)
if node. rchild is not None :
queue. append( node. rchild)
if __name__ == '__main__' :
MyTree = BinTree( )
temp_list = list ( input ( ) . split( " " ) )
if temp_list != [ '' ] :
for i in range ( len ( temp_list) ) :
MyTree. append( temp_list[ i] )
N = int ( input ( ) )
if N!= 1 :
for i in range ( N) :
print ( eval ( input ( ) ) )
else :
print ( "None" )
6.构建红黑树-RBTREE
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
using namespace std;
# define RED 0
# define BLACK 1
typedef struct RBTreeNode
{
char color;
int key;
struct RBTreeNode * lchild;
struct RBTreeNode * rchild;
struct RBTreeNode * parent;
} Node, * RBTree;
typedef struct rb_root
{
Node * node;
} RBRoot;
RBRoot* creat_rbtree ( )
{
RBRoot * root = ( RBRoot* ) malloc ( sizeof ( RBRoot) ) ;
root-> node = NULL ;
return root;
}
Node* creat_rbtree_node ( int key, Node * parent, Node * lchild, Node * rchild)
{
Node* p;
p = ( Node* ) malloc ( sizeof ( Node) ) ;
p-> key = key;
p-> lchild = lchild;
p-> rchild = rchild;
p-> color = BLACK;
return p;
}
void rbtree_left_rotate ( RBRoot * root, Node * x)
{
Node * y = x-> rchild;
x-> rchild = y-> lchild;
if ( y-> lchild != NULL )
y-> lchild-> parent = x;
y-> parent = x-> parent;
if ( x-> parent == NULL )
{
root-> node = y;
}
else
{
if ( x-> parent-> lchild == x)
{
x-> parent-> lchild = y;
}
else
{
x-> parent-> rchild = y;
}
}
y-> lchild = x;
x-> parent = y;
}
void rbtree_right_rotate ( RBRoot * root, Node * y)
{
Node * x = y-> lchild;
y-> lchild = x-> rchild;
if ( x-> rchild != NULL )
{
x-> rchild-> parent = y;
}
x-> parent = y-> parent;
if ( y-> parent == NULL )
{
root-> node = x;
}
else
{
if ( y-> parent-> rchild == y)
{
y-> parent-> rchild = x;
}
else
{
y-> parent-> lchild = x;
}
}
x-> rchild = y;
y-> parent = x;
}
void rbtree_insert_fixup ( RBRoot * root, Node * node)
{
Node * parent, * gparent;
while ( ( parent = node-> parent) && ( parent-> color == RED) )
{
gparent = parent-> parent;
if ( parent == gparent-> lchild)
{
{
Node * uncle = gparent-> rchild;
if ( uncle && uncle-> color == RED)
{
uncle-> color = BLACK;
parent-> color = BLACK;
gparent-> color = RED;
node = gparent;
continue ;
}
}
if ( parent-> rchild == node)
{
Node * tmp;
rbtree_left_rotate ( root, parent) ;
tmp = parent;
parent = node;
node = tmp;
}
parent-> color = BLACK;
gparent-> color = RED;
rbtree_right_rotate ( root, gparent) ;
}
else
{
{
Node * uncle = gparent-> lchild;
if ( uncle && ( uncle-> color == RED) )
{
uncle-> color = BLACK;
parent-> color = BLACK;
gparent-> color = RED;
node = gparent;
continue ;
}
}
if ( parent-> lchild == node)
{
Node * tmp;
rbtree_right_rotate ( root, parent) ;
tmp = parent;
parent = node;
node = tmp;
}
parent-> color = BLACK;
gparent-> color = RED;
rbtree_left_rotate ( root, gparent) ;
}
}
root-> node-> color = BLACK;
}
void rbtree_insert ( RBRoot * root, Node * node)
{
Node * y = NULL ;
Node * x = root-> node;
while ( x != NULL )
{
y = x;
if ( x-> key> node-> key)
{
x = x-> lchild;
}
else
{
x = x-> rchild;
}
}
node-> parent = y;
if ( y != NULL )
{
if ( node-> key< y-> key)
{
y-> lchild = node;
}
else
{
y-> rchild = node;
}
}
else
{
root-> node = node;
}
node-> color = RED;
rbtree_insert_fixup ( root, node) ;
}
int insert_rbtree ( RBRoot * root, int key)
{
Node * node;
node = creat_rbtree_node ( key, NULL , NULL , NULL ) ;
if ( node == NULL ) return - 1 ;
else rbtree_insert ( root, node) ;
return 0 ;
}
void inorder ( RBTree tree)
{
if ( tree != NULL )
{
printf ( "%d " , tree-> key) ;
inorder ( tree-> lchild) ;
inorder ( tree-> rchild) ;
}
}
void inorder_rbtree ( RBRoot * root)
{
if ( root)
inorder ( root-> node) ;
}
void Judge_color ( RBTree tree) {
if ( tree != NULL ) {
if ( tree-> color == 0 ) {
printf ( "0 " ) ;
}
else
printf ( "1 " ) ;
Judge_color ( tree-> lchild) ;
Judge_color ( tree-> rchild) ;
}
}
void Judge ( RBRoot * root) {
if ( root)
Judge_color ( root-> node) ;
}
int main ( )
{
int key[ 50 ] , k = 0 , n;
while ( cin >> n) {
key[ k] = n;
k++ ;
if ( getchar ( ) == '\n' ) break ;
}
RBRoot * root = NULL ;
root = creat_rbtree ( ) ;
for ( int i = 0 ; i< k; i++ )
{
insert_rbtree ( root, key[ i] ) ;
}
inorder_rbtree ( root) ;
printf ( "\n" ) ;
Judge ( root) ;
system ( "pause" ) ;
}
7.构造AVL树
# include <iostream>
# include <algorithm>
# include <time.h>
using namespace std;
typedef int T;
typedef struct AVLtree
{
T value;
T Height;
AVLtree* left;
AVLtree* right;
} AVLtree;
static T max ( T a, T b)
{
if ( a> b)
{
return a;
}
else
{
return b;
}
}
static T Get_H ( AVLtree* tree)
{
if ( NULL == tree)
return - 1 ;
return tree-> Height;
}
AVLtree* LL_rotation ( AVLtree* tree)
{
AVLtree* temp;
temp = tree-> left;
tree-> left = temp-> right;
temp-> right = tree;
tree-> Height = max ( Get_H ( tree-> left) , Get_H ( tree-> right) ) + 1 ;
temp-> Height = max ( Get_H ( temp-> left) , Get_H ( temp-> left) ) + 1 ;
return temp;
}
AVLtree* RR_rotation ( AVLtree* tree)
{
AVLtree* temp;
temp = tree-> right;
tree-> right = temp-> left;
temp-> left = tree;
tree-> Height = max ( Get_H ( tree-> left) , Get_H ( tree-> right) ) + 1 ;
temp-> Height = max ( Get_H ( temp-> right) , Get_H ( temp-> left) ) + 1 ;
return temp;
}
AVLtree* LR_rotation ( AVLtree* tree)
{
tree-> left = RR_rotation ( tree-> left) ;
return LL_rotation ( tree) ;
}
AVLtree* RL_rotation ( AVLtree* tree)
{
tree-> right = LL_rotation ( tree-> right) ;
return RR_rotation ( tree) ;
}
AVLtree* AVLinsert ( T x, AVLtree* tree)
{
if ( tree == NULL )
{
tree = ( AVLtree* ) malloc ( sizeof ( AVLtree) ) ;
tree-> value = x;
tree-> Height = 0 ;
tree-> left = tree-> right = NULL ;
}
else if ( tree-> value> x)
{
tree-> left = AVLinsert ( x, tree-> left) ;
if ( Get_H ( tree-> left) - Get_H ( tree-> right) == 2 )
{
if ( ( tree-> left) -> value> x)
{
tree = LL_rotation ( tree) ;
}
else
{
tree = LR_rotation ( tree) ;
}
}
}
else if ( tree-> value< x)
{
tree-> right = AVLinsert ( x, tree-> right) ;
if ( Get_H ( tree-> right) - Get_H ( tree-> left) == 2 )
{
if ( ( tree-> right) -> value< x)
{
tree = RR_rotation ( tree) ;
}
else
{
tree = RL_rotation ( tree) ;
}
}
}
tree-> Height = max ( Get_H ( tree-> left) , Get_H ( tree-> right) ) + 1 ;
return tree;
}
void AVLprint ( AVLtree* tree)
{
cout << tree-> value << endl;
if ( tree-> left != NULL )
{
AVLprint ( tree-> left) ;
}
if ( tree-> right != NULL )
{
AVLprint ( tree-> right) ;
}
}
void AVLdelete ( AVLtree* * t)
{
if ( NULL == t || NULL == * t)
return ;
AVLdelete ( & ( ( * t) -> left) ) ;
AVLdelete ( & ( ( * t) -> right) ) ;
free ( * t) ;
* t = NULL ;
}
int main ( )
{
T i= 0 , temp;
AVLtree* tree = NULL ;
srand ( time ( NULL ) ) ;
int m, key[ 100 ] ;
while ( cin >> m) {
key[ i] = m;
i++ ;
if ( getchar ( ) == '\n' ) break ;
}
for ( int j = 0 ; j < i; j++ )
{
temp = key[ j] ;
tree = AVLinsert ( temp, tree) ;
}
AVLprint ( tree) ;
AVLdelete ( & tree) ;
system ( "pause" ) ;
}
8.判断是否是AVL树
# include <iostream>
# include <algorithm>
# include <time.h>
# include <string>
# include <vector>
# include <sstream>
using namespace std;
typedef int T;
typedef struct AVLtree
{
T value;
T Height;
AVLtree* left;
AVLtree* right;
} AVLtree;
typedef AVLtree * pAVLtree;
static T max ( T a, T b)
{
if ( a> b)
{
return a;
}
else
{
return b;
}
}
static T Get_H ( AVLtree* tree)
{
if ( NULL == tree)
return - 1 ;
return tree-> Height;
}
AVLtree* LL_rotation ( AVLtree* tree)
{
AVLtree* temp;
temp = tree-> left;
tree-> left = temp-> right;
temp-> right = tree;
tree-> Height = max ( Get_H ( tree-> left) , Get_H ( tree-> right) ) + 1 ;
temp-> Height = max ( Get_H ( temp-> left) , Get_H ( temp-> left) ) + 1 ;
return temp;
}
AVLtree* RR_rotation ( AVLtree* tree)
{
AVLtree* temp;
temp = tree-> right;
tree-> right = temp-> left;
temp-> left = tree;
tree-> Height = max ( Get_H ( tree-> left) , Get_H ( tree-> right) ) + 1 ;
temp-> Height = max ( Get_H ( temp-> right) , Get_H ( temp-> left) ) + 1 ;
return temp;
}
AVLtree* LR_rotation ( AVLtree* tree)
{
tree-> left = RR_rotation ( tree-> left) ;
return LL_rotation ( tree) ;
}
AVLtree* RL_rotation ( AVLtree* tree)
{
tree-> right = LL_rotation ( tree-> right) ;
return RR_rotation ( tree) ;
}
AVLtree* AVLinsert ( T x, AVLtree* tree)
{
if ( tree == NULL )
{
tree = ( AVLtree* ) malloc ( sizeof ( AVLtree) ) ;
tree-> value = x;
tree-> Height = 0 ;
tree-> left = tree-> right = NULL ;
}
else if ( tree-> value> x)
{
tree-> left = AVLinsert ( x, tree-> left) ;
if ( Get_H ( tree-> left) - Get_H ( tree-> right) == 2 )
{
if ( ( tree-> left) -> value> x)
{
tree = LL_rotation ( tree) ;
}
else
{
tree = LR_rotation ( tree) ;
}
}
}
else if ( tree-> value< x)
{
tree-> right = AVLinsert ( x, tree-> right) ;
if ( Get_H ( tree-> right) - Get_H ( tree-> left) == 2 )
{
if ( ( tree-> right) -> value< x)
{
tree = RR_rotation ( tree) ;
}
else
{
tree = RL_rotation ( tree) ;
}
}
}
tree-> Height = max ( Get_H ( tree-> left) , Get_H ( tree-> right) ) + 1 ;
return tree;
}
void AVLdelete ( AVLtree* * t)
{
if ( NULL == t || NULL == * t)
return ;
AVLdelete ( & ( ( * t) -> left) ) ;
AVLdelete ( & ( ( * t) -> right) ) ;
free ( * t) ;
* t = NULL ;
}
int main ( )
{
string s, s1;
int i = 1 ;
vector< string> vec;
vec. push_back ( "#" ) ;
getline ( cin, s) ;
stringstream ss ( s) ;
while ( getline ( ss, s1, ' ' ) ) {
i++ ;
vec. push_back ( s1) ;
}
T temp;
int chushi[ 100 ] , chushi_c= 0 ;
AVLtree* tree = NULL ;
srand ( time ( NULL ) ) ;
for ( int k = 1 ; k < i; k++ ) {
if ( vec[ k] == "#" ) { }
else {
temp = stoi ( vec[ k] ) ;
chushi[ chushi_c] = temp;
chushi_c++ ;
tree = AVLinsert ( temp, tree) ;
}
}
int floor[ 100 ] , f_c= 0 ;
pAVLtree temp2[ 100 ] ;
int in = 0 , out = 0 ;
temp2[ in++ ] = tree;
while ( in > out) {
if ( temp2[ out] ) {
floor[ f_c] = temp2[ out] -> value;
temp2[ in++ ] = temp2[ out] -> left;
temp2[ in++ ] = temp2[ out] -> right;
f_c++ ;
}
out++ ;
}
int judge = 1 ;
for ( int p = 0 ; p < chushi_c; p++ ) {
if ( chushi[ p] == floor[ p] ) { }
else {
judge = 0 ; break ;
}
}
if ( judge == 0 )
cout << "False" ;
else
cout << "True" ;
system ( "pause" ) ;
}
9.蒙特卡洛算法计算圆周率
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;
int main ( )
{
srand ( ( int ) time ( 0 ) ) ;
double num;
double count = 0 ;
cin >> num;
for ( int i = 0 ; i < num; i++ )
{
int x = rand ( ) % 100 ;
int y = rand ( ) % 100 ;
if ( ( x - 50 ) * ( x - 50 ) + ( y - 50 ) * ( y - 50 ) <= 2500 ) {
count++ ;
}
}
cout << 4 * count / num;
}
# include <stdio.h>
# include <stdlib.h>
# define RENDER_MAX 100000
double Rand ( double L, double R) {
double ret;
ret = L + ( R- L) * rand ( ) * 1.0 / RAND_MAX;
return ret;
}
int main ( ) {
srand ( time ( NULL ) ) ;
int i;
int count = 0 ;
for ( i= 0 ; i< RENDER_MAX; i++ ) {
double x, y;
x = Rand ( - 1 , 1 ) ;
y = Rand ( - 1 , 1 ) ;
if ( x* x + y* y <= 1 ) {
count ++ ;
}
}
printf ( "%f\n" , 4.0 * count/ RENDER_MAX ) ;
}
10.戈尼斯堡七桥问题
# include <iostream>
using namespace std;
int main ( ) {
int n, m;
cin >> n >> m;
int num[ 1000 ] = { 0 } ;
int count = 1 ;
int judge = 0 ;
for ( int i = 0 ; i < 2 * m; i++ ) {
cin >> num[ i] ;
}
for ( int j = 0 ; j < 2 * m- 1 ; j++ ) {
if ( num[ j] != 0 ) {
for ( int k = j + 1 ; k < 2 * m; k++ ) {
if ( num[ j] == num[ k] ) {
count++ ;
num[ k] = 0 ;
}
}
if ( count % 2 == 1 ) {
judge++ ;
}
}
count = 1 ;
}
if ( judge == 0 ) {
cout << 1 ;
}
else
cout << 0 ;
}