|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "./binary_search_tree_node.hpp" |
| 4 | +#include "./binary_search_tree_interface.h" |
| 5 | + |
| 6 | +#include <iostream> |
| 7 | +#include <queue> |
| 8 | + |
| 9 | +namespace mtl |
| 10 | +{ |
| 11 | + template <typename anytype> |
| 12 | + class BinarySearchTree : public BinarySearchTreeInterface<BinarySearchTreeNode<anytype>> |
| 13 | + { |
| 14 | + public: |
| 15 | + //构造函数 |
| 16 | + BinarySearchTree() : m_root(nullptr) {} |
| 17 | + |
| 18 | + //析构函数 |
| 19 | + ~BinarySearchTree() |
| 20 | + { |
| 21 | + erase(); |
| 22 | + } |
| 23 | + |
| 24 | + //创建搜索二叉树 |
| 25 | + void create() |
| 26 | + { |
| 27 | + create(&m_root); |
| 28 | + } |
| 29 | + |
| 30 | + //判空 |
| 31 | + bool empty() const override |
| 32 | + { |
| 33 | + return 0 == m_size; |
| 34 | + } |
| 35 | + |
| 36 | + //获得结点树 |
| 37 | + int size() const override |
| 38 | + { |
| 39 | + return m_size; |
| 40 | + } |
| 41 | + |
| 42 | + //前序遍历 |
| 43 | + void preOrder(void(*func)(BinarySearchTreeNode<anytype> *)) |
| 44 | + { |
| 45 | + visit = func; |
| 46 | + preOrder(m_root); |
| 47 | + std::cout << std::endl; |
| 48 | + } |
| 49 | + |
| 50 | + //中序遍历 |
| 51 | + void inOrder(void(*func)(BinarySearchTreeNode<anytype> *)) |
| 52 | + { |
| 53 | + visit = func; |
| 54 | + inOrder(m_root); |
| 55 | + std::cout << std::endl; |
| 56 | + } |
| 57 | + |
| 58 | + //后序遍历 |
| 59 | + void postOrder(void(*func)(BinarySearchTreeNode<anytype> *)) |
| 60 | + { |
| 61 | + visit = func; |
| 62 | + postOrder(m_root); |
| 63 | + std::cout << std::endl; |
| 64 | + } |
| 65 | + |
| 66 | + //按层遍历 |
| 67 | + void levelOrder(void(*func)(BinarySearchTreeNode<anytype> *)) |
| 68 | + { |
| 69 | + visit = func; |
| 70 | + levelOrder(m_root); |
| 71 | + std::cout << std::endl; |
| 72 | + } |
| 73 | + |
| 74 | + //获得高度 |
| 75 | + int height() const |
| 76 | + { |
| 77 | + return height(m_root); |
| 78 | + } |
| 79 | + |
| 80 | + //获得根结点 |
| 81 | + BinarySearchTreeNode<anytype>* getRoot() |
| 82 | + { |
| 83 | + return m_root; |
| 84 | + } |
| 85 | + |
| 86 | + //删除搜索树 |
| 87 | + void erase() |
| 88 | + { |
| 89 | + postOrder(dispose); |
| 90 | + m_root = nullptr; |
| 91 | + m_size = 0; |
| 92 | + } |
| 93 | + |
| 94 | + private: |
| 95 | + BinarySearchTreeNode<anytype> *m_root; |
| 96 | + static int m_size; |
| 97 | + |
| 98 | + static void (*visit)(BinarySearchTreeNode<anytype> *); |
| 99 | + static void preOrder(BinarySearchTreeNode<anytype> *); |
| 100 | + static void inOrder(BinarySearchTreeNode<anytype> *); |
| 101 | + static void postOrder(BinarySearchTreeNode<anytype> *); |
| 102 | + static void levelOrder(BinarySearchTreeNode<anytype> *); |
| 103 | + static int height(BinarySearchTreeNode<anytype> *); |
| 104 | + static void dispose(BinarySearchTreeNode<anytype> *); |
| 105 | + static void output(BinarySearchTreeNode<anytype> *); |
| 106 | + static void create(BinarySearchTreeNode<anytype> **); |
| 107 | + }; |
| 108 | + |
| 109 | + //静态函数指针初始化 |
| 110 | + template <typename anytype> |
| 111 | + void (*BinarySearchTree<anytype>::visit)(BinarySearchTreeNode<anytype> *) = nullptr; |
| 112 | + |
| 113 | + //静态变量size初始化 |
| 114 | + template <typename anytype> |
| 115 | + int BinarySearchTree<anytype>::m_size = 0; |
| 116 | + |
| 117 | + //私有前序遍历 |
| 118 | + template <typename anytype> |
| 119 | + void BinarySearchTree<anytype>::preOrder(BinarySearchTreeNode<anytype> *root) |
| 120 | + { |
| 121 | + if (nullptr != root) |
| 122 | + { |
| 123 | + BinarySearchTree<anytype>::visit(root); |
| 124 | + preOrder(root->m_left_child); |
| 125 | + preOrder(root->m_right_child); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + //私有中序遍历 |
| 130 | + template <typename anytype> |
| 131 | + void BinarySearchTree<anytype>::inOrder(BinarySearchTreeNode<anytype> *root) |
| 132 | + { |
| 133 | + if (nullptr != root) |
| 134 | + { |
| 135 | + inOrder(root->m_left_child); |
| 136 | + BinarySearchTree<anytype>::visit(root); |
| 137 | + inOrder(root->m_right_child); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + //私有后序遍历 |
| 142 | + template <typename anytype> |
| 143 | + void BinarySearchTree<anytype>::postOrder(BinarySearchTreeNode<anytype> *root) |
| 144 | + { |
| 145 | + if (nullptr != root) |
| 146 | + { |
| 147 | + postOrder(root->m_left_child); |
| 148 | + postOrder(root->m_right_child); |
| 149 | + BinarySearchTree<anytype>::visit(root); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + //私有按层遍历 |
| 154 | + template <typename anytype> |
| 155 | + void BinarySearchTree<anytype>::levelOrder(BinarySearchTreeNode<anytype> *root) |
| 156 | + { |
| 157 | + std::queue<BinarySearchTreeNode<anytype> *> q; |
| 158 | + while (nullptr != root) |
| 159 | + { |
| 160 | + BinarySearchTree<anytype>::visit(root); |
| 161 | + |
| 162 | + if (nullptr != root->m_left_child); |
| 163 | + { |
| 164 | + q.push(root->m_left_child); |
| 165 | + } |
| 166 | + |
| 167 | + if (nullptr != root->m_right_child) |
| 168 | + { |
| 169 | + q.push(root->m_right_child); |
| 170 | + } |
| 171 | + |
| 172 | + if (q.empty()) |
| 173 | + { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + root = q.front(); |
| 178 | + q.pop(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + //获得高度 |
| 183 | + template <typename anytype> |
| 184 | + int BinarySearchTree<anytype>::height(BinarySearchTreeNode<anytype> *root) |
| 185 | + { |
| 186 | + if (nullptr == root) |
| 187 | + { |
| 188 | + return 0; |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + int height_left = height(root->m_left_child); |
| 193 | + int height_right = height(root->m_right_child); |
| 194 | + return height_left > height_right ? ++height_left : ++height_right; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + //删除结点 |
| 199 | + template <typename anytype> |
| 200 | + void BinarySearchTree<anytype>::dispose(BinarySearchTreeNode<anytype> *root) |
| 201 | + { |
| 202 | + delete root; |
| 203 | + } |
| 204 | + |
| 205 | + //创建树 |
| 206 | + template <typename anytype> |
| 207 | + void BinarySearchTree<anytype>::create(BinarySearchTreeNode<anytype> **root) |
| 208 | + { |
| 209 | + anytype data; |
| 210 | + std::cin.clear(); |
| 211 | + std::cin >> data; |
| 212 | + |
| 213 | + if ('#' != data) |
| 214 | + { |
| 215 | + ++m_size; |
| 216 | + *root = new BinarySearchTreeNode<anytype>(data); |
| 217 | + |
| 218 | + if (nullptr != *root) |
| 219 | + { |
| 220 | + create(&(*root)->m_left_child); |
| 221 | + create(&(*root)->m_right_child); |
| 222 | + } |
| 223 | + else |
| 224 | + { |
| 225 | + std::cerr << __FILE__ << ' ' << __LINE__ << "#" << __FUNCTION__ << " 内存申请失败\n"; |
| 226 | + } |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + *root = nullptr; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | +} |
0 commit comments