算法导论 10.4.5 O(1)空间遍历二叉树
来源:互联网 发布:入骨相思知不知 编辑:程序博客网 时间:2024/05/19 03:20
#pragma once#include <new>#include <cstddef>#include <vector>#include <stack>//template<typename T>class binary_search_tree{public:using value_type = int;struct node{value_type value_;node* left_;node* right_;node* parent_;};void insert(std::initializer_list<value_type> il){for (auto&& v : il){this->insert(v);}}void insert(const value_type& v){node* ptr = head_;node* parent = nullptr;while (ptr != nullptr){parent = ptr;if (v < ptr->value_) ptr = ptr->left_;else ptr = ptr->right_;}auto new_node = get_new_node();if (head_ == nullptr) head_ = new_node;else if (v < parent->value_) parent->left_ = new_node;else parent->right_ = new_node;new (&new_node->value_) value_type(v);new_node->parent_ = parent;}std::vector<value_type> inoreder_walk() const{std::vector<value_type> res{};node* ptr = head_ ;bool counter = false;while (ptr){if (ptr->left_!= nullptr)ptr = ptr->left_;else{res.push_back(ptr->value_);while (!ptr->right_){bool isBreak = false;if (ptr->parent_ == nullptr) return std::move(res);while (ptr == ptr->parent_->right_){isBreak = true;ptr = ptr->parent_->parent_;if (ptr->parent_ == nullptr){if (counter) return std::move(res);counter = true;break;}}if (!isBreak)ptr = ptr->parent_;res.push_back(ptr->value_);}ptr = ptr->right_;}}return res;}void transplant(node* src, node* des){if (des == head_) head_ = src;if (src->parent_->left_ == des)src->parent_->left_ = des;else{src->parent_->right_ = des;}src->parent_ = des->parent_;}node* tree_mininum(node* n) const noexcept{node* ptr = n;node* res = nullptr;while (ptr != nullptr){res = ptr;ptr = ptr->left_;}return res;}void erase(node* nodeToDel){if (nodeToDel == nullptr) return ;if (nodeToDel->left_ == nullptr)transplant(nodeToDel->right_, nodeToDel);else if (nodeToDel->right_ == nullptr)transplant(nodeToDel->left_, nodeToDel);else{auto successor = tree_mininum(nodeToDel->right_);if (successor != nodeToDel->right_){transplant(successor->right_, successor);successor->right_ = nodeToDel->right_;successor->right_->parent_ = successor;}transplant(successor, nodeToDel);successor->left_ = nodeToDel->left_;successor->left_->parent_ = successor;}delete nodeToDel;}node* get_new_node() const{auto new_node = static_cast<node*>(::operator new(sizeof(node)));new_node->left_ = new_node->right_ = new_node->parent_ = nullptr;return new_node;}private:node* head_ = nullptr;};
0 0
- 算法导论 10.4.5 O(1)空间遍历二叉树
- 算法导论 习题10.4-5 二叉树的遍历(非递归,O(1)存储)
- 算法导论 10-4-5 O(n)时间 O(1)空间遍历树
- 算法导论 10.4-5 O(n)时间非递归遍历二叉树
- 算法导论 10.4-2 O(n)时间 递归遍历二叉树
- 算法导论 10.4-3 O(n) 二叉树 非递归遍历
- 【算法】二叉树的非递归遍历的简洁写法/迭代器实现/O(1)空间复杂度的Morris遍历
- 中序遍历二叉树+O(1)空间
- 二叉树遍历 Morris O(1)空间复杂度
- 《算法导论》第12章 二叉查找树 (1)遍历
- 【算法导论】二叉树的深度优先遍历
- 【算法导论】二叉树的广度优先遍历
- 【算法导论】二叉树的前中后序非递归遍历实现
- 算法导论—二叉树非递归遍历
- 《算法导论》二叉树遍历的递归&非递归实现
- Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
- Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
- Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
- iGrimace安装包下载地址
- JAVA List删除时需注意的地方
- iGrimace iOS应用闪退或图标消失的解决办法
- HDU 5386 Cover(模拟)
- MFC动态创建菜单
- 算法导论 10.4.5 O(1)空间遍历二叉树
- 利用HTTP-only Cookie缓解XSS
- Oracle_object_id和data_object_id的区别与联系
- POJ - 3160 Father Christmas flymouse(最长路+强连通分量)
- Swift-常量(let)和变量(var)
- hdu 5389 Zero Escape (dp)
- HDU 5387 Clock
- tableView - 自定义等高cell-xib方式
- 八种排序算法(C语言简易版)