直接贴代码
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 5 using namespace std; 6 7 template<class item> 8 class Node { 9 public: 10 item key; 11 Node* left; 12 Node* right; 13 Node*p; 14 Node(const Node& u):key(u.key),left(u.left),right(u.right),p(u.p){ 15 // cout << "拷贝构造" << endl; 16 } 17 Node(item key):key(key),left(0),right(0),p(0){ 18 // cout << "默认构造?" << key << endl; 19 } 20 }; 21 22 template<class T> 23 class binaryTree { 24 private: 25 Node<T>* root; 26 void destroy(Node<T>* u); 27 void _inorder(Node<T>* u); 28 void _print(ostream& os, Node<T>* u) { 29 if (u == 0)return; 30 if (u->p == 0)os << u->key << "是树根"; 31 else if (u->p->left == u)os << u->key << "是" << u->p->key << "的左孩子"; 32 else os << u->key << "是" << u->p->key << "的右孩子"; 33 os << endl; 34 _print(os, u->left); 35 _print(os, u->right); 36 } 37 public: 38 //测试函数:打印 39 void print() { 40 _print(cout, root); 41 } 42 //测试函数:取根 43 Node<T>* _getRoot() { return root; } 44 45 ~binaryTree() { //析构函数不需释放内存,因为测试main里面用的并不是用new申请的动态内存 46 //destroy(root); 47 } 48 binaryTree(Node<T>* root=0):root(root){} 49 50 void inorder() { _inorder(root); } 51 void insert(Node<T>* u); 52 void remove(Node<T>* u); 53 void transPlant(Node<T>*u, Node<T>* v); 54 Node<T>* search(T k); 55 Node<T>* maximum(Node<T>* u); 56 Node<T>* minimum(Node<T>* u); 57 Node<T>* predecessor(Node<T>* u); 58 Node<T>* successor(Node<T>*); 59 }; 60 template<class T> 61 void binaryTree<T>::destroy(Node<T>* u) { 62 if (u != 0) { 63 destroy(u->left); 64 destroy(u->right); 65 delete u; 66 } 67 } 68 69 template<class T> 70 void binaryTree<T>::_inorder(Node<T>* u) { 71 if (u) { 72 _inorder(u->left); 73 cout << u->key << endl; 74 _inorder(u->right); 75 } 76 } 77 78 template<class T> 79 Node<T>* binaryTree<T>::search(T k) { 80 Node<T>* u = root; 81 while (u) { 82 if (k == u->key)return u; 83 else if (k < u->key)u = u->left; 84 else u = u->right; 85 } 86 return u;//NULL 87 } 88 89 template<class T> 90 Node<T>* binaryTree<T>::maximum(Node<T>* u) { 91 while (u->right) 92 u = u->right; 93 return u; 94 } 95 96 template<class T> 97 Node<T>* binaryTree<T>::minimum(Node<T>* u) { 98 while (u->left) 99 u = u->left; 100 return u; 101 } 102 //前继节点 103 template<class T> 104 Node<T>* binaryTree<T>::predecessor(Node<T>* u) { 105 if (u->left)return maximum(u->left); 106 107 Node<T>* y = u->p; 108 while (y&&u == y->left) { 109 u = y; 110 y = y->p; 111 } 112 return y; 113 } 114 //后继节点 115 template <class T> 116 Node<T>* binaryTree<T>::successor(Node<T>* u) { 117 if (u->right)return minimum(); 118 119 Node<T>* y = u->p; 120 while (y&&u == y->right) { 121 u = y; 122 y = y->p; 123 } 124 return y; 125 } 126 127 template<class T> 128 void binaryTree<T>::insert(Node<T>* u) { 129 Node<T>* pre = 0; 130 Node<T>* cur = root; 131 while (cur) { 132 pre = cur; 133 if (u->key < cur->key)cur = cur->left; 134 else cur = cur->right; 135 } 136 u->p = pre; 137 138 if (pre == 0)root = u; 139 else if (u->key < pre->key)pre->left = u; 140 else pre->right = u; 141 } 142 143 template<class T> 144 void binaryTree<T>::transPlant(Node<T>*u, Node<T>* v) { 145 if (u->p == 0)root = v; 146 else if (u->p->left == u)u->p->left = v; 147 else u->p->right = v; 148 if (v)v->p = u->p; 149 } 150 151 template<class T> 152 void binaryTree<T>::remove(Node<T>* u) { 153 if (u == 0)return; 154 155 if (u->left == 0)transPlant(u, u->right); 156 else if (u->right == 0)transPlant(u, u->left); 157 else { 158 Node<T>* y = minimum(u->right);//找u的后继节点y,y没有左孩子 159 if (y != u->right) {// 160 transPlant(y, y->right); 161 y->right = u->right; 162 y->right->p = y; 163 } 164 transPlant(u, y); 165 y->left = u->left; 166 y->left->p = y; 167 } 168 169 //delete u; 170 } 171 172 /*typedef struct node node; 173 struct node { 174 node(const node&) { cout << "拷贝" << endl; } 175 node(node&&) { cout << "移动" << endl; } 176 node() { cout << "默认" << endl; } 177 int xi; 178 };*/ 179 180 181 int main(void) { 182 binaryTree<int> xi; 183 int a[10] = { 6,2,1,5,7,8,3,4 }; 184 vector<Node<int> > hhh; 185 for (int i = 0;i < 8;i++) { 186 Node<int> tmp(a[i]); 187 hhh.push_back(tmp); 188 } 189 for (int i = 0;i < 8;i++)xi.insert(&hhh[i]); 190 xi.print(); 191 192 xi.remove(xi._getRoot()->left); 193 xi.print(); 194 return 0; 195 }