该完整程序主要是递归函数的使用及模板的使用,完成了对二叉树基本的链表操作,主要有二叉树的建立,前序、中序、后序遍历,求树的高度,每层结点数(包含树的最大宽度),左右结点对换,二叉树的内存释放,求解树的叶子数。
#include using namespace std; #define FLAG '#' typedef char Record; template struct Binary_Node { Entry data; Binary_Node Binary_Node Binary_Node(); Binary_Node(const Entry& x); }; template Binary_Node { left=NULL; right=NULL; } template Binary_Node { data=x; left=NULL; right=NULL; } template class Binary_tree { public: bool empty()const; Binary_tree(); Binary_tree(Binary_tree void create_tree(Binary_Node void recursive_copy(Binary_Node void pre_traverse(Binary_Node void mid_traverse(Binary_Node void post_traverse(Binary_Node void count_node(Binary_Node void count_leaves(Binary_Node int tree_height(Binary_Node void Ltree_To_Rtree(Binary_Node void count_width(Binary_Node int get_count()const;//得到树的结点数 Binary_Node void destroy_tree(Binary_Node Binary_tree protected: T*pre_visit; T*mid_visit; Binary_Node int node_numb; }; template Binary_tree { root=NULL; node_numb=0; } template void Binary_tree { if(tree==NULL) { return; } else { cur=new Binary_Node node_numb++; recursive_copy(tree->left,cur->left); recursive_copy(tree->right,cur->right); } } template Binary_tree { recursive_copy(org.root,root); } template Binary_tree { recursive_copy(org.root,root); return *this; } template bool Binary_tree { return root==NULL; } template int Binary_tree { return node_numb; } template void Binary_tree { T val; cout<<"请输入数据,若空以#输入: "; cin>>val; tree=new Binary_Node if(val==FLAG) { tree=NULL; return; } else { node_numb++; if(root==NULL) root=tree; create_tree(tree->left); create_tree(tree->right); } } template Binary_Node { return root; } template void Binary_tree { if(tree) { count++; count_node(tree->left,count); count_node(tree->right,count); } } template void Binary_tree { if(tree) { if((tree->left==NULL)&&(tree->right==NULL)) count++; count_leaves(tree->left,count); count_leaves(tree->right,count); } } template void Binary_tree { if(tree) { if(tree!=root) count++; count_width(tree->left,count_mid,count); count_width(tree->right,count_mid,count); count_mid[count]++; } } template int Binary_tree { int count_left=0, count_right=0; if(tree==NULL)return 0; else { count_left=tree_height(tree->left); count_right=tree_height(tree->right); return(count_left>count_right)?(count_left+1):(count_right+1); } } template void Binary_tree { if(tree) { Ltree_To_Rtree(tree->left); Ltree_To_Rtree(tree->right); if(tree->left!=NULL||tree->right!=NULL) { Binary_Node tree->left=tree->right; tree->right=temp; } } } template void Binary_tree { if(tree) { Binary_Node destroy_tree(tree->left); destroy_tree(tree->right); if(parent==root) { delete root; root=NULL; parent=NULL; node_numb--; } else{ delete parent; parent=NULL; node_numb--;} } } template void Binary_tree { if(root==NULL) { cout<<"树为空,不能遍历"< } if(tree) { cout<<" "< pre_traverse(tree->left); pre_traverse(tree->right); } } template void Binary_tree { if(root==NULL) { cout<<"树为空,不能遍历"< } if(tree) { mid_traverse(tree->left); cout<<" "< mid_traverse(tree->right); } } template void Binary_tree { if(root==NULL) { cout<<"树为空,不能遍历"< } if(tree) { post_traverse(tree->left); post_traverse(tree->right); cout<<" "< } } void main() { int m=0,leaves=0,Tree_height; Binary_tree Binary_Node Tree.create_tree(pp); Binary_tree cout<<"前序遍历结果为:"; Tree.pre_traverse(Tree.get_root()); cout< cout< cout< cout<<"结点个数为:"< cout<<"叶子个数为:"< cout<<"树高度为:"< int *count_wid=new int[Tree_height]; for(int i=0;i Tree.count_width(Tree.get_root(),count_wid,m); int Max=count_wid[0]; m=0; for(int i=0;i cout<<"在第"< if(Max Max=count_wid[i]; m=i; } } cout<<"在第"< Tree.destroy_tree(Tree.get_root()); delete []count_wid; } 树如下: (a) (b) (c) (d) (e) (f) (g) 输入: 输出: