1 #pragma once 2 #include<Windows.h> 3 #include<stdio.h> 4 5 #define SUCCESS 1 // 执行成功 6 #define ERROR -1 // 执行失败 7 8 template<class T> 9 class TreeNode { 10 public: 11 T element; //当前节点存储的数据 12 TreeNode<T>* pLeft; //指向左子节点的指针 13 TreeNode<T>* pRight; //指向右子节点的指针 14 TreeNode<T>* pParent; //指向父结点的指针 15 16 17 TreeNode(T& ele) { 18 //初始化Node节点 19 memset(&element, 0, sizeof(TreeNode)); 20 //为元素赋值 21 memcpy(&element, &ele, sizeof(T)); 22 pLeft = pRight = pParent = NULL; 23 } 24 //重载== 比较两结点是否相等 25 bool operator==(TreeNode<T>* node) { 26 return node->element == element ? true : false; 27 } 28 }; 29 30 template<class T> 31 class BSortTree { 32 public: 33 BSortTree(); //构造函数 34 ~BSortTree(); //析构函数 35 public: //判断树是否为空 36 bool IsEmpty(); //新增节点 37 DWORD Insert(T element); //删除节点 38 void Delete(T element); 39 TreeNode<T>* Search(T element); 40 TreeNode<T>* GetRoot(); //查找节点 41 void InOrderTraverse(TreeNode<T>* pNode); //中序遍历 42 void PreOrderTraverse(TreeNode<T>* pNode); //前序遍历 43 void PostOrderTraverse(TreeNode<T>* pNode); //后序遍历 44 private: 45 TreeNode<T>* GetMaxNode(TreeNode<T>* pNode); //获取以pNode为根的最大节点 46 TreeNode<T>* GetMinNode(TreeNode<T>* pNode); //获取以pNode为根的最小节点 47 TreeNode<T>* SearchNode(TreeNode<T>* pNode, T element); //获取以pNode为根的最小节点 48 DWORD InsertNode(T element, TreeNode<T>* pNode); //新增节点 49 DWORD DeleteNode(T element, TreeNode<T>* pNode); //删除节点 50 void Clear(TreeNode<T>* pNode); //清空所有节点 51 private: 52 TreeNode<T>* m_pRoot; //根结点指针 53 int size; //树中元素总个数 54 }; 55 56 template<class T> 57 void BSortTree<T>::InOrderTraverse(TreeNode<T>* pNode) 58 { 59 60 //中序遍历所有怪物,列出怪的名字 61 if (pNode == NULL) 62 { 63 return; 64 } 65 66 67 InOrderTraverse(pNode->pLeft); 68 printf("%d ", pNode->element); 69 InOrderTraverse(pNode->pRight); 70 71 return; 72 73 } 74 75 template<class T> 76 TreeNode<T>* BSortTree<T>::GetRoot() 77 { 78 return m_pRoot; 79 } 80 81 template<class T> 82 BSortTree<T>::BSortTree() 83 { 84 m_pRoot = NULL; 85 size = 0; 86 } 87 template<class T> 88 BSortTree<T>::~BSortTree() { 89 90 Clear(m_pRoot); 91 } 92 template<class T> 93 DWORD BSortTree<T>::Insert(T element) 94 { 95 //如果根节点为空 96 if (!m_pRoot) 97 { 98 m_pRoot = new TreeNode<T>(element); 99 size++; 100 return SUCCESS; 101 } 102 //如果根节点不为空 103 return InsertNode(element, m_pRoot); 104 } 105 template<class T> 106 DWORD BSortTree<T>::InsertNode(T element, TreeNode<T>* pNode) 107 { 108 //将元素封装到节点中 109 TreeNode<T>* pNewNode = new TreeNode<T>(element); 110 //如果element == 当前节点 直接返回 111 if (element == pNode->element) 112 { 113 return SUCCESS; 114 } 115 //如果pNode的左子节点为NULL 并且element < 当前节点 116 if (pNode->pLeft == NULL && element < pNode->element) 117 { 118 pNode->pLeft = pNewNode; 119 pNewNode->pParent = pNode; 120 size++; 121 return SUCCESS; 122 } 123 //如果pNode的右子节点为NULL 并且element > 当前节点 124 if (pNode->pRight == NULL && element > pNode->element) { 125 pNode->pRight = pNewNode; 126 pNewNode->pParent = pNode; 127 size++; 128 return SUCCESS; 129 } 130 //如果element<当前节点 且当前节点的左子树不为空 131 if (element < pNode->element) 132 { 133 InsertNode(element, pNode->pLeft); 134 } 135 else 136 { 137 InsertNode(element, pNode->pRight); 138 } 139 return SUCCESS; 140 } 141 142 template<class T> 143 void BSortTree<T>::Clear(TreeNode<T>* pNode) 144 { 145 if (pNode != NULL) 146 { 147 Clear(pNode->pLeft); 148 Clear(pNode->pRight); 149 delete pNode; 150 pNode = NULL; 151 } 152 } 153 154 template<class T> 155 bool BSortTree<T>::IsEmpty() 156 { 157 return size == 0 ? true : false; 158 } 159 160 template<class T> 161 TreeNode<T>* BSortTree<T>::Search(T element) 162 { 163 return SearchNode(m_pRoot, element); 164 } 165 template<class T> 166 TreeNode<T>* BSortTree<T>::SearchNode(TreeNode<T>* pNode, T element) 167 { 168 if (pNode == NULL) //如果节点为NULL 169 { 170 return NULL; 171 } 172 else if (element == pNode->element) //如果相等 173 { 174 return pNode; 175 } //如果比节点的元素小 向左找 176 else if (element < pNode->element) 177 { 178 return SearchNode(pNode->pLeft, element); 179 } 180 else //如果比节点的元素大 向右找 181 { 182 return SearchNode(pNode->pRight, element); 183 } 184 } 185 186 template<class T> 187 void BSortTree<T>::Delete(T element) 188 { 189 DeleteNode(element, this->m_pRoot); 190 } 191 192 template<class T> 193 DWORD BSortTree<T>::DeleteNode(T element, TreeNode<T>* pNode) 194 { 195 //删除节点 196 197 if (element == pNode->element) 198 { 199 printf("找到了 "); 200 if (pNode->pLeft == NULL && pNode->pRight==NULL) 201 { 202 if (pNode->element < pNode->pParent->element) 203 { 204 pNode->pParent->pLeft = NULL; 205 } 206 else 207 { 208 pNode->pParent->pRight = NULL; 209 } 210 delete pNode; 211 pNode = NULL; 212 return SUCCESS; 213 } 214 if (pNode->pLeft==NULL && pNode->element < pNode->pParent->element) 215 { 216 pNode->pParent->pLeft = pNode->pRight; 217 delete pNode; 218 pNode = NULL; 219 return SUCCESS; 220 } 221 if (pNode->pLeft == NULL && pNode->element > pNode->pParent->element) 222 { 223 pNode->pParent->pRight = pNode->pRight; 224 delete pNode; 225 pNode = NULL; 226 return SUCCESS; 227 } 228 if (pNode->pRight == NULL && pNode->element < pNode->pParent->element) 229 { 230 pNode->pParent->pLeft = pNode->pLeft; 231 delete pNode; 232 pNode = NULL; 233 return SUCCESS; 234 } 235 if (pNode->pRight == NULL && pNode->element > pNode->pParent->element) 236 { 237 pNode->pParent->pRight = pNode->pLeft; 238 delete pNode; 239 pNode = NULL; 240 return SUCCESS; 241 } 242 if (pNode->pParent == NULL) 243 { 244 TreeNode<T>* Temp = pNode->pRight; 245 while (Temp->pLeft != NULL) 246 { 247 Temp = Temp->pLeft; 248 } 249 Temp->pLeft = pNode->pLeft->pRight; 250 pNode->pLeft->pRight = pNode->pRight; 251 pNode->pLeft->pParent = pNode->pParent; 252 this->m_pRoot = pNode->pLeft; 253 delete pNode; 254 pNode = NULL; 255 Temp = NULL; 256 return SUCCESS; 257 return SUCCESS; 258 } 259 else 260 { 261 TreeNode<T>* Temp = pNode->pRight; 262 pNode->pParent->pLeft = pNode->pLeft; 263 while (Temp->pLeft != NULL) 264 { 265 Temp = Temp->pLeft; 266 } 267 Temp->pLeft = pNode->pLeft->pRight; 268 pNode->pLeft->pRight = pNode->pRight; 269 pNode->pLeft->pParent = pNode->pParent; 270 delete pNode; 271 pNode = NULL; 272 Temp = NULL; 273 return SUCCESS; 274 } 275 return SUCCESS; 276 } 277 278 279 if (element < pNode->element) 280 { 281 DeleteNode(element, pNode->pLeft); 282 } 283 else 284 { 285 DeleteNode(element, pNode->pRight); 286 } 287 288 return SUCCESS; 289 }