AVL树的查找和插入
来源:互联网 发布:大数据在银行的应用 编辑:程序博客网 时间:2024/05/22 17:48
1、当根节点的平衡因子为2时,总体上左子树高度比右子树高2,因此总体右旋转
(1)左子树的平衡因子为0,1时,也就是左子树的左子树比它的右子树高度高时,对根节点进行一次右旋转即可
(2)左子树的平衡因子为-1时,对左子树进行一次左旋转,再对根节点进行一次右旋转
2、当根节点的平衡因子为-2时,总体上右子树高度比左子树高2,因此总体左旋转
(1)右子树的平衡因子为0,-1时,也就是右子树的右子树比它的左子树高度高时,对根节点进行一次左旋转即可
(2)左子树的平衡因子为1时,对左子树进行一次右旋转,再对根节点进行一次左旋转
#ifndef AVL_NODE#define AVL_NODE template <class Entry>struct AvlNode{ Entry entry; AvlNode<Entry> *left; AvlNode<Entry> *right; int bf; AvlNode(Entry ent) { entry = ent; left = NULL; right = NULL; bf = 0; }}; #endif template <class Entry>AvlNode<Entry> *AVL_search(AvlNode<Entry> *&root, const Entry entry) { if (root == NULL) return NULL; if (root -> entry == entry) return root; else if (root -> entry > entry) return AVL_search(root -> left, entry); else return AVL_search(root -> right, entry);} template <class Entry>void AVL_insert(AvlNode<Entry> *&root, const Entry entry) { AvlNode<Entry> *t = AVL_search(root, entry); if (t != NULL) return; bool a = insert(root, entry); return;}template <class Entry>bool insert(AvlNode<Entry> *&root, const Entry entry) { if (root == NULL) { AvlNode<Entry> *node = new AvlNode<Entry>(entry); root = node; return true; } if (entry < root -> entry) { if (root -> left == NULL && root -> right == NULL) { AvlNode<Entry> *node = new AvlNode<Entry>(entry); root -> left = node; root -> bf = 1; return true; } else if (root -> left == NULL && root -> right != NULL) { AvlNode<Entry> *node = new AvlNode<Entry>(entry); root -> left = node; root -> bf = 0; return false; } bool flag = insert(root -> left, entry); if (flag) { if (root -> bf > 0) { if (root -> left -> bf > 0) { rightRotation(root); return false; } else { left_rightRotation(root); return false; } } else if (root -> bf == 0) { root -> bf = 1; return true; } else { root -> bf = 0; return false; } } else return false; } else { if (root -> left == NULL && root -> right == NULL) { AvlNode<Entry> *node = new AvlNode<Entry>(entry); root -> right = node; root -> bf = -1; return true; } else if (root -> left != NULL && root -> right == NULL) { AvlNode<Entry> *node = new AvlNode<Entry>(entry); root -> right = node; root -> bf = 0; return false; } bool flag = insert(root -> right, entry); if (flag) { if (root -> bf < 0) { if (root -> right -> bf < 0) { leftRotation(root); return false; } else { right_leftRotation(root); return false; } } else if (root -> bf == 0) { root -> bf = -1; return true; } else { root -> bf = 0; return false; } } else return false; }}template <class Entry>void leftRotation(AvlNode<Entry> *&root) { AvlNode<Entry> *rig = root -> right; root -> right = rig -> left; rig -> left = root; root = rig; setHigh(root);}template <class Entry>void rightRotation(AvlNode<Entry> *&root) { AvlNode<Entry> *lef = root -> left; root -> left = lef -> right; lef -> right = root; root = lef; setHigh(root);}template <class Entry>void right_leftRotation(AvlNode<Entry> *&root) { rightRotation(root -> right); leftRotation(root); setHigh(root);}template <class Entry>void left_rightRotation(AvlNode<Entry> *&root) { leftRotation(root -> left); rightRotation(root); setHigh(root);}template <class Entry>int setHigh(AvlNode<Entry> *root) { if (root == NULL) return 0; int l, r; l = setHigh(root -> left); if (l == -10) return -10; r = setHigh(root -> right); if (r == -10) return -10; root -> bf = l - r; if (root -> bf >= 2 || root -> bf <= -2) return -10; if (setHigh(root -> left) >= setHigh(root -> right)) return setHigh(root -> left) + 1; else return setHigh(root -> right) + 1;}
0 0
- AVL树的查找和插入
- AVL树的查找,插入,删除
- AVL树的插入、删除、查找操作
- 平衡二叉查找树(AVL)的查找、插入、删除
- 平衡二叉查找树(AVL)的查找,插入,删除
- AVL树的插入删除查找算法实现和分析-2(树高度法)
- AVL树的插入删除查找算法实现和分析-2(树高度法)
- 平衡二叉树(AVL树)的查找,插入和删除
- AVL树的插入删除查找算法实现和分析-1(平衡因子法)
- AVL树的插入删除查找算法实现和分析-1(平衡因子法)
- AVL树的插入和删除
- AVL树的创建和插入操作
- AVL 树的插入和删除
- AVL树的插入和检查
- 【数据结构】AVL树的旋转和插入
- AVL树的插入和删除
- 平衡二叉查找树(AVL)的插入
- 算法-AVL树的插入
- 【Leetcode】Grey Code
- 【来日复制粘贴】关于排名
- ios调试高德地图api
- 解决ScheduledThreadPoolExecutor关闭连接池方法
- 结合网上的知识,用c++实现了specification模式
- AVL树的查找和插入
- 十年,在我们身边都发生了哪些变化?
- Qt4 SDK(mingw编译版)与VS2008/VS2005的集成开发环境的安装配置
- hadoop伪分布式环境搭建
- UVA - 10112 Myacm Triangles
- Hibernate实体类定义总结
- unity3d 预定义着色器预处理宏
- C#基础知识之五
- 该好好复习了,没多少时间了