二叉索引树java代码 二叉搜索树代码(12)


vector vs;
int i;
for (i = 1 ; i9 ; i ++)
vs.push_back(i);
vs.push_back(0);
random_shuffle(vs.begin(), vs.end());
random_shuffle(vs.begin(), vs.end());
for ( i = 0 ; i9 ; i ++)
{
m_iChess[i] = vs[i];
}
if (!EstimateUncoil(m_iChess))
{
unsigned char array[9] = {1,2,3,8,0,4,7,6,5};
memcpy(m_iTargetChess , array , 9);
}
else
{
unsigned char array[9] = {1,2,3,4,5,6,7,8,0};
memcpy(m_iTargetChess , array , 9);
}
m_iStepCount = 0;
}
数据压缩函数实现:
inline void CNineGird::ArrayToDword(unsigned char *array , DWORD data)
{
unsigned char night = 0;
for ( int i = 0 ; i9 ; i ++)
{
if (array[i] == 8)
{
night = (unsigned char)i;
break;
}
}
array[night] = 0;
data = https://www.04ip.com/post/0;
data = https://www.04ip.com/post/(DWORD)((DWORD)array[0]29 | (DWORD)array[1]26 |
(DWORD)array[2]23 | (DWORD)array[3]20 |
(DWORD)array[4]17 | (DWORD)array[5]14 |
(DWORD)array[6]11 | (DWORD)array[7]8 |
(DWORD)array[8]5 | night);
array[night] = 8;
}
解压缩时跟压缩正好相反 , 解压代码:
inline void CNineGird::DwordToArray(DWORD data , unsigned char *array)
{
unsigned char chtem;
for ( int i = 0 ; i9 ; i ++)
{
chtem = (unsigned char)(data(32 - (i + 1) * 3)0x00000007);
array[i] = chtem;
}
chtem = (unsigned char)(data0x0000001F);
array[chtem] = 8;
}
由于可扩展的数据量非常的大,加上在保存的时候使用的是DWORD类型,将每一步数据都记录在一个排序二叉树中,按从小到大从左到有的排列,搜索的时候跟每次搜索将近万次的形式比较快几乎是N次方倍 , 把几个在循环中用到的函数声明为内联函数,并在插入的时候同时搜索插入的数据会不会在树中有重复来加快总体速度 。二叉树插入代码:
inline bool CNineGird::AddTree(DWORD place , PlaceList* parent)
{
if (parent == NULL)
{
parent = new PlaceList();
parent-Left = parent-Right = NULL;
parent-Place = place;
return true;
}
if (parent-Place == place)
return false;
if (parent-Placeplace)
{
return AddTree(place , parent-Right);
}
return AddTree(place , parent-Left);
}
计算结果是奇数排列还是偶数排列的代码:
bool CNineGird::EstimateUncoil(unsigned char *array)
{
int sun = 0;
for ( int i = 0 ; i8 ; i ++)
{
for ( int j = 0 ; j9 ; j ++)
{
if (array[j] != 0)
{
if (array[j] == i +1 )
break;
if (array[j]i + 1)
sun++;
}
}
}
if (sun % 2 == 0)
return true;
else
return false;
}
移动到空格位的代码比较简单 , 只要计算是否会移动到框外面就可以了 , 并在移动的时候顺便计算一下是不是已经是目标结果,这是用来给用户手工移动是给与提示用的,代码:
inline bool CNineGird::MoveChess(unsigned char *array , int way)
{
int zero , chang;
bool moveok = false;
for ( zero = 0 ; zero9 ; zero ++)
{
if (array[zero] == 0)
break;
}
POINT pnt;
pnt.x = zero % 3;
pnt.y = int(zero / 3);
switch(way)
{
case 0 : //up
if (pnt.y + 13)
{
chang = (pnt.y + 1) * 3 + pnt.x ;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 1 : //down
if (pnt.y - 1-1)
{
chang = (pnt.y - 1) * 3 + pnt.x ;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 2 : //left
if (pnt.x + 13)

推荐阅读