AVL树插入算法
来源:互联网 发布:手机更改网络 编辑:程序博客网 时间:2024/06/05 00:11
没有用到平衡因子。。。
数据结构如下:
struct avl{int data;//节点数字int frequency;//插入的次数int hight;//节点高度avl *left;//左子树avl* right;//右子树};code:
#include<stdio.h>#include<malloc.h>struct avl{int data;//节点数字int frequency;//插入的次数int hight;//节点高度avl *left;//左子树avl* right;//右子树};int max(int a,int b){return a>b?a:b;}avl* init(int root_data){avl* p=(avl*)malloc(sizeof(avl));p->data=root_data;p->frequency=1;p->left=NULL;p->right=NULL;p->hight=0;return p;}int hgt(avl *x){if(!x)return -1;return x->hight;}void leftrotate(avl * &root){avl *tmp=root->right;root->right=tmp->left;tmp->left=root;root->hight=max(hgt(root->left),hgt(root->right))+1;tmp->hight=max(hgt(tmp->left),hgt(tmp->right))+1;root=tmp;}void rightrotate(avl * &root){avl *tmp=root->left;root->left=tmp->right;tmp->right=root;root->hight=max(hgt(root->left),hgt(root->right))+1;tmp->hight=max(hgt(tmp->left),hgt(tmp->right))+1;root=tmp;}void LRrotate(avl * &root){leftrotate(root->left);rightrotate(root);}void RLrotate(avl * &root){rightrotate(root->right);leftrotate(root);}void insert(avl * &root,int x){avl *p=init(x);if(!root)root=p;else if(root->data==x)root->frequency++;else if(root->data>x){insert(root->left,x);if(hgt(root->left)-hgt(root->right)==2){if(x<root->left->data)rightrotate(root);else if(x>root->left->data)LRrotate(root);}}else if(root->data<x){insert(root->right,x);if(hgt(root->right)-hgt(root->left)==2){if(x>root->right->data)leftrotate(root);else if(x<root->right->data)RLrotate(root);}}root->hight=max(hgt(root->left),hgt(root->right))+1;}int main(){avl* root=NULL;insert(root,3);insert(root,7);insert(root,8);insert(root,2);insert(root,1);insert(root,9);insert(root,5);insert(root,10);}
- AVL树插入算法
- 算法-AVL树的插入
- AVL树的插入算法
- AVL树的创建,插入算法
- AVL 树插入算法记忆技巧总结
- AVL树插入删除
- AVL树插入小结
- 二叉查找树(AVL)插入算法Java实现
- AVL树的插入与旋转算法解析
- AVL树的插入操作
- AVL树实现(插入删除)
- [算法]AVL树
- 算法之AVL树
- AVL树常用算法算法
- 【AVL树】AVL树的插入操作以及旋转
- AVL树的插入删除查找算法实现和分析-2(树高度法)
- AVL树的插入删除查找算法实现和分析-2(树高度法)
- AVL树的插入删除查找算法实现和分析-1(平衡因子法)
- Word文档编辑心得
- 毕设格式
- 经济学
- 怎样搜免费论文
- 无需无线路由,将系统为win7的笔记本变成wifi的方法
- AVL树插入算法
- java命名规范-java编程规范
- JAVA中浅复制与深复制
- Java中 引用调用 VS 按值调用
- Eclipse调试Java的10个技巧
- java中的static
- Feed Your Mind
- The Power of Imagination
- verilog仿真