Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 3 . 7 LAB: Iterable 2 - 3 - 4 treeIn this lab, the Tree 2 3 4 class is extended to support iteration

13.7 LAB: Iterable 2-3-4 treeIn this lab, the Tree234 class is extended to support iteration with a range-based for loop. Such support is provided via the implementation of an iterator that can iterate through the tree's keys in ascending order.An iterator is an object that maintains a pointer to a specific element in a collection and can move to the next element. Ex: A Tree234 iterator points to the tree's minimum key upon construction. The iterator can then move to the second to minimum key, then the third to minimum, and so on. After moving past the tree's last key, the iterator can move no further.Overview of iterable objects in C++This lab requires implementation of a simplified iterator that:implements the dereference operator (*) to return the key that the iterator currently points to,implements the pre-increment operator (++) to advance to the next key,supports copy construction and copy assignment, andsupports equality and inequality comparison.Range-based for loops work on any class that implements the begin() and end() member functions such that each returns an iterator. So Tree234 implements begin() and end(). Tree234's begin() member function returns an instance of Tree234Iterator object representing the inclusive starting point of iteration: the tree's minimum key. Tree234's end() member function returns an instance of Tree234Iterator object representing the exclusive ending point of iteration: one beyond the tree's maximum key.Step 1: Inspect the Node234.h fileInspect the Node234.h file. Access Node234.h by clicking on the orange arrow next to main.cpp at the top of the coding window. Node234.h is read only and has a complete implementation of a Node234 class for a 2-3-4 tree node. Member variables are protected and so must be accessed through the provided getter and setter functions.Step 2: Inspect the Tree234Iterator.h fileInspect the Tree234Iterator.h file. The Tree234Iterator class is declared, but required member functions are not implemented. The implementation must satisfy the following requirements:Iteration never changes the tree in any wayIteration starts at the tree's minimum key and ends at the maximumConstruction occurs in worst-case O(log N) timeDereferencing executes in worst-case O(1) timeIncrementing executes in worst-case O(log N) timeSpace complexity is worst-case O(log N)For simplicity, assume the tree is not changed by an outside source during the iterator's lifetime.Step 3: Understand requirement implicationsTo satisfy the requirements, the iterator must maintain a collection of node pointers. A node exists in the collection only if that node must be revisited at some point in time.The iterator must visit only the necessary nodes to deliver a key when the iterator is dereferenced. "Visiting" a node means calling any of that node's member functions. Ex: Suppose an iterator is built for the tree below. Then the iterator's * operator is invoked to return key 5, then ++ is invoked to advance to key 10, then * is invoked to return key 10, then ++ is invoked to advance to key 15, and then * is invoked to return key 15. The iterator should have only visited the highlighted nodes.2-3-4 tree with 14 nodes and 19 keys. Root node's keys are 30 and 70. Root's left child has keys 10 and 20. Root's middle1 child has keys 40,50, and 60. Root's middle2 child has keys 80 and 90. Node (10,20) has 3 single-keyed children: 5,15, and 25. Node (40,50,60) has 4 single-keyed children: 25,45,55, and 65. Node (80,90) has 3 single-keyed children: 75,85, and 95.Step 4: Implement the Tree234Iterator classImplement the Tree234Iterator to satisfy the complexity requirements mentioned above. Code in main.cpp adds random keys to a Tree234 object, then tests that the iterator properly iterates through all keys in ascending order. But time and space complexity aren't tested by code in main.cpp. Rather, main.cpp only ensures that the iterator properly iterates through all keys.Most unit tests will fail if the iterator does not properly iterate through all the tree's keys in the correct order. So run code in develop mode and ensure that the test passes before submitting code.HintsAssume that Tree234's begin() and end() member functions return a Tree234Iterator object.Tree234 uses a template type, IteratorType, for the iterator type, allowing specialized iterators to be used for grading. So the declared return type for begin() and end() is actually IteratorType.The conceptual description of an iterator mentions that the iterator "points to" a key. In the actual implementation, the "pointer" to a key is not an int*. The iterator must know if more keys exist in the node, and a direct pointer to the key integer itself does not include such information.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

Discuss how selfesteem is developed.

Answered: 1 week ago