c++ - Binary Tree Search - delete a node without child -
i have binary tree search, , try remove it's biggest number in tree. has crash while delete node without child. totally don't know why. here code. please me figure out it.
typedef struct node* ref; struct node { int info; int n; ref left; ref right; }; struct tree { ref head; int n; }; ref getnode(int k) { ref = new node; a->info = k; a->left = null; a->right = null; a->n = 1; return a; }; void add(int k, tree &t) { if ( t.n>15000 ) return; ref head = t.head; if(!head) t.head = getnode(k); else while( head ) { if ( k > head->info) if ( head->right ) head = head->right; else { cout<<"right "; ref temp = getnode(k); head->right = temp; break; } else if ( k < head->info ) if ( head->left ) head = head->left; else { cout<<"left "; ref temp = getnode(k); head->left = temp; break; } else { cout<<"parent "; head->n ++; break; } } t.n ++ ; }; void removemax ( tree &t ) { ref head = t.head; if( !head ) return; while ( head ) { if( head->right ) head = head->right; else { if( !head->left ) { t.n -= head->n; delete head; head = null; break; } else { t.n -= head->n; ref temp = head->left; *head = *head->left; delete temp; break; } } } }; void print(ref head) { if(!head) return; print(head->left); cout<<head->info<<" "<<head<<" "; print(head->right); }