1 #pragma once 2 #include "stdio.h" 3 4 struct Node 5 { 6 Node(int aValue) 7 :m_Value(aValue) 8 ,m_pLeft(NULL) 9 ,m_pRight(NULL) 10 ,m_pParent(NULL) 11 { 12 13 } 14 int m_Value; 15 Node* m_pLeft; 16 Node* m_pRight; 17 Node* m_pParent; 18 }; 19 20 class BinarySearchTree 21 { 22 public: 23 BinarySearchTree(void); 24 ~BinarySearchTree(void); 25 void Init(); 26 void Insert(int aValue); 27 void Delete(int aValue); 28 Node* MaxNode(Node* apNode); 29 Node* MinNode(Node* apNode); 30 Node* Search(int aValue); 31 32 void PreOrderPrint(); 33 void AfterOrderPrint(); 34 void MidOrderPrint(); 35 36 void PrintNode( Node* lpNode ) 37 { 38 printf("%d ", lpNode->m_Value); 39 } 40 41 Node* Successor(Node* aNode); 42 43 private: 44 Node* m_pRootNode; 45 };
1 #include "BinarySearchTree.h" 2 #include <stack> 3 #include <queue> 4 5 BinarySearchTree::BinarySearchTree(void) 6 :m_pRootNode(NULL) 7 { 8 } 9 10 BinarySearchTree::~BinarySearchTree(void) 11 { 12 } 13 14 void BinarySearchTree::Insert( int aValue ) 15 { 16 Node* lpNewNode = new Node(aValue); 17 if (NULL == m_pRootNode) 18 { 19 m_pRootNode = lpNewNode; 20 } 21 else 22 { 23 Node* lpNode = m_pRootNode; 24 Node* lpPNode = NULL; 25 26 while(lpNode) 27 { 28 lpPNode = lpNode; 29 if (lpNode->m_Value > aValue) 30 { 31 lpNode = lpNode->m_pLeft; 32 } 33 else 34 { 35 lpNode = lpNode->m_pRight; 36 } 37 } 38 39 if (lpPNode->m_Value > aValue) 40 { 41 lpPNode->m_pLeft = lpNewNode; 42 } 43 else 44 { 45 lpPNode->m_pRight = lpNewNode; 46 } 47 lpNewNode->m_pParent = lpPNode; 48 } 49 } 50 51 void BinarySearchTree::Init() 52 { 53 } 54 55 void BinarySearchTree::Delete( int aValue ) 56 { 57 Node* lpNode = Search(aValue); 58 if (NULL == lpNode) 59 { 60 return; 61 } 62 63 Node* lpDeleteNode = NULL; 64 65 if (!lpNode->m_pLeft && !lpNode->m_pRight) 66 { 67 if (lpNode->m_pParent->m_pLeft = lpNode) 68 { 69 lpNode->m_pParent->m_pLeft = NULL; 70 } 71 else 72 { 73 lpNode->m_pParent->m_pRight = NULL; 74 } 75 delete lpNode; 76 } 77 else 78 { 79 if (!lpNode->m_pLeft && lpNode->m_pRight) 80 { 81 if (lpNode->m_pParent->m_pLeft == lpNode) 82 { 83 lpNode->m_pParent->m_pLeft = lpNode->m_pRight; 84 lpNode->m_pRight->m_pParent = lpNode->m_pParent; 85 } 86 else 87 { 88 lpNode->m_pParent->m_pRight = lpNode->m_pRight; 89 lpNode->m_pRight->m_pParent = lpNode->m_pParent; 90 } 91 delete lpNode; 92 lpNode =NULL; 93 } 94 else if (lpNode->m_pLeft && !lpNode->m_pRight) 95 { 96 if (lpNode->m_pParent->m_pLeft == lpNode) 97 { 98 lpNode->m_pParent->m_pLeft = lpNode->m_pLeft; 99 lpNode->m_pLeft->m_pParent = lpNode->m_pParent; 100 } 101 else 102 { 103 lpNode->m_pParent->m_pRight = lpNode->m_pLeft; 104 lpNode->m_pLeft->m_pParent = lpNode->m_pParent; 105 } 106 delete lpNode; 107 lpNode = NULL; 108 } 109 else 110 { 111 Node* lpSuccessorNode = Successor(lpNode); 112 lpNode->m_Value = lpSuccessorNode->m_Value; 113 if (lpSuccessorNode->m_pRight) 114 { 115 lpSuccessorNode->m_pParent->m_pLeft = lpSuccessorNode->m_pRight; 116 lpSuccessorNode->m_pRight->m_pParent = lpSuccessorNode->m_pParent; 117 } 118 delete lpSuccessorNode; 119 lpSuccessorNode = NULL; 120 } 121 } 122 } 123 124 void BinarySearchTree::PreOrderPrint() 125 { 126 std::queue<Node*> lStack; 127 Node* lpCurNode = m_pRootNode; 128 129 if (NULL != lpCurNode) 130 { 131 lStack.push(lpCurNode); 132 while(!lStack.empty()) 133 { 134 Node* lpNode = lStack.front(); 135 lStack.pop(); 136 PrintNode(lpNode); 137 if (lpNode->m_pLeft) 138 { 139 lStack.push(lpNode->m_pLeft); 140 } 141 if (lpNode->m_pRight) 142 { 143 lStack.push(lpNode->m_pRight); 144 } 145 } 146 } 147 } 148 149 void BinarySearchTree::AfterOrderPrint() 150 { 151 std::stack<Node*> lStack; 152 Node* lpCurNode = m_pRootNode; 153 bool lDone = true; 154 while(!lStack.empty() || lpCurNode) 155 { 156 if (lpCurNode) 157 { 158 lStack.push(lpCurNode); 159 lpCurNode = lpCurNode->m_pRight; 160 } 161 else 162 { 163 lpCurNode = lStack.top(); 164 lStack.pop(); 165 PrintNode(lpCurNode); 166 lpCurNode = lpCurNode->m_pLeft; 167 } 168 } 169 } 170 171 void BinarySearchTree::MidOrderPrint() 172 { 173 Node* lpNode = m_pRootNode; 174 std::stack<Node*> lQueue; 175 176 while(NULL != lpNode || !lQueue.empty()) 177 { 178 if (NULL != lpNode) 179 { 180 lQueue.push(lpNode); 181 lpNode = lpNode->m_pLeft; 182 } 183 else 184 { 185 lpNode = lQueue.top(); 186 lQueue.pop(); 187 PrintNode(lpNode); 188 lpNode = lpNode->m_pRight; 189 } 190 } 191 } 192 193 Node* BinarySearchTree::MinNode(Node* apNode) 194 { 195 Node* lpPreNode = NULL; 196 Node* lpNode = apNode; 197 198 while(lpNode) 199 { 200 lpPreNode = lpPreNode; 201 lpNode = lpNode->m_pLeft; 202 } 203 204 return lpPreNode; 205 } 206 207 Node* BinarySearchTree::MaxNode(Node* apNode) 208 { 209 Node* lpPreNode = NULL; 210 Node* lpNode = apNode; 211 212 while(lpNode) 213 { 214 lpPreNode = lpPreNode; 215 lpNode = lpNode->m_pRight; 216 } 217 218 return lpPreNode; 219 } 220 221 Node* BinarySearchTree::Successor(Node* aNode) 222 { 223 if (NULL == m_pRootNode) 224 { 225 return NULL; 226 } 227 228 Node* lpNode = aNode; 229 if (lpNode->m_pRight) 230 { 231 return MinNode(lpNode->m_pRight); 232 } 233 else 234 { 235 while(lpNode && lpNode->m_pParent->m_pRight == lpNode) 236 { 237 lpNode = lpNode->m_pParent; 238 } 239 240 return lpNode; 241 } 242 } 243 244 Node* BinarySearchTree::Search( int aValue ) 245 { 246 Node* lpNode = m_pRootNode; 247 while(lpNode) 248 { 249 if (lpNode->m_Value > aValue) 250 { 251 lpNode = lpNode->m_pLeft; 252 } 253 else if (lpNode->m_Value < aValue) 254 { 255 lpNode = lpNode->m_pRight; 256 } 257 else 258 { 259 return lpNode; 260 } 261 } 262 return NULL; 263 }