代码随想录 打卡17

平衡二叉树


平衡二叉树

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值