Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Remove in a binary search tree in C++ Having trouble implementing this function. Not sure what I did wrong but would appreciate help. Thank you.

Remove in a binary search tree in C++

Having trouble implementing this function. Not sure what I did wrong but would appreciate help. Thank you.

Code:

image text in transcribed

image text in transcribed

#include using namespace std; / * The data structure of a binary search tree node struct BinaryTreeNode { int key BinaryTreeNode* left = 0; BinaryTreeNode* right = 0; }; //Initialized to NULL //Initialized to NULL @functionality :- If tree is NULL then we do nothing. Otherwise, we compare paramater key with the key of the tree. If the parameter key is less than the key of the tree, we continue to remove on the left sub-tree. However, if it greater than the key of the tree, it will continue to remove on the right sub-tree. If they are equal, the node from the binary search tree will be removed. Now, if the node to be removed has left and right children, the successors key will be copied to the node and the successor node will be removed from the right sub-tree. However, if the node has either of right or left childern, it will point to that child and release the memory of the subject node. Lastly, if the node subject that would be removed does not have any children, the memory will be released from the subject node and a NULL will be assigned to its pointer. void remove (BinaryTreeNode*s root, const int key) { if (root == NULL) { return; 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 -0 if (key root->key) { remove (root->left, key); } else if (key > root->key) { remove (root->right, key); } else if ((root->left != NULL) 4 (root->right != NULL)) { root->key = findMin (root->right) ->key; remove (root->right, root->key); } else { BinaryTreeNode* nodeToRemove = root; root = (root->left != NULL) ? root->left : root->right; delete nodeToRemove

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

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago