Date: 2019-04-11 18:49:18

AVL树的基本操作

 //存储结构
struct node
{
int data;
int height; //记录当前子树的高度(叶子->根)
//存储平衡因子的话,无法通过其子树算得该树的平衡因子
node *lchild, *rchild;
}; //新建结点
node *newNode(int v)
{
node *root = new node;
root->data = v;
root->height = ;
root->lchild = root->rchild = NULL;
return root;
} //获取当前结点所在高度
int GetHeight(node *root)
{
if(root == NULL)
return ;
return root->height;
} //计算结点的平衡因子
int GetBalanceFactors(node *root)
{
return GetHeight(root->lchild)-GetHeight(root->rchild);
} //更新结点高度
void UpdataHeight(node *root)
{
root->height = max(GetHeight(root->lchild), GetHeight(root->rchild))+;
} //查找
void Search(node *root, int x)
{
if(root == NULL)
return;
if(x == root->data)
//visit
else if(x < root->data)
Search(root->lchild, x);
else
Search(root->rchild, x);
} //左右旋互为逆操作
//左旋
void LeftRotation(node *&root)
{
node *temp = root->lchild; //temp指向新的根结点B
root->rchild = temp->lchild; //B的左子树给A的右子树
temp->lchild = root; //B的左子树变为A
UpdataHeight(root); //更新结点高度
UpdataHeight(temp);
root = temp; //令B成为新的根结点
} //右旋
void RightRotation(node *&root)
{
node *temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
UpdataHeight(root);
UpdataHeight(temp);
root = temp;
} /*
1.LL: A==+2, A->lchild=+1
A作为root进行右旋
2.LR: A==+2, A->lchild=-1
A->lchild作为root进行左旋 --> 转化为LL
A作为root进行右旋
3.RR: A==-2, A->rchild=-1
A作为root进行左旋
4.RL: A==-2, A->rchild=+1
A->rchild作为root进行右旋 --> 转化为RR
A作为root进行左旋
*/ //插入
void Insert(node *&root, int v)
{
if(root == NULL)
{
root = newNode(v);
return;
}
if(v < root->data)
{
Insert(root->lchild, v);
UpdataHeight(root); //更新树高
if(GetBalanceFactor(root) == )
{
if(GetBalanceFactor(root->lchild) == )
RightRotation(root);
else
{
LeftRotation(root->lchild);
RightRotation(root);
}
}
}
else
{
Insert(root->rchild, v);
UpdataHeight(root);
if(GetBalanceFactor(root) == -)
{
if(GetBalanceFactor(root->rchild) == -)
LeftRotation(root);
else
{
RightRotation(root->rchild);
LeftRotation(root);
}
}
}
} //建立
node *Create(int data[], int n)
{
node *root = NULL;
for(int i=; i<n; i++)
Insert(root, data[i]);
return root;
}

判断一棵树是否为AVL树

 #include <cstdio>
const int M = ;
int pre[M]={,,,,,,,,,};
int in[M]={,,,,,,,,,};
struct node
{
int data;
node *lchild, *rchild;
}; node *Create(int preL, int preR, int inL, int inR)
{
if(preL > preR)
return NULL;
node *root = new node;
root->data = pre[preL];
int k;
for(k=inL; k<=inR; k++)
if(in[k] == root->data)
break;
int numLeft = k-inL;
root->lchild = Create(preL+, preL+numLeft, inL, k-);
root->rchild = Create(preL+numLeft+, preR, k+, inR);
} int IsAvl = true;
int IsAVL(node *root)
{
if(root == NULL)
return -;
int bl = IsAVL(root->lchild)+;
int br = IsAVL(root->rchild)+;
if(bl-br> || bl-br<-)
IsAvl = false;
return bl>br?bl:br;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif node *root = Create(,M-,,M-);
IsAVL(root);
if(IsAvl)
printf("Yes.");
else
printf("No."); return ;
}

平衡二叉树(Self-balancing Binary Search Tree)的更多相关文章

  1. Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums where the elements are sorted in ascending order, convert ...

  2. [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)

    108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...

  3. What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?

    Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...

  4. Method for balancing binary search trees

    Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...

  5. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. pat1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  7. LeetCode108——Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  8. 【数据结构05】红-黑树基础----二叉搜索树(Binary Search Tree)

    目录 1.二分法引言 2.二叉搜索树定义 3.二叉搜索树的CRUD 4.二叉搜索树的两种极端情况 5.二叉搜索树总结 前言 在[算法04]树与二叉树中,已经介绍过了关于树的一些基本概念以及二叉树的前中 ...

  9. LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14

    108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...

随机推荐

  1. java中statickeyword

    1.static变量 依照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:还有一种是没有被static修饰的变量,叫实例变量. 两者的差别是: 对于静态变量 ...

  2. C++_homework_StackSort

    顾名思义(?)类似于单调栈?维护一个单调递减的栈.一旦准备入栈的元素大于栈顶元素,栈一直弹出直到准备入栈的元素小于等于栈顶元素,弹出的元素压入另一个tmp栈中. #include <iostre ...

  3. 感知器算法 C++

    We can estimate the weight values for our training data using stochastic gradient descent. Stochasti ...

  4. hdu2089不要62(数位dp)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. sklearn中的数据预处理和特征工程

    小伙伴们大家好~o( ̄▽ ̄)ブ,沉寂了这么久我又出来啦,这次先不翻译优质的文章了,这次我们回到Python中的机器学习,看一下Sklearn中的数据预处理和特征工程,老规矩还是先强调一下我的开发环境是 ...

  6. POJ 3230 DP

    f[i][j]=max(f[i][j],f[i-1][k]-a[k][j]+b[i][j]) i->第i天 j-–>到第j个城市 #include <cstdio> #incl ...

  7. R - Games

    Problem description Manao works on a sports TV. He's spent much time watching the football games of ...

  8. Cloudera Manager安装之利用parcels方式(在线或离线)安装单节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(Ubuntu14.04)(四)

    .. 欢迎大家,加入我的微信公众号:大数据躺过的坑     免费给分享       同时,大家可以关注我的个人博客:  http://www.cnblogs.com/zlslch/   和  http ...

  9. 在PL/SQL中使用带参数的游标

    需求:查询并输出部门名称为SALES的员工信息 SET serveroutput ON; DECLARE CURSOR c_emp(paramName VARCHAR2) IS SELECT * FR ...

  10. JQuery 数据加载中禁止操作页面

    比较常见的做法,但对我而言是第一次做,记录一下. 为了把找来的loading.gif 的背景色设置为透明,还特意装了quicktime. 有学到一些额外的东西. 先将div及img定义好 <bo ...