void LeftRotate(Node*& pRoot){ Node* p = pRoot->right; pRoot->right = p->left; p->left = pRoot; pRoot = p; } void RightRotate(Node*& pRoot){ Node* p = pRoot->left; pRoot->left = p->right; p->right = pRoot; pRoot = p; } void Insert(Node*& pRoot, int x){ //Insert if(pRoot == NULL) pRoot = creatNode(x); if(pRoot->key > x) Insert(pRoot->left, x); if(pRoot->key > x) Insert(pRoot->right, x); //Balance x = height(pRoot->left) - height(pRoot->right); if(x >= 2){ if(height(pRoot->left->left) > height(pRoot->left->right)) RightRotate(pRoot); // MCB Trái Trái if(height(pRoot->left->left) < height(pRoot->left->right)){ LeftRotate(pRoot->left); //MCB Trái Phải RightRotate(pRoot); } if(x<=-2){ //MCB Phải // // } } void Remove(Node*& pRoot, int x){ if(pRoot->key > x) Remove(pRoot->left, x); if(pRoot->key > x) Remove(pRoot->right, x); if(pRoot->key == x){ TH1: pRoot k có con Delete pRoot; pRoot = NULL; TH2: pRoot có 1 con p = pRoot; pRoot = pRoot-> con; delete p; TH3: pRoot có 2 con // p = Node trái nhất của cây con bên phải // p = Node phải nhất của cây con bên trái pRoot->key = p->key; remove(pRoot->left, pRoot->key); // conf nua tu lam }