HackerRank "Self Balancing Tree"
Something to learn: Rotation ops in AVL tree does not require recursion.
https://github.com/andreimaximov/hacker-rank/blob/master/data-structures/tree/self-balancing-tree/main.cpp
node *create_node(int val)
{
node *pNew = new node;
pNew->val = val;
pNew->ht = ;
pNew->left = pNew->right = nullptr; return pNew;
} int height(node *p)
{
if(!p) return -;
return p->ht;
} int get_height(node *p)
{
if (!p) return ;
return + max(height(p->left), height(p->right));
} node *right_rotate(node *p)
{
node *pOrigRoot = p;
node *root = p->left;
pOrigRoot->left = root->right;
root->right = pOrigRoot; pOrigRoot->ht = get_height(pOrigRoot);
root->ht = get_height(root); return root;
} node *left_rotate(node *p)
{
node *pOrigRoot = p;
node *root = p->right;
pOrigRoot->right = root->left;
root->left = pOrigRoot; pOrigRoot->ht = get_height(pOrigRoot);
root->ht = get_height(root); return root;
} int balance_factor(node *p)
{
if (!p) return ;
return height(p->left) - height(p->right);
} node *balance(node *p)
{
int w = balance_factor(p); if (!p || abs(w) < )
return p; if (w > )
{
if(balance_factor(p->left) < )
{
p->left = left_rotate(p->left);
}
return right_rotate(p);
}
else
{
if(balance_factor(p->right) > )
{
p->right = right_rotate(p->right);
}
return left_rotate(p);
} return p;
} node * insert(node * root,int val)
{
if (val < root->val)
{
if(!root->left)
{
root->left = create_node(val);
}
else
{
root->left = insert(root->left, val);
}
}
else
{
if(!root->right)
{
root->right = create_node(val);
}
else
{
root->right = insert(root->right, val);
}
}
root->ht = get_height(root); return balance(root);
}
HackerRank "Self Balancing Tree"的更多相关文章
- *[hackerrank]Cut the tree
https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部 ...
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- HackerRank "Kundu and Tree" !!
Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html Idea is: we union all pure black edg ...
- *[hackerrank]Tree Covering
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...
- 【HackerRank】Cut the tree
题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...
- 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 ...
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python)
A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON ...
- HackerRank "The Indian Job"
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...
随机推荐
- CentOS安装时小坑记录
在安装CentOS的时候,由于第一次安装小白,将VM虚拟机的内存设置为512M,导致进行安装的时候无法进入正常的画面安装模式,只能使用简版安装界面,可能对于很多小白不是很熟悉,特此记录,安装CentO ...
- cocos2d-html5版日历组件
根据一as3版本给改编成成了js版的.as3版本的地址: http://download.csdn.net/detail/fengye513/2722289 胜利项目做的差不多了,由于项目用的是c+ ...
- ubuntu13 安装并配置ssh,交换密钥
1.下载安装sudo apt-get install ssh,若出现问题,可选择安装openssh-server,或ssh-server等 2.启动ssh,命令:sudo /etc/init.d/ss ...
- abap程序修改程序
*&———————————————————————**& Report ZHELI_CODE*&*&———————————————————————**&*&am ...
- ZOJ 1076 Gene Assembly
原题链接 题目大意:首先学习一个生物学的单词,exon:外显子,DNA序列中能够翻译表达的片段.给出很多外显子的起始点和终点,求寻找包含最多外显子的一条链,并且输出这些外显子的编号. 解法:先把所有外 ...
- AspxGridView
转:出处 http://i.cnblogs.com/EditPosts.aspx?opt=1 AspxGridView点滴 1:页码设置 <SettingsPager> & ...
- install usb serial
Install driver for USB-UART bridge converter on Linux Ubuntu12.04 Ubuntu下USB转串口芯片驱动程序安装,支持cp210x,pl2 ...
- 工作中遇到的问题--实现CustomerSetting的实时更新
首先在项目运行时就初始化CustomerSettings的值,采用@Bean,默认是singtone模式,只会加载一次. @Configuration@Order(3)@EnableWebMvcSec ...
- URAL 1160 Network(最小生成树)
Network Time limit: 1.0 secondMemory limit: 64 MB Andrew is working as system administrator and is p ...
- 固定虚拟机的IP