From d1e80a87075a9310917886c85bbd9c419c5e1b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 27 Oct 2014 15:47:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0avl-tree?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/AVLTree.h | 40 +++++++++++++++++++++++++++++++++ TinySTL/TinySTL.vcxproj | 1 + TinySTL/TinySTL.vcxproj.filters | 3 +++ 3 files changed, 44 insertions(+) create mode 100644 TinySTL/AVLTree.h diff --git a/TinySTL/AVLTree.h b/TinySTL/AVLTree.h new file mode 100644 index 0000000..3912e6e --- /dev/null +++ b/TinySTL/AVLTree.h @@ -0,0 +1,40 @@ +#ifndef _AVL_TREE_H_ +#define _AVL_TREE_H_ + +#include "Allocator.h" + +namespace TinySTL{ + //class of avl tree + template + class avl_tree{ + public: + typename T value_type; + private: + struct node{ + T data_; + node *left_, *right_; + size_t height_; + explicit node(T d = T(), node *l = 0, node *r = 0, size_t h = 0) + :data_(d), left_(l), right_(r), height_(h){} + }; + typedef TinySTL::allocator dataAllocator; + private: + node *root_; + public: + avl_tree() :root_(0){} + avl_tree(const avl_tree&) = delete; + avl_tree& operator = (const avl_tree&) = delete; + ~avl_tree(){ destroyAndDeallocateAllNodes(root_); } + private: + void destroyAndDeallocateAllNodes(node *p){ + if (p != 0){ + destroyAndDeallocateAllNodes(p->left_); + destroyAndDeallocateAllNodes(p->right_); + dataAllocator::destroy(p); + dataAllocator::deallocate(p); + } + } + }; +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index d42101c..fd62738 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -86,6 +86,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index ce861fc..57f3834 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -83,5 +83,8 @@ 头文件 + + 头文件 + \ No newline at end of file