How do you delete a node in a Binary Search Tree in C++?
How do you delete the node of a tree with edge cases applied to it ..
Why do we need to replace s->element with temp->element, even though the values of the two are equal?
`struct tree{ int element; tree *left; tree *right; }; tree *Delete(int x, tree *s){ tree *temp; if(s==NULL) cout<<“Element not found”<<endl; else if(x<s->element) s->left=Delete(x, s->left); else if(x>s->element) s->right=Delete(x, s->right); else if(s->left&&s->right){ temp=findmin(s->right); s->element=temp->element; s->right=Delete(s->element, s->right); } else { temp=s; if(s->left==NULL) s=s->right; else if(s->right==NULL) s=s->left; free(temp); } return s; }` I have tried AI and asked friends, […]
Why do we need to replace s->element with temp->element, even though the values of the two are equal?
`struct tree{ int element; tree *left; tree *right; }; tree *Delete(int x, tree *s){ tree *temp; if(s==NULL) cout<<“Element not found”<<endl; else if(x<s->element) s->left=Delete(x, s->left); else if(x>s->element) s->right=Delete(x, s->right); else if(s->left&&s->right){ temp=findmin(s->right); s->element=temp->element; s->right=Delete(s->element, s->right); } else { temp=s; if(s->left==NULL) s=s->right; else if(s->right==NULL) s=s->left; free(temp); } return s; }` I have tried AI and asked friends, […]
i am trying to delete a node from binary search tree , but i am getting error : heap-use-after-free on address 0x5030000000d8 , my code
/**
Count all the paths to reach to a specific node in BST
I want to write a program in c++. In the input, in first line, two integers N(number of nodes) and P(destination node) are given. Then, in second line, those N nodes are given.
Trying to understand my mistakes – Beginner needs advice on how to make this dsi work
I have successfully completed 9 data structure exercises in C++ but the binary search tree dsi is stumping me. After it is compiled, everything looks fine, however when i try to execute this program, it crashed.
output to the binary tree console
the root is passed to the Print function, whose left and right values are Null. but the thing is, if you start looking through the debugger from the line for (size_t i = 0; i < tree.GetTreeDepth(); ++i)
then you can see that the root has nodes. but when passed to the Print function, the nodes disappear again and only the value at the root remains.