本文整理汇总了C++中AVL类的典型用法代码示例。如果您正苦于以下问题:C++ AVL类的具体用法?C++ AVL怎么用?C++ AVL使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AVL类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//Main function
int main()
{
AVL *a = new AVL();
string str;
int val;
cin >> str;
clock_t start, end;
start = clock();
while(!cin.eof())
{
if(str.compare("insert") == 0)
{
cin >> val;
if((a->access(val)) == true)
cout << "Element already inserted" << endl;
else
{
a->insert(val);
cout << "Element inserted" << endl;
}
}
if(str.compare("access") == 0)
{
cin >> val;
if((a->access(val)) == true)
cout << "Element accessed" << endl;
else
cout << "Element not found" << endl;
}
示例2: main
int main() {
AVL avl;
avl.insert(5);
avl.print();
avl.insert(3);
avl.print();
}
示例3: search
int AVLHash :: search(int k)
{
int pos = hashCode(k);
AVL *tree = hash_table[pos];
return tree->search(k);
}
示例4: insert
void AVLHash :: insert(int k, int v)
{
int pos;
AVL *tree;
pos = hashCode(k);
tree = hash_table[pos];
tree->insert(k, v);
}
示例5: printTreeStatus
void printTreeStatus(const AVL<T> &t) {
cout << "----------------------------------------" << endl;
if (t.isEmpty()) cout << "The tree is empty" << endl;
else {
cout << "Tree root is: " << t.root() << endl;
cout << "Tree height is: " << t.height() << endl;
cout << "Tree contains " << t.size() << " elements" << endl;
}
cout << "----------------------------------------" << endl;
}
示例6: main
int main()
{
int n,x;
AVL <int> avl;
cout<<"input the number of elements:";
cin>>n;
for(int i=0;i<n;i++)
cin>>x,avl.insert(x);
avl.print();
return 0;
}
示例7: main
int main(void){
AVL tree;
tree.insert(10);
tree.insert(30);
tree.insert(20);
tree.insert(15);
tree.insert(18);
tree.insert(16);
cout << "*************************" << endl;
tree.inOrderPrint();
}
示例8: main
int main(void){
AVL avl;
avl.insert(10);
avl.insert(5);
avl.insert(20);
avl.insert(3);
avl.insert(15);
avl.insert(8);
avl.insert(25);
avl.insert(7);
avl.insert(2);
avl.insert(9);
avl.insert(6);
}
示例9: main
int main(){
FileIO * file = new FileIO("tube.txt");
vector<Station *> station_list = file->read_file_to_station_list();
vector<Station *> sorted_list = file->read_file_to_station_list();
std::sort(sorted_list.begin(), sorted_list.end(), sort_by_name);
// print_all(sorted_list, station_list);
// search_by_keyword(sorted_list, station_list);
// mode_selection(sorted_list, station_list);
//test section
node<int> *_node = new node<int>();
// cout<<sorted_list[0]->get_station_index()<<endl;
_node->data = sorted_list[0]->get_station_index();
AVL<int> *tree = new AVL<int>(_node); //new binary_tree<int>(_node);
for(vector<Station *>::iterator it = sorted_list.begin() + 1;it != sorted_list.end();++it){
bool b_return = tree->insert((*it)->get_station_index());
// cout<<(*it)->get_station_index()<<" "<<b_return<<endl;
}
cout<<"done!"<<endl;
// tree->traverse_inorder(tree->root);
cout<<"Tree height is "<<tree->tree_height<<endl;
node<int> *returned_node = tree->search(0);
cout<<returned_node->data<<" "<<returned_node->height<<endl;
node<int> *rhs = tree->root;
// cout<<rhs->data<<" "<<rhs->height<<endl;
rhs = rhs->rhs;
node<int> min, max;
// cout<<"d 1"<<endl;
tree->find_min_from(rhs, min);
// cout<<"d 2"<<endl;
tree->find_max_from((tree->root)->lhs, max);
cout<<"min in right branch is "<<min.data<<" "<<min.height<<endl;
cout<<"max in left branch is "<<max.data<<" "<<max.height<<endl;
cout<<"depth is "<<tree->find_depth(tree->root)<<endl;
cout<<"left branch depth is "<<tree->find_depth((tree->root)->lhs)<<endl;
cout<<"right branch depth is "<<tree->find_depth((tree->root)->rhs)<<endl;
cout<<"balance factor is "<<tree->balance_factor(tree->root)<<endl;
cout<<"tree is balanced? "<<tree->is_balanced(tree->root)<<endl;
cout<<"tree root "<<(tree->root)->data<<endl;
// delete[] tree->root;
delete tree;
return 0;
}
示例10: ARC033_C
signed ARC033_C(){
int q;
cin>>q;
AVL<Int> avl;
for(int i=0;i<q;i++){
int t,x;
cin>>t>>x;
if(t==1) avl.insert(x);
else{
Int key=avl.rank(x-1)->key;
cout<<key<<endl;
avl.erase(key);
}
}
return 0;
}
示例11: ARC028_B
signed ARC028_B(){
int n,k;
cin>>n>>k;
int x[n];
for(int i=0;i<n;i++) cin>>x[i];
map<int,int> m;
for(int i=0;i<n;i++) m[x[i]]=i+1;
AVL<Int> avl;
for(int i=0;i<k-1;i++) avl.insert(x[i]);
for(int i=k-1;i<n;i++){
avl.insert(x[i]);
Int key=avl.rank(k-1)->key;
cout<<m[key]<<endl;
assert(avl.index(key)==k-1);
}
return 0;
}
示例12: main
/**** Programa Principal ******************************************************/
int main(){
//Variables locales
AVL<int> v;
int e;
int op=0;
while (op!=12)
{
cout<<"________________________________________________________________________________"<<endl
<<" ADT Arbol AVL"<<endl
<<"________________________________________________________________________________"<<endl<<endl
<<" ___________________________________________________________"<<endl
<<" | [ 1 ] Meter elemento |"<<endl
<<" | [ 2 ] Sacar elemento |"<<endl
<<" | [ 3 ] Inorden |"<<endl
<<" | [ 4 ] Preorden |"<<endl
<<" | [ 5 ] Postorden |"<<endl
<<" | [ 6 ] Inorden Inv. |"<<endl
<<" | [ 7 ] Preorden Inv |"<<endl
<<" | [ 8 ] Postorden Inv |"<<endl
<<" | [ 9 ] Hijos |"<<endl
<<" | [ 10 ] Camino |"<<endl
<<" | [ 11 ] Altura |"<<endl
<<" |___________________________________________________________|"<<endl<<endl
<<" Opcion a Elegir: ";
cin>>op;
system("cls");
if (op==1)
{
cout<<"Dame un numero a insertar ";
cin>>e;
v.meter(e);
system("pause");
system("cls");
}
if (op==2)
{
cout<<"Dame un numero a sacar ";
cin>>e;
v.sacar(e);
system("pause");
system("cls");
}
示例13: isInOrder
bool isInOrder(AVL<T> const &in) {
vector<T> list = in.inOrder();
for (auto it = list.begin() + 1; it < list.end(); it++) {
if (*it < *(it - 1))
return false;
}
return true;
}
示例14: main
int main() {
AVL<int>* avl = new AVL<int>();
//AVL<double>* avlD = new AVL<double>();
//AVL<std::string>* avlS = new AVL<std::string>();
cout << "Pre Order Traversal Print" << endl;
cout << endl;
avl->insert(4);
avl->insert(2);
avl->insert(3);
avl->insert(7);
avl->insert(1);
avl->insert(17);
avl->insert(16);
avl->insert(5);
avl->print();
cout << endl;
/*
cout << "Post Order Traversal" << endl;
avl->postOrderTraversal();
cout << endl;
cout << "In Order Traversal" << endl;
avl->inOrderTraversal();
cout << endl;
cout << "End!" << endl;*/
}
示例15: testAVL
/******************************************************************************************
* Test an AVL
******************************************************************************************/
template <typename T> void testAVL(int n) {
AVL<T>* avl = new AVL<T>;
while (avl->size() < n) {
T e = dice((T)n*3); //[0, 3n)范围内的e
switch (dice(3)) {
case 0: { //查找,成功率 <= 33.3%
printf("Searching for "); print(e); printf(" ...\n");
BinNodePosi(T) & p = avl->search(e);
p ?
printf("Found with"), print(p), printf("\n") :
printf("Not found\n");
break;
}
case 1: { //删除,成功率 <= 33.3%
printf("Removing "); print(e); printf(" ...\n");
avl->remove(e) ? printf("Done\n"), print(avl) : printf("Not exists\n");
break;
}
default: {//插入,成功率 == 100%
printf("Inserting "); print(e); printf(" ...\n");
BinNodePosi(T) p = avl->insert(e);
printf("Done with"), print(p), printf("\n"), print(avl);
break;
}
}
}
while (avl->size() > 0) {
T e = dice((T)n*3); //[0, 3n)范围内的e
printf("Removing "); print(e); printf(" ...\n");
avl->remove(e) ? printf("Done\n"), print(avl) : printf("Not exists\n");
}
release(avl);
}