平衡二叉树:
查询复杂度为O(logn);
左子树与右子树绝对值不超过1(平衡因子为左子树与右子树之差);
node* newNode(int v)//生成新节点,v为节点权值
{
node *Node =new node;//申请node型的地址空间
Node->v=v;
Node->height=1;//高度初始化为1;
Node->lchild=Node->rchild=NULL;
return Node;//返回新节点的地址
}
int getHeight(node *root)//获取高度
{
if(root==NULL)
return 0;//空节点高度为0;
return root->height;
}
int getBalanceFactor(node *root)//获取平衡因子
{
return getHeight(root->lchild)-getHeight(root-rchild);//zuo左子树高度减去右子树高度;
}
void updateHeight(node *root)//更新根节点高度
{
root->height=max(getHeight(root->rchild),getHeight(root->rchild))+1;//子树高度最大值+1;
max函数属于<algorithm>;
}