AVL树进行插入的非递归函数
来源:互联网 发布:linux坏了怎么卸载 编辑:程序博客网 时间:2024/06/06 04:00
数据结构与算法分析——c语言描述 练习4.19 答案
挺有意思的。改递归为非递归。其实原理还是一样,都用了栈。
AvlTree insert(ElementType X, AvlTree t) {AvlTree root = t;std::stack<AvlTree> route;while (1) {if (t == NULL) {t = (AvlTree)malloc(sizeof(struct AvlNode));if (t == NULL)Error("out of memory");t->element = X;t->left = NULL;t->right = NULL;t->height = 0;t->isDeleted = 0;route.push(t);break;}else if (X < retrieve(t)) {route.push(t);t = t->left;continue;}else if (X >retrieve(t)) {route.push(t);t = t->right;continue;}else {t->isDeleted = 0;return root;}}AvlTree father,son;while (1) {son = route.top();route.pop();if (route.empty())return son;father = route.top();route.pop();if (father->element < son->element) {//儿子在右边father->right = son;if (height(father->right) - height(father->left) == 2) {if (X > retrieve(father->right))father = singleRotateWithRight(father);elsefather = doubleRotateWithRight(father);}route.push(father);}else if (father->element > son->element) {//儿子在左边father->left = son;if (height(father->left) - height(father->right) == 2) {if (X < retrieve(father->left)) {father = singleRotateWithLeft(father);}elsefather = doubleRotateWithLeft(father);}route.push(father);}father->height = Max(height(father->left), height(father->right)) + 1;}}
0 0
- 向AVL树进行插入的非递归函数
- AVL树进行插入的非递归函数
- C语言 AVL树的 非递归 方法插入
- AVL树的基本操作之插入(递归与非递归编码)(2)
- AVL树插入例程非递归实现C语言
- AVL树非递归实现插入和删除例程
- AVL平衡树插入非递归实现 C语言
- AVL树C++实现以及(递归函数)非递归改进
- AVL树的插入与删除(均为递归实现)
- AVL(平衡树)C++代码(非递归)
- 二叉树中插入节点的递归与非递归
- 算法-AVL树的插入
- AVL树的插入操作
- AVL树的插入算法
- Treap树非递归插入
- 转载 AVL(平衡树)C++代码(非递归)
- 【AVL树】AVL树的插入操作以及旋转
- 二叉树的非递归插入和遍历
- mongoDB add user in v3.0 问题的解决(Property 'addUser' of object admin is not a func)
- matlab实战中一些重要的函数总结
- 说说 Objective-C 里的 @()字面量语法
- Graph_tool的简单安装 ubuntu15.10
- 花生米三连发(动规)
- AVL树进行插入的非递归函数
- RHEL-6忘记root密码的解决办法
- 根据 Mavlink 协议串口通信实现数据的实时更新并用折线图显示
- AngularJS vs. jQuery,看看谁更胜一筹
- 第九周实践项目3————人数不定的工资类
- 面试之1604
- C++ for循环跳过某一项求和
- od 逆向破解口诀
- c++栈类,以数组为存储结构。