HackerRank Self Balancing Tree(AVL树)
2015-12-19 22:21
986 查看
题目链接
AVL-维基百科
AVL-维基百科
/* Node is defined as : typedef struct node { int val; struct node* left; struct node* right; int ht; } node; */ int Height(node *T) { if (!T) return -1; //In this question,NULL'height is -1 return T->ht; } int Max(int a, int b) { return a > b ? a : b; } node *SingleLeft(node *k1) //单旋(RR) { node *k2; k2 = k1->right; k1->right = k2->left; k2->left = k1; k1->ht = Max(Height(k1->left), Height(k1->right)) + 1; k2->ht = Max(Height(k2->left), Height(k2->right)) + 1; return k2; } node *SingleRight(node *k1) //单旋(LL) { node *k2; k2 = k1->left; k1->left = k2->right; k2->right = k1; k1->ht = Max(Height(k1->left), Height(k1->right)) + 1; k2->ht = Max(Height(k2->left), Height(k2->right)) + 1; return k2; } node *LeftRight(node *k1) //双旋(LR) { k1->left = SingleLeft(k1->left); return SingleRight(k1); } node *RightLeft(node *k1) //双旋(RL) { k1->right = SingleRight(k1->right); return SingleLeft(k1); } int BF(node *p) { return Height(p->left) - Height(p->right); } node *insert(node *root, int val) { if (!root) { root = (node *)malloc(sizeof(node)); root->val = val; root->left = root->right = NULL; root->ht = 0; //Leaf's height is 0 } else { if (root->val < val) { root->right = insert(root->right, val); root->ht = Max(Height(root->left), Height(root->right)) + 1; if (BF(root) == -2) { if (val < root->right->val) //RL { root = RightLeft(root); } if (val > root->right->val) //RR { root = SingleLeft(root); } } } if (root->val > val) { root->left = insert(root->left, val); root->ht = Max(Height(root->left), Height(root->right)) + 1; if (BF(root) == 2) { if (val > root->left->val) //LR { root = LeftRight(root); } if (val < root->left->val) //LL { root = SingleRight(root); } } } } return root; }
完整版 #include<stdio.h> //Inserting of AVL tree #include<stdlib.h> #include<string.h> typedef struct node { int val; struct node *left; struct node *right; int ht; //Height }node; int Height(node *T) { if (!T) return 0; return T->ht; } int Max(int a, int b) { return a > b ? a : b; } node *SingleLeft(node *k1) //单旋(RR) { node *k2; k2 = k1->right; k1->right = k2->left; k2->left = k1; k1->ht = Max(Height(k1->left), Height(k1->right)) + 1; k2->ht = Max(Height(k2->left), Height(k2->right)) + 1; return k2; } node *SingleRight(node *k1) //单旋(LL) { node *k2; k2 = k1->left; k1->left = k2->right; k2->right = k1; k1->ht = Max(Height(k1->left), Height(k1->right)) + 1; k2->ht = Max(Height(k2->left), Height(k2->right)) + 1; return k2; } node *LeftRight(node *k1) //双旋(LR) { k1->left = SingleLeft(k1->left); return SingleRight(k1); } node *RightLeft(node *k1) //双旋(RL) { k1->right = SingleRight(k1->right); return SingleLeft(k1); } int BF(node *p) { return (Height(p->left) - Height(p->right)); } node *insert(node *root, int val) { if (!root) { root = (node *)malloc(sizeof(node)); root->val = val; root->left = root->right = NULL; root->ht = 1; } else { if (root->val < val) { root->right = insert(root->right, val); root->ht = Max(Height(root->left), Height(root->right)) + 1; if (BF(root) == -2) { if (val < root->right->val) //RL { root = RightLeft(root); } if (val > root->right->val) //RR { root = SingleLeft(root); } } } if (root->val > val) { root->left = insert(root->left, val); root->ht = Max(Height(root->left), Height(root->right)) + 1; if (BF(root) == 2) { if (val > root->left->val) //LR { root = LeftRight(root); } if (val < root->left->val) //LL { root = SingleRight(root); } } } } return root; } node *Create() { node *T; int val; T = NULL; scanf("%d", &val); while (val != -1) { T = insert(T, val); scanf("%d", &val); } return T; } void Inorder(node *T) { if (T) { Inorder(T->left); printf("%d ", T->val); printf("%d ", BF(T)); Inorder(T->right); } } void Preorder(node *T) { if (T) { printf("%d ", T->val); printf("%d ", BF(T)); Preorder(T->left); Preorder(T->right); } } int main() { node *T; T = Create(); //test Inorder(T); printf("\n"); Preorder(T); printf("\n"); return 0; }
相关文章推荐
- [hackerrank]Weekly Challenges - Week 11
- [学习笔记]AVL平衡二叉树
- AVL树的旋转操作 图解 最详细
- HackerRank Weekly Challenges - Week 6: Minimum Average Waiting Time 停不下来的厨师
- HackerRank Weekly Challenges - Week 6: Consecutive Subsequences 你有余我也有余
- 数据结构之平衡二叉树AVL
- BST与AVL的C++模板实现
- 基本数据结构之AVL树-简单实现
- 基本数据结构之AVL树
- 1066. Root of AVL Tree (25)
- AVL树的旋转
- AVL平衡树
- HackerRank Largest Rectangle
- HackerRank Find Median(中位数)
- HackerRank Huffman Decoding(Huffman解码)
- HackerRank Truck Tour
- C语言数据结构-树
- 平衡二叉树(AVL树)
- 1066. Root of AVL Tree (25)
- AVL树的实现