Search for question
Question

/n BinarySearchTree A binary search tree supports efficiently storing and searching for elements. Write implementations in BinarySearchTree.hpp for each _impl function. The file already contains function stubs and you should replace the assert(false) with your code. For example: BinarySearchTree.hpp 1 static bool empty_impl(const Node *node) { 2 assert(false); // Replace with your code 3 } Run the public Binary Search Tree tests. 1 $ make BinarySearchTree_compile_check.exe 2 $ make BinarySearchTree_public_tests.exe 3 $ ./BinarySearchTree_public_tests.exe Write tests for BinarySearchTree in BinarySearchTree_tests.cpp using the Unit Test Framework. You'll submit these tests to the autograder. See the Unit Test Grading section. 1 $ make BinarySearchTree_tests.exe 2 $ ./BinarySearchTree_tests.exe Submit BinarySearchTree.hpp and BinarySearchTree_tests.cpp to the autograder. Setup Rename these files (VS Code (macOS), VS Code (Windows), Visual Studio, Xcode, CLI): • BinarySearchTree.hpp.starter -> BinarySearchTree.hpp BinarySearchTree_tests.cpp.starter -> BinarySearchTree_tests.cpp The BinarySearchTree tests should compile and run. The public tests and compile check will fail until you implement the functions. The test you write (BinarySearchTree_tests.cpp) will pass because the starter file only contains ASSERT_TRUE (true). 1 $ make BinarySearchTree_compile_check.exe 2 $ make BinarySearchTree_public_tests.exe 3 $ ./BinarySearchTree_public_tests.exe 4 $ make BinarySearchTree_tests.exe 5 $ /BinarySearchTree_tests.exe Configure your IDE to debug either the public tests or your own tests. Set program name to: VS Code (macOS) VS Code (Windows) Xcode Public tests ${workspaceFolder}/BinarySearch Tree_public_tests.exe Set program name to: ${workspaceFolder}/BinarySearch Tree_public_tests.exe Include compile sources: BinarySearchTree_public_tests.cpp Your own tests Set program name to: ${workspaceFolder}/BinarySearch Tree_tests.exe Set program name to: ${workspaceFolder}/BinarySearch Tree_tests.exe Include compile sources: BinarySearchTree_tests.cpp Visual Exclude files from the build: • Include BinarySearchTree_public_tests.cpp Studio • Exclude any other tests and main.cpp Exclude files from the build: • Include BinarySearch Tree_tests.cpp • Exclude any other tests and main.cpp ProTip: When writing tests for check_sorting_invariant (), you can use an iterator to break the invariant. For example: 1 2 3 4 5 6 BinarySearchTree<int> b; b.insert(1); b.insert(0); // change first datum to 2, resulting in the first broken tree above *b.begin() = 2; ASSERT_FALSE (b.check_sorting_invariant()); Data Representation The data representation for BinarySearchTree is a tree-like structure of nodes similar to that described in lecture. Each Node contains an element and pointers to left and right subtrees. The structure is self-similar. A null pointer indicates an empty tree. You must use this data representation. Do not add member variables to BinarySearchTree or Node. Public Member Functions and Iterator Interface The public member functions and iterator interface for BinarySearchTree are already implemented in the starter code. DO NOT modify the code for any of these functions. They delegate the work to private, static implementation functions, which you will write. Implementation Functions The core of the implementation for BinarySearch Tree is a collection of private, static member functions that operate on tree-like structures of nodes. You are responsible for writing the implementation of several of these functions. To disambiguate these implementation functions from the public interface functions, we have used names ending with _impl. (This is not strictly necessary, because the compiler can differentiate them based on the Node* parameter.) There are a few keys to thinking about the implementation of these functions: • The functions have no idea that such a thing as the BinarySearchTree class exists, and they shouldn't. A "tree" is not a class, but simply a tree-shaped structure of Node s. The parameter node points to the root of these nodes. • A recursive implementation depends on the idea of similar subproblems, so a "subtree" is just as much a tree as the "whole tree". That means you shouldn't need to think about "where you came from" in your implementation. • Every function should have a base case! Start by writing this part. • You only need to think about one "level" of recursion at a time. Avoid thinking about the contents of subtrees and take the recursive leap of faith. We've structured the starter code so that the first bullet point above is actually enforced by the language. Because they are static member functions, they do not have access to a receiver object (i.e. there's no this pointer). That means it's actually impossible for these functions to try to do something bad with the BinarySearchTree object (e.g. trying to access the root member variable). Instead, the implementation functions are called from the regular member functions to perform specific operations on the underlying nodes and tree structure, and are passed only a pointer to the root Node of the tree/subtree they should work with. The empty_impl function must run in constant time. It must must be able to determine and return its result immediately, without using either iteration or recursion. The rest of the implementation functions must be recursive. There are additional requirements on the kind of recursion that must be used for some functions. See comments in the starter code for details. Iteration (i.e. using loops) is not allowed in any of the _impl functions. Using the Comparator The impl functions that need to compare data take in a comparator parameter called less. Make sure to use less rather than the < operator to compare elements! The insert_impl Function The key to properly maintaining the sorting invariant lies in the implementation of the insert_impl function - this is essentially where the tree is built, and this function will make or break the whole ADT. Your insert_impl function should follow this procedure: The insert_impl Function The key to properly maintaining the sorting invariant lies in the implementation of the insert_impl function - this is essentially where the tree is built, and this function will make or break the whole ADT. Your insert_impl function should follow this procedure: 1. Handle an originally empty tree as a special case. 2. Insert the element into the appropriate place in the tree, keeping in mind the sorting invariant. You'll need to compare elements for this, and to do so make sure to use the less comparator passed in as a parameter. 3. Use the recursive leap of faith and call insert_impl itself on the left or right subtree. Hint: You do need to use the return value of the recursive call. (Why?) Important: When recursively inserting an item into the left or right subtree, be sure to replace the old left or right pointer of the current node with the result from the recursive call. This is essential, because in some cases the old tree structure (i.e. the nodes pointed to by the old left or right pointer) is not reused. Specifically, if the subtree is empty, the only way to get the current node to "know" about the newly allocated node is to use the pointer returned from the recursive call. Technicality: In some cases, the tree structure may become unbalanced (i.e. too many nodes on one side of the tree, causing it to be much deeper than necessary) and prevent efficient operation for large trees. You don't have to worry about this. Testing Pro-tip: When writing tests for functions that return a size_t (which is an unsigned integer type), compare against an unsigned literal. For example: 1 BinarySearchTree<int> b; 2 ASSERT_EQUAL (b.height(), Ou); Map Write a map abstract data type (ADT). Map is an associative container, and works just like std::map. Write implementations at the end of Map.hpp for the functions declared at the beginning of Map.hpp. The most important functions are find, insert, and the ( ) operator. Your implementations should not require much code. Reuse the functionality provided by BinarySearchTree. Run the public Map tests. 1 $ make Map_compile_check.exe 2 $ make Map_public_tests.exe 3 $ ./Map_public_tests.exe Write tests for Map in Map_tests.cpp using the Unit Test Framework. While you should write your own tests for Map to ensure that your implementation is correct, you do not have to submit your tests to the autograder. 1 $ make Map_tests.exe 2 $ ./Map_tests.exe Submit Map.hpp to the autograder. Don't forget to include the code you finished earlier, BinarySearchTree.hpp and BinarySearchTree_tests.cpp . Setup Template Parameters BinarySearchTree has two template parameters: • T - The type of elements stored within the tree. • Compare - The type of comparator object (a functor) that should be used to determine whether one element is less than another. The default type is std:: less<T>, which compares two T objects with the < operator. To compare elements in a different fashion, a custom comparator type must be specified. No Duplicates Invariant In the context of this project, duplicate values are NOT allowed in a BST. This does not need to be the case, but it avoids some distracting complications. Sorting Invariant A binary search tree is special in that the structure of the tree corresponds to a sorted ordering of elements and allows efficient searches (i.e. in logarithmic time). Every node in a well-formed binary search tree must obey this sorting invariant: • It represents an empty tree (i.e. a null Node* ). - OR - • The left subtree obeys the sorting invariant, and every element in the left subtree is less than the root element (i.e. this node). - AND - The right subtree obeys the sorting invariant, and the root element (i.e. this node) is less than every element in the right subtree. Put briefly, go left and you'll find smaller elements. Go right and you'll find bigger ones. For example, the following are all well-formed sorted binary trees: While the following are not: Valid 4 6 Valid Invalid Invalid 3 Invalid Invalid i ñ ñ s Map Examples A map is an associative container. It stores two types, key and value. Our map works just like std::map. Map<string, double> words; std::map<string, double> words; One way to use a map is a lot like an array. words ["hello"] = 1; Maps store a std:: pair type, which "glues" one key to one value. The computer science term is Tuple, a fixed-size heterogeneous container. pair<string, double> tuple; tuple.first = "world"; tuple.second = 2; words.insert(tuple); Here's a more compact way to insert a pair. words.insert({"pi", 3.14159}); The range-for loop makes it easier to iterate over a map. for (const auto &kv: words) { } const auto &word = kv.first; //key auto number = kv. second; //value cout << word << " <<number << endl; You can check if a key is in the map. The find() function returns an iterator. auto found_it = words.find("pi"); if (found it != words.end()) { const auto &word = (*found_it).first; //key auto number = (*found_it).second; //value cout << "found " << word << <<number << endl; } When using the ( ) notation, an element not found is automatically created. If the value type of the map is numeric, it will always be by default. " cout << "bleh: << words ["bleh"] << endl;