Question
Data Structure in C++ Implement AVL trees (with parent pointers). Implement it as a binary search tree with integer keys (i.e., an ordered set). Your
Data Structure in C++
Implement AVL trees (with parent pointers).
Implement it as a binary search tree with integer keys (i.e., an ordered set). Your nodes should have type
struct node { int key; node* left; node* right; node* parent; int height; // Other members as needed by your tree implementation... };
Note that maintaining parent pointers complicates some of the algorithms! I would suggest implementing the basic, unbalanced BST operations first, and then adding the parent pointers and making sure everything still works, and finally adding the balancing code.
You must implement the following tree operations:
node*& find(node*& root, int value); // Search void insert(node*& root, int value); // Insertion void remove(node*& root, int value); // Deletion void print(std::ostream& out, node* root); // Print node* merge(node* t1, node* t2); // Tree-merge bool check(node* root); // Check tree for correctness
Be sure to correctly handle the case where root == nullptr (i.e., the empty tree)!
Depending on how you feel about references vs. pointers, you might prefer to change the *& (reference to a pointer) parameters to ** (double-pointer). A single pointer will not suffice, however.
The print function is mostly for your convenience in testing your code; you can print your trees in any format youd like, though Id suggest using an inorder traversal, as that will print the elements of the tree in numerical order (hopefully!). Likewise, the check function should return true if the tree has the correct tree structure (ordering, pointers connected correctly, etc.).
To merge two trees, you can simply insert all the nodes of one into the other, although there are more efficient ways of doing it
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started