借鉴了书本和学长的代码
1 #include<iostream> 2 using namespace std; 3 template<class T> 4 struct BTNode // 结点类 5 { 6 BTNode() 7 { 8 lchild=rchild=NULL; 9 10 } 11 BTNode(const T &x) 12 { 13 element=x; 14 lchild=rchild=NULL; 15 } 16 BTNode(const T &x,BTNode<T> *l,BTNode<T> *r) 17 { 18 element=x; 19 lchild=l; 20 rchild=r; 21 22 } 23 T element; 24 BTNode<T>* lchild,*rchild; 25 }; 26 template<class T> 27 class BinaryTree 28 { 29 public: 30 BinaryTree() 31 { 32 root=NULL; 33 } 34 ~BinaryTree() 35 { 36 delete []root; 37 } 38 bool Root(T &x)const; 39 void MakeTree(const T &x,BinaryTree<T> & left,BinaryTree<T> &right); 40 41 void BreakTree(T &x,BinaryTree<T> & left,BinaryTree<T> &right); 42 // void Visit(T &x); 43 void PreOrder(void (*Visit)(T &x)); 44 void PreOrder(void (*Visit)(T &x),BTNode<T> *t); 45 void InOrder(void (*Visit)(T &x)); 46 void InOrder(void (*Visit)(T &x),BTNode<T> *t); 47 void PostOrder(void (*Visit)(T &x)); 48 void PostOrder(void (*Visit)(T &x),BTNode<T> *t); 49 50 int Size(); 51 int Size(BTNode<T>*t); 52 int Node_num(BTNode<T>*p) ; 53 BTNode<T>* Copy(BTNode <T>*t); 54 int High(BTNode<T>*p); 55 void Exchange(BTNode<T>*&t); 56 57 BTNode<T> *root; 58 }; 59 template<class T> 60 bool BinaryTree<T>::Root(T &x)const 61 { 62 if(root) 63 { 64 x=root->element; 65 return true; 66 } 67 else 68 return false; 69 } 70 template<class T> 71 void BinaryTree<T>::MakeTree(const T &x,BinaryTree<T> & left,BinaryTree<T> &right) 72 { 73 if(root||&left==&right ) 74 return ; 75 root=new BTNode<T>(x,left.root,right.root); 76 left.root=right.root=NULL; 77 } 78 79 template <class T> 80 void BinaryTree<T>::BreakTree(T &x,BinaryTree<T> & left,BinaryTree<T> &right) 81 { 82 if(!root||&left.root==&right.root||left.root||right.root) 83 return; 84 x=root->element; 85 left.root=root->lchild; 86 right.root=root->rchild; 87 delete root; 88 root=NULL; 89 } 90 91 92 template <class T> 93 void Visit(T &x) 94 { 95 cout<<x<<" "; 96 } 97 //先序遍历 98 template<class T> 99 void BinaryTree<T>::PreOrder(void (*Visit)(T &x),BTNode<T> *t) 100 { 101 if(t) 102 { 103 Visit(t->element); 104 PreOrder(Visit,t->lchild); 105 PreOrder(Visit,t->rchild); 106 } 107 } 108 template <class T> 109 void BinaryTree<T>::PreOrder(void (*Visit)(T &x)) 110 { 111 PreOrder(Visit,root); 112 113 } 114 115 //中序遍历 116 template <class T> 117 void BinaryTree<T>::InOrder(void (*Visit)(T &x)) 118 { 119 InOrder(Visit,root); 120 121 } 122 template<class T> 123 void BinaryTree<T>::InOrder(void (*Visit)(T &x),BTNode<T> *t) 124 { 125 if(t) 126 { 127 InOrder(Visit,t->lchild); 128 Visit(t->element); 129 InOrder(Visit,t->rchild); 130 } 131 } 132 //后序遍历 133 template <class T> 134 void BinaryTree<T>::PostOrder(void (*Visit)(T &x)) 135 { 136 PostOrder(Visit,root); 137 138 } 139 template<class T> 140 void BinaryTree<T>::PostOrder(void (*Visit)(T &x),BTNode<T> *t) 141 { 142 if(t) 143 { 144 PostOrder(Visit,t->lchild); 145 PostOrder(Visit,t->rchild); 146 Visit(t->element); 147 } 148 } 149 150 template<class T> //求二叉树的结点数 151 int BinaryTree<T>::Size() 152 { 153 return Size(root); 154 } 155 template<class T> 156 int BinaryTree<T>::Size(BTNode<T>*t) 157 { 158 if(!t) 159 return 0; 160 else return Size(t->lchild)+Size(t->rchild)+1; 161 } 162 163 164 template<class T> 165 int BinaryTree<T>::Node_num(BTNode<T>*p) //叶子结点 166 { 167 if(p) 168 { 169 if(p -> lchild == NULL && p -> rchild == NULL) 170 return 1; 171 else 172 return Node_num(p -> lchild) + Node_num(p -> rchild); 173 } 174 else 175 return 0; 176 } 177 178 template<class T> 179 BTNode<T>* BinaryTree<T>::Copy(BTNode <T>*t) // 复制一棵二叉树 180 { 181 if(!t) 182 return NULL; 183 184 BTNode<T>* q=new BTNode<T>(t->element); 185 q->lchild=Copy(t->lchild); 186 q->rchild=Copy(t->rchild); 187 return q; 188 189 } 190 template<class T> 191 int BinaryTree<T>::High(BTNode<T>*p) //高度 192 { 193 if(p == NULL) 194 return 0; 195 else if(p -> lchild == NULL && p -> rchild == NULL) 196 return 1; 197 else 198 return (High(p -> lchild) > High(p -> rchild) ? High(p -> lchild) + 1 : High(p -> rchild) + 1); 199 } 200 201 template<class T> 202 void BinaryTree<T>::Exchange(BTNode<T>* &t) //左右子树交换 203 { 204 if(t) 205 { 206 BTNode<T>*q = t -> lchild; 207 t -> lchild = t->rchild; 208 t -> rchild = q; 209 Exchange(t -> lchild); 210 Exchange(t -> rchild); 211 } 212 } 213 214 int main () 215 { 216 BinaryTree<char> a,b,x,y,z,temp; 217 char e; 218 219 y.MakeTree(‘E‘,a,b); 220 z.MakeTree(‘F‘,a,b); 221 x.MakeTree(‘C‘,y,z); 222 y.MakeTree(‘D‘,a,b); 223 z.MakeTree(‘B‘,y,x); 224 cout<<"z的先序遍历:"; 225 z.PreOrder(Visit); 226 227 cout<<endl<<"z的中序遍历:"; 228 z.InOrder(Visit); 229 cout<<endl<<"z的后序遍历:"; 230 z.PostOrder(Visit); 231 cout<<endl<<"结点数:"<<z.Size(); 232 cout<<endl<<"z的高度:"<<z.High(z.root); 233 cout<<endl<<"z的叶子结点:"<<z.Node_num(z.root); 234 temp.root=temp.Copy(z.root); 235 cout<<endl<<"temp的先序遍历:"; 236 temp.PreOrder(Visit); 237 238 z.Exchange(z.root); 239 cout<<endl<<"z的左右字数交换后的先序遍历:"; 240 z.PreOrder(Visit); 241 242 243 z.BreakTree(e,y,x); 244 z.PreOrder(Visit); //无输出 245 // x.PreOrder(Visit); 246 cout<<endl<<"z的高度:"<<z.High(z.root); 247 cout<<endl<<"z的叶子结点:"<<z.Node_num(z.root); 248 cout<<endl; 249 return 0; 250 }
时间: 2024-12-26 22:50:19