资源简介
二叉树的实现,是学习灯俊辉版数据结构与算法课程时的练习代码
代码片段和文件信息
/******************************************************************************************
* Data Structures in C++
* ISBN: 7-302-33064-6 & 7-302-33065-3 & 7-302-29652-2 & 7-302-26883-3
* Junhui DENG deng@tsinghua.edu.cn
* Computer Science & Technology Tsinghua University
* Copyright (c) 2006-2013. All rights reserved.
******************************************************************************************/
/******************************************************************************************
* Test of B-Tree
******************************************************************************************/
#include “BTree_test.h“
/******************************************************************************************
* Test a BTree
******************************************************************************************/
template void testBTree ( int order int n ) {
BTree bt ( order );
while ( bt.size() < n ) {
T e = dice ( ( T ) n * 3 ); //[0 3n)范围内的e
switch ( dice ( 3 ) ) {
case 0: { //查找,成功率 <= 33.3%
printf ( “Searching for “ ); print ( e ); printf ( “ ... “ );
BTNodePosi(T) p = bt.search ( e );
printf ( p ? “Found\n“ : “Not found\n“ );
break;
}
case 1: { //删除,成功率 <= 33.3%
printf ( “Removing “ ); print ( e ); printf ( “ ... “ );
bt.remove ( e ) ?
printf ( “Done\n“ ) print ( bt ) :
printf ( “Not exists\n“ );
break;
}
default: {//插入,成功率 == 100%
printf ( “Inserting “ ); print ( e ); printf ( “ ... “ );
int oldSize = bt.size();
bt.insert ( e ) ?
printf ( “Done\n“ ) print ( bt ) :
printf ( “Dup key\n“ );
break;
}
}
}
while ( bt.size() > 0 ) {
T e = dice ( ( T ) n * 3 ); //[0 3n)范围内的e
printf ( “Removing “ ); print ( e ); printf ( “ ... “ );
bt.remove ( e ) ?
printf ( “Done\n“ ) print ( bt ) :
printf ( “not exists\n“ );
}
}
/******************************************************************************************
* 测试主入口
******************************************************************************************/
int main ( int argc char* argv[] ) {
if ( 3 > argc ) { printf ( “Usage: %s \a\a\n“ argv[0] ); return 1; }
int order = atoi ( argv[1] ); if ( order < 3 ) { printf ( “Make sure the order (%d) is no less than 3.\a\a\n“ order ); return 1; }
int size = atoi ( argv[2] ); if ( size < 0 ) { printf ( “Make sure the size (%d) is no less than 0.\a\a\n“ size ); return 1; }
srand ( ( unsigned int ) time ( NULL ) );
testBTree ( atoi ( argv[1] ) atoi ( argv[2] ) ); //元素类型、比较器可以在这里任意选择
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1229 2015-01-01 01:01 btnode.h
文件 1476 2015-01-01 01:01 btree.h
文件 4543 2015-01-01 01:01 btree.vcproj
文件 1494 2015-01-01 01:01 BTree.vcproj.user
文件 991 2015-01-01 01:01 btree_implementation.h
文件 927 2015-01-01 01:01 btree_insert.h
文件 1176 2015-01-01 01:01 btree_remove.h
文件 997 2015-01-01 01:01 btree_search.h
文件 1692 2015-01-01 01:01 btree_solveoverflow.h
文件 4569 2015-01-01 01:01 btree_solveunderflow.h
文件 574 2015-01-01 01:01 btree_test.h
文件 2937 2015-01-01 01:01 main.cpp
- 上一篇:flash动画剧——网络安全
- 下一篇:邓俊辉老师的数据结构与算法课程代码bst实现
评论
共有 条评论