平衡二叉树
平衡二叉树
int getHeight(TreeNode* node) {
if (node == NULL) {
return 0;
}
int leftHeight = getHeight(node->left);
if (leftHeight == -1) return -1;
int rightHeight = getHeight(node->right);
if (rightHeight == -1) return -1;
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
}
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
typedef struct avl
{
int nValue;
int height;
struct avl* pFather;
struct avl* pLeft;
struct avl* pRight;
avl(int val, struct avl* parent) {
nValue = val;
pLeft = nullptr;
pRight = nullptr;
pFather = parent;
height = 1;
}
}AVL;
// x
// A
// B C
// D E
// x
// B
// B A
// D E C
// E变成A的左孩子
// A变成B的右孩子
// B变成X(如果存在)的孩子
template <typename T>
void RightRotate(T** pRoot)
{
if (*pRoot == nullptr) return;
if ((*pRoot)->pLeft == nullptr) return;
T* pNode = *pRoot;
T* pMark = pNode->pLeft;
pNode->pLeft = pMark->pRight;
pMark->pRight = pNode;
if (pNode->pFather) {
if (pNode == pNode->pFather->pLeft) {
pNode->pFather->pLeft = pMark;
}
else {
pNode->pFather->pRight = pMark;
}
}
else {
*pRoot = pMark;
}
if (pNode->pLeft) {
pNode->pLeft->pFather = pNode;
}
pMark->pFather = pNode->pFather;
pNode->pFather = pMark;
}
template <typename T>
void LeftRotate(T** pRoot)
{
if (*pRoot == nullptr) return;
if ((*pRoot)->pRight == nullptr) return;
T* pNode = *pRoot;
T* pMark = pNode->pRight;
pNode->pRight = pMark->pLeft;
pMark->pLeft = pNode;
if (pNode->pFather) {
if (pNode == pNode->pFather->pLeft) {
pNode->pFather->pLeft = pMark;
}
else {
pNode->pFather->pRight = pMark;
}
}
else {
*pRoot = pMark;
}
if (pNode->pRight) {
pNode->pRight->pFather = pNode;
}
pMark->pFather = pNode->pFather;
pNode->pFather = pMark;
}
int max(int a, int b) {return a > b ? a : b;}
AVL* searchNode(AVL* pRoot, int nNum)
{
if (pRoot == nullptr) return nullptr;
if (pRoot->nValue > nNum)
return searchNode(pRoot->pLeft, nNum);
else if (pRoot->nValue < nNum)
return searchNode(pRoot->pRight, nNum);
else
return pRoot;
}
int height(AVL *root)
{
return root ? root->height : 0;
}
void adjustHeight(AVL *root)
{
root->height = 1 + max(height(root->pLeft), height(root->pRight));
}
AVL* balanceTree(AVL* root)
{
if (height(root->pLeft) - height(root->pRight) > 1)
{
if (height(root->pLeft->pLeft) > height(root->pLeft->pRight))
{
RightRotate<AVL>(&root);
}
else
{
LeftRotate<AVL>(&(root->pLeft));
RightRotate<AVL>(&root);
}
}
else if (height(root->pRight) - height(root->pLeft) > 1)
{
if (height(root->pRight->pRight) > height(root->pRight->pLeft))
{
LeftRotate<AVL>(&root);
}
else
{
RightRotate<AVL>(&(root->pRight));
LeftRotate<AVL>(&root);
}
}
return root;
}
AVL *insert(AVL *root, int val)
{
AVL *current = root;
while (current->nValue != val)
{
if (val < current->nValue)
{
if (current->pLeft) current = current->pLeft;
else
{
current->pLeft = new AVL(val, current);
current = current->pLeft;
}
}
else if (val > current->nValue)
{
if (current->pRight) current = current->pRight;
else
{
current->pRight = new AVL(val, current);
current = current->pRight;
}
}
else return root; /* Value was in the tree, dumbass */
}
do
{
current = current->pFather;
adjustHeight(current);
current = balanceTree(current);
} while (current->pFather);
return current;
}
void print_tree_indent(AVL *node, int indent)
{
int ix;
for (ix = 0; ix < indent; ix++) printf(" ");
if (!node) printf("Empty child\n");
else
{
printf("node: %d; height: %d\n", node->nValue, node->height);
print_tree_indent(node->pLeft, indent + 4);
print_tree_indent(node->pRight, indent + 4);
}
}
void print_tree(AVL *node)
{
print_tree_indent(node, 0);
}
int main(int argc, char *argv[])
{
AVL *root = new AVL(1, NULL);
root = insert(root, 2);
root = insert(root, 3);
root = insert(root, 4);
root = insert(root, 5);
print_tree(root);
system("pause");
return 0;
}