Bootstrap

由递增序列生成平衡的查找二叉树

#包括“Buildable_tree.h”
模板<class Record>
无效Buildable_tree <RECORD> :: build_insert(诠释计数,常量记录和新数据,列表<Binary_node <RECORD> *>&last_node)
{
整数水平;
为(等级= 1;计数%2 == 0;等级+ +)
计数/ = 2;
Binary_node <RECORD> * next_node =新Binary_node <RECORD>(新数据),*父; last_node.retrieve(1级,next_node - >左);


 


如果(电平> = last_node.size())
last_node.insert(水平,next_node);
其他
last_node.replace(水平,next_node);
如果(last_node.retrieve(+1级,父)==成功&&父 - >右键== NULL)
母公司- >右= next_node;
} <class Record>模板Binary_node <RECORD> * Buildable_tree <RECORD> :: find_root(常量列表<Binary_node <RECORD> *>&last_node){







Binary_node <RECORD> * high_node;
last_node.retrieve(last_node.size()-1,high_node);
返回high_node;
} <class Record>模板无效Buildable_tree <RECORD> :: connect_trees(常量列表<Binary_node <RECORD> *>&last_node){





Binary_node <RECORD> * high_node,* low_node;
整型高级别的= last_node.size()-1;
整型带低位;
而(高级别的> 2)的
{
last_node.retrieve(高级别的,high_node);
如果(high_node - >右!= NULL)
高级别的 - ;
其他
{
带低位=高级别的;

{
last_node.retrieve( - 带低位,low_node);
}而(low_node! = NULL && low_node - >数据<high​​_node->数据);

high_node - >右键= low_node;
高级别的=带低位;
}
}
} 模板<class Record> ERROR_CODE Buildable_tree <RECORD> :: build_tree(名单<RECORD>与供应){





ERROR_CODE OUT_DATA =成功;
列表<Binary_node <RECORD> *> last_node;
Binary_node <RECORD> *没有= NULL;
last_node.insert(0,无);
记录X,last_x;
而(supply.retrieve(计数,X)==成功)
{ 如果(计数> 0 && x <= last_x)
 
{
OUT_DATA =失败;
打破;
}
build_insert(+ +计数,X,last_node);
last_x = X;
}
根= find_root(last_node);
connect_trees(last_node);
返回OUT_DATA;
}
<class Record>模板
无效Buildable_tree <RECORD> ::晴(Binary_node <RECORD> *&sub_root)
{
如果(sub_root == NULL)
返回;
清晰(sub_root - >右);
清晰(sub_root - >左);
删除sub_root;
sub_root = NULL;
} <class Record>模板无效Buildable_tree <RECORD> ::晴(){





清除(根);
}


































;