Specify, design, and implement a class for complete binary trees using an array
ID: 3545737 • Letter: S
Question
Specify, design, and implement a class for complete binary trees using an array representation. You should have only one method that adds a new node(since there is only one place where a node may be added, and one method that removes the last node of the tree. Code using java please
Explanation / Answer
//CONSTANT MEMBER FUNCTIONS const Item& data() const { return data_field; } const binary_tree_node* left() const { return left_field; } const binary_tree_node* right() const { return right_field; } bool is_leaf() const { return (left_field ==NULL) && (right_field ==NULL); } private: Item data_field; binary_tree_node *left_field; binary_tree_node *right_field; }; template void tree_clear(binary_tree_node*& root_ptr); //Precondition: root_ptr is the root pointer of a binary tree (whihc may be NULL for the empty tree //Postcondition: All nodes at the root or below have been returned to the heap, and root_ptr has been set to NULL. template binary_tree_node* tree_copy (const binary_tree_node* root_ptr); //Precondition: root_ptr is the root pointer of a binary tree (which may be NULL for the empty tree). //Postcondition: A copy of the binary tree has been made, and the return value is apointer to the root of this copy. template void tree_clear(binary_tree_node*& root_ptr) { if (root_ptr != NULL) { tree_clear( root_ptr->left()); tree_clear( root_ptr->right()); delete root_ptr; root_ptr = NULL; } } template binary_tree_node* tree_copy (const binary_tree_node* root_ptr) { binary_tree_node *l_ptr; binary_tree_node *r_ptr; if (root_ptr ==NULL) return NULL; else { l_ptr = tree_copy( root_ptr->left()); r_ptr = tree_copy( root_ptr->right()); return new binary_tree_node (root_ptr->data(), l_ptr, r_ptr); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.