void TestBinaryTreeHeight2() { BinarySearchTree<BSTNode1<int>, int> b; int test = 0; for (int i = 0; i < 10000; i++) { test = rand() % 100000; b.add(test); } assert(b.height() - b.depth(b.getNode(test)) == b.height2(b.getNode(test))); assert(b.height2(b.getRoot()) == b.height()); }
void testPart1(){ BinarySearchTree<BSTNode1<int>, int> tree = treeMaker(1); BSTNode1<int>* node = tree.getNode(10); cout << tree.size() << endl; cout << tree.height() << endl; cout << tree.height2(node) << endl << endl; BinarySearchTree<BSTNode1<int>, int> tree2 = treeMaker(2); BSTNode1<int>* node2 = tree2.getNode(20); cout << tree2.size() << endl; cout << tree2.height() << endl; cout << tree2.height2(node2) << endl << "END1" << endl; }
bool TestPartOne() { srand(time(0)); BinarySearchTree<BSTNode1<int>, int> tree; tree.add(15); tree.add(10); tree.add(14); tree.add(17); tree.add(20); tree.add(16); tree.add(19); tree.add(26); tree.add(7); tree.add(9); tree.add(5); return (tree.height() == tree.Height2()) ? true : false; }
int main() { BinarySearchTree<string> bt // only a binary tree, not a BST ( new BinaryNode<string>("Margaret", new BinaryNode<string>("John", new BinaryNode<string>("Hugh",NULL,NULL), new BinaryNode<string>("Amy",NULL,NULL) ), new BinaryNode<string>("Kay", new BinaryNode<string>("Peter",NULL,NULL), new BinaryNode<string>("Craig",NULL,NULL) ) ), "" ); cout << "bt.printTree_inorder() :\n"; bt.printTree_inorder(); cout << '\n'; cout << "bt.printTree_postorder() :\n"; bt.printTree_postorder(); cout << '\n'; cout << "bt.height(): " << bt.height() << '\n'; BinarySearchTree<int> bti // only a binary tree, not a BST ( new BinaryNode<int>(13, new BinaryNode<int>(17, new BinaryNode<int>(19,NULL,NULL), new BinaryNode<int>(20,NULL,NULL) ), new BinaryNode<int>(23, new BinaryNode<int>(24,NULL,NULL), new BinaryNode<int>(25,NULL,NULL) ) ), "" ); }
void testPart1() { cout << "Part 1" << endl; BinarySearchTree<BSTNode1<int>, int> tree; tree.add(15); tree.add(5); tree.add(16); tree.add(3); tree.add(12); tree.add(10); tree.add(13); tree.add(6); tree.add(7); tree.add(20); tree.add(18); tree.add(23); cout << "height of node: " << tree.height2(tree.getNode(5)) << endl; cout << "height of tree: " << tree.height() << endl; cout << endl; }
int main() { srand(time(0)); BinarySearchTree BST; RedBlackTree RBT; cout << "Inserting 1000 ordered elements into a Binary Search Tree and a Red Black Tree:" << endl << endl; for (int i = 0; i < 1000; i++) { BST.insert(i); RBT.insert(i); } cout << "Height of the Binary Search Tree: " << BST.height() << endl; cout << "Height of the Red Black Tree: " << RBT.height() << endl; cout << endl; BinarySearchTree BST2; RedBlackTree RBT2; cout << "Inserting 1000 random elements into a Binary Search Tree and a Red Black Tree:" << endl << endl; int r; for (int i = 0; i < 1000; i++) { r = rand() % 1000; BST2.insert(r); RBT2.insert(r); } cout << "Height of the Binary Search Tree: " << BST2.height() << endl; cout << "Height of the Red Black Tree: " << RBT2.height() << endl; cout << endl; }
int main() { // Declarations char choice = '0'; BinarySearchTree<User,string> UserTree; User temp, maximum, minimum; User* ptr; string username, password, firstName, lastName; // Read in from file ifstream infile("users.txt"); while (infile >> temp) { UserTree.insert(temp); } infile.close(); // Menu while(choice != 'Q') { cout << "Enter the corresponding number for what you want to do:" << endl << "1: Attempt to log in" << endl << "2: Register a new user" << endl << "3: Delete an existing user" << endl << "4: View total number of users" << endl << "5: View min and max username" << endl << "6: View tree height" << endl << "Q: Quit the program" << endl; cin >> choice; switch(choice) { case '1': // Authenticate User cout << endl << "Log in:" << endl << " Username: "******" Password: "******"Invalid username/password combination." << endl; } else if (ptr->authenticate(password)){ cout << endl << "Welcome " << ptr->getFirstName() << ", " << ptr->getLastName() << "." << endl << endl << endl; } else { cout << endl << "Invalid username/password combination." << endl << endl << endl; } break; case '2': // Register User cout << endl << "Enter the unique username of the user you want to create: "; cin >> username; if (!UserTree.search(username)) { cout << " Password: "******" First Name: "; cin >> firstName; cout << " Last Name: "; cin >> lastName; User newuser(username, password, firstName, lastName); UserTree.insert(newuser); cout << endl << "User successfully created!" << endl << endl << endl; } else { cout << endl << "User already exists!" << endl << endl << endl; } break; case '3': cout << endl << "Enter the user name of the user you want to remove:"; cin >> username; if (UserTree.remove(username)) { cout << endl << username << " was successfully removed!" << endl << endl << endl; } else { cout << endl << "No such user exists." << endl << endl << endl; } break; case '4': cout << endl << " There are " << UserTree.size() << " users in this tree." << endl; break; case '5': maximum = UserTree.max(); minimum = UserTree.min(); cout << endl << " Minimum Username: "******" Maximum Username: "******"The User Tree is " << UserTree.height() << " users in height." << endl << endl << endl; break; }