void menu() { Tree *tree = new Tree; char c; do { printf("1: View\n"); printf("2: Find\n"); printf("3: Add\n"); printf("4: Del\n"); printf("5: Clear\n"); printf("\nEsc: Exit\n"); c = getch(); switch(c) { case '1': cout << "Print:"; tree->Print(); cout << endl; break; case '2': cout << "Find:"; int val; cin >> val; bool flag; flag = tree->Exists(val); cout << (flag == true ? "true" : "false") << endl; break; case '3': cout << "Add:"; int add; cin >> add; tree->Add(add); cout << endl; break; case '4': cout << "Delete:"; int del; cin >> del; tree->Delete(del); cout << endl; break; case '5': tree->Clear(); cout << endl; break; } } while(c != 27); delete tree; }
int main(int argc, char *argv[]) { Tree<int, int> tree; unsigned int num, key, key2, root; bool error; srand(time(NULL)); build_tree(&tree, 0, 20); key = rand_int(0, 20); key2 = rand_int(0, 20); num = tree.SearchByKey(key2, &error); print_valid(&tree); cout << endl << "[Remove by key " << key << "]" << endl; if (tree.RemoveByKey(key)) { cout << "ERROR removing by key " << key << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Remove ( key " << key2 << ") by data " << num << "]" << endl; if (tree.RemoveByData(num)) { cout << "ERROR removing by data " << num << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// root = tree.Root(); cout << endl << "[Remove root " << root << " by key " << root << "]" << endl; if (tree.RemoveByKey(root)) { cout << "ERROR removing by key " << root << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Insert]" << endl; build_tree(&tree, 20, 23); print_valid(&tree); //////////////////////////////////////////////////////////////// root = tree.Root(); cout << endl << "[Remove root " << root << " by key " << root << "]" << endl; if (tree.RemoveByKey(root)) { cout << "ERROR removing by key " << root << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Clear tree]" << endl; tree.Clear(); print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Build new tree]" << endl; build_tree(&tree, 0, 5); print_valid(&tree); return 0; }