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"的更多相关文章

  1. *[hackerrank]Cut the tree

    https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部 ...

  2. 【HackerRank】Utopian tree

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...

  3. HackerRank "Kundu and Tree" !!

    Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html Idea is: we union all pure black edg ...

  4. *[hackerrank]Tree Covering

    https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...

  5. 【HackerRank】Cut the tree

    题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...

  6. 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 ...

  7. POJ 1655 Balancing Act 树的重心

    Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...

  8. 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  ...

  9. HackerRank "The Indian Job"

    A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...

随机推荐

  1. PAT (Basic Level) Practise:1036. 跟奥巴马一起编程

    [题目链接] 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在 ...

  2. 【转】java环境配置

    JAVA开发环境的搭建(配置JAVA开发环境) 一.安装JDK 1. JVM(Java Virtual Machine—Java虚拟机) JRE(Java Runtime Environment—Ja ...

  3. 使用oss批量上传图片

    <?php set_time_limit(0);// 引入自动加载类// 确保路径是否正确require_once 'autoload.php';// 确定参数 需要申请$accessKeyId ...

  4. Matlab位运算操作

    本文为转载他人文章: bitand 按位与操作 a = 7; b = bitand(10,a); disp(dec2bin(a,8)); %ans = 00000111 disp(dec2bin(b, ...

  5. Windows 10触摸板手势

    高級使用者試用 Windows 10 筆記本電腦的觸控板上的這些手勢: •選擇一項: 在觸控板上點擊. •滾動: 將兩根手指放在觸控板上,然後以水準或垂直方向滑動. •放大或縮小: 將兩根手指放在觸控 ...

  6. indexOf()的用法

    indexOf()的用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判断和截取字符串! indexOf()定义和用法indexOf( ...

  7. Makefile---make内嵌函数及make命令显示 (九)

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 这一节我们讲一下make的函数,在之前的章节已经讲到了几个函数:wildcard.patsubs ...

  8. JavaWeb学习记录(九)——Cookie的增加、删除、查看

    一.servlet功能代码: public void doGet(HttpServletRequest request, HttpServletResponse response)           ...

  9. C++@类的静态成员变量和静态成员函数

    参考: http://blog.csdn.net/morewindows/article/details/6721430 http://www.cnblogs.com/lzjsky/archive/2 ...

  10. spring源码学习之:spring容器的applicationContext启动过程

    Spring 容器像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式进行工作.如果我们将Spring容器比喻为一辆汽车,可以将 BeanFactory看成汽车的发动机, ...