Relative Content

Tag Archive for cbinary-search-tree

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, […]

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.