AVL树
来源:互联网 发布:计算机二级考试 知乎 编辑:程序博客网 时间:2024/04/29 07:03
#include<iostream>#include<queue>using namespace std;struct AVLTreeNode{ int element; AVLTreeNode* left; AVLTreeNode* right; int height; AVLTreeNode(int ele):element(ele),left(nullptr),right(nullptr),height(0){}};class AVLTree{public: AVLTree(); AVLTreeNode* Find(int ele); void Insert(int ele); void Delete(AVLTreeNode* node); void LevelTraversal(); //~AVLTree(); void RotateWithLeftChild(AVLTreeNode* &node); void RotateWithRightChild(AVLTreeNode* &node); void DoubleWithLeftChild(AVLTreeNode* &node); void DoubleWithRightChild(AVLTreeNode* &node); int GetHeight(const AVLTreeNode* node)const { if (node == nullptr) { return -1; } else { return node->height; } }private: AVLTreeNode* root; void Insert(AVLTreeNode* &node, int ele);};AVLTree::AVLTree(){ root = nullptr;}void AVLTree::Insert(AVLTreeNode* &node, int ele){ if (node == nullptr) { node = new AVLTreeNode(ele); } else if (ele < node->element) { Insert(node->left, ele); if ((GetHeight(node->left) - GetHeight(node->right)) == 2) { if (ele < node->left->element) { RotateWithLeftChild(node); } else { DoubleWithLeftChild(node); } } } else { Insert(node->right, ele); if ((GetHeight(node->right) - GetHeight(node->left)) == 2) { if (node->right->element < ele) { RotateWithRightChild(node); } else { DoubleWithRightChild(node); } } } node->height = max(GetHeight(node->left), GetHeight(node->right)) + 1;}void AVLTree::Insert(int ele){ Insert(root, ele);}void AVLTree::LevelTraversal(){ queue<AVLTreeNode*> que; que.push(root); int cur_level_size = 1; int next_level_size = 0; while (!que.empty()) { AVLTreeNode* cur = que.front(); cout << cur->element << "("<<cur->height<<")"<<" "; que.pop(); cur_level_size--; if (cur->left != nullptr) { que.push(cur->left); next_level_size++; } if (cur->right != nullptr) { que.push(cur->right); next_level_size++; } if (cur_level_size == 0) { cur_level_size = next_level_size; next_level_size = 0; cout << endl; } }}void AVLTree::RotateWithLeftChild(AVLTreeNode* &node){ AVLTreeNode* left_child = node->left; node->left = left_child->right; left_child->right = node; node->height = max(GetHeight(node->left), GetHeight(node->right)) + 1; left_child->height = max(GetHeight(left_child->left), GetHeight(left_child->right)) + 1; node = left_child;}void AVLTree::RotateWithRightChild(AVLTreeNode* &node){ AVLTreeNode* right_child = node->right; node->right = right_child->left; right_child->left = node; node->height = max(GetHeight(node->left), GetHeight(node->right)) + 1; right_child->height = max(GetHeight(right_child->left), GetHeight(right_child->right)) + 1; node = right_child;}void AVLTree::DoubleWithLeftChild(AVLTreeNode* &node){ RotateWithRightChild(node->left); RotateWithLeftChild(node);}void AVLTree::DoubleWithRightChild(AVLTreeNode* &node){ RotateWithLeftChild(node->right); RotateWithRightChild(node);}AVLTreeNode* AVLTree::Find(int ele){ AVLTreeNode* node = root; while (node != nullptr&&node->element != ele) { if (ele < node->element) { node = node->left; } else { node = node->right; } } return node;}int main(){ AVLTree avl; avl.Insert(3); avl.Insert(2); avl.Insert(1); avl.Insert(4); avl.Insert(5); avl.Insert(6); avl.Insert(7); avl.Insert(16); avl.Insert(15); avl.Insert(14); avl.Insert(13); avl.Insert(12); avl.Insert(11); avl.Insert(10); avl.Insert(8); avl.Insert(9); avl.LevelTraversal(); return 0;}
阅读全文
0 0
- AVL树
- AVL树
- AVL树
- avl树
- AVL树
- avl树
- AVL树
- AVL树
- AVL树
- AVL树
- avl树
- avl树
- AVL树
- AVL树
- AVL树
- AVL树
- AVL 树
- AVL树
- IOS input输入会白屏
- C++ IO库
- 记录下自己接百度导航的问题
- 安装wxpython for Python3.5
- 原码, 反码, 补码 详解
- AVL树
- R: Working with named objects in a loop
- LeetCode(38) Count and Say
- CodeForces 396 B.On Sum of Fractions(数论)
- C++析构函数为什么要为虚函数
- Golang gRPC 示例
- BP神经网络初阶详解
- Vue.js起步
- Hibernate和MyBatis的缓存机制和比较