mình có 1 cây nhị phân có các phần tử trên đó,xong mình có 1 node khác muôn đem nó lên làm góc thì mình phải làm thế nào để thay thế góc cũ và các vị trí của các node cũ đúng với điều kiện của cây nhị phân vậy?
Theo mình bạn nên dùng giải thuật xóa 1 node trong cây nhị phân. Khi nhập vào node gốc muốn xóa nó sẽ tìm kiếm node thay thế cho mình. Code đây có gì bạn nghiên cứu thêm nhé
C Code:
void SearchStandFor(tree &p, tree &q) { if(q->right!=NULL) SearchStandFor(p,q->right); else { p->data=q->data; p=q; q=q->left; } } int Removenode(tree &root, int x) { if(root==NULL) return 0; if(root->data>x) return Removenode(root->left,x); if(root->data<x) return Removenode(root->right,x); node *p = root; if(root->left==NULL) root=root->right; else if(root->right==NULL) root=root->left; else SearchStandFor(p,root->left); delete(p); return 1; }