// binarytree.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <string> #include <deque> #include <cassert> using namespace std; /*模拟二叉树的一些操作: 作者:wangyang 时间:2010年10月29日 */ /*数的节点类*/ template <typename T> class TNode { public: TNode(){} //构造函数 TNode(T data, TNode<T> *lptr = NULL, TNode<T> *rptr = NULL) { nodeValue = data; left = lptr; right = rptr; } //数据成员 T nodeValue; TNode<T> *left; TNode<T> *right; }; /*二叉树类*/ template <typename T> class BinaryTree { public: TNode<T>* createTree(T arr[],int start, int end);//创建二叉树 void inorderOutput(TNode<T>* root, const string& separator = " ");//中序输出树的节点 void postorderOutput(TNode<T>* root, const string& separator = " ");//后序输出树的节点 void beforeorderOutput(TNode<T>* root, const string& separator = " ");//前序输出树的节点) void levelorderOutput(TNode<T>* root, const string& separator = " ");//层次遍历树的节点 void countLeaf(TNode<T> *t, int &count);//计算叶子节点的数目 int depth(TNode<T>* root);//计算数的深度 private: TNode<T> *root; //树根 }; /*二叉树的创建:arr为数组,start为起点,end的终点*/ template <typename T> TNode<T>* BinaryTree<T>::createTree(T arr[], int start, int end) { TNode<T> *p;//树的节点 if (start >= end) { p = NULL; } else { p = new TNode<T>(); assert(p != NULL); p->nodeValue = arr[start]; p->left = createTree(arr, 2*start+1, end); p->right = createTree(arr, 2*start+2, end); } return p; } /*中序递归输出二叉树中的元素:*/ /*参数root为数的根 参数separator为两个元素间的间隔符*/ template <typename T> void BinaryTree<T>::inorderOutput(TNode<T> *root, const string& separator /* = */) { if (root != NULL) { inorderOutput(root->left, separator); cout<<root->nodeValue<<separator; inorderOutput(root->right, separator); } } /*后序递归输出二叉树中的元素:*/ /*参数root为数的根 参数separator为两个元素间的间隔符*/ template <typename T> void BinaryTree<T>::postorderOutput(TNode<T> *root, const string& separator /* = */) { if (root != NULL) { postorderOutput(root->left, separator); postorderOutput(root->right, separator); cout<<root->nodeValue<<separator; } } /*前序递归输出二叉树中的元素:*/ /*参数root为数的根 参数separator为两个元素间的间隔符*/ template <typename T> void BinaryTree<T>::beforeorderOutput(TNode<T> *root, const string& separator /* = */) { if (root != NULL) { cout<<root->nodeValue<<separator; beforeorderOutput(root->left, separator); beforeorderOutput(root->right, separator); } } /*层次遍历树的节点:借助队列做存储容器*/ /*参数root为数的根 参数separator为两个元素间的间隔符*/ template <typename T> void BinaryTree<T>::levelorderOutput(TNode<T>* root, const string& separator /* = */) { deque<TNode<T> *> d; TNode<T> *temp; d.push_back(root); while (!d.empty()) { temp = d.front(); cout<<temp->nodeValue<<separator; d.pop_front(); if (temp->left != NULL) { d.push_back(temp->left); } if (temp->right != NULL) { d.push_back(temp->right); } } } /*计算叶子节点的数目:通过递归来实现*/ /*参数root为数的根 参数count为引用计数,初值必须为0*/ template <typename T> void BinaryTree<T>::countLeaf(TNode<T> *t, int &count) { if (t != NULL) { //检查t是否为叶子节点,如是count加1 if ((t->left != NULL) && (t->right != NULL)) { count++; } countLeaf(t->left, count);//计算左子树 countLeaf(t->right, count);//计算右子树 } } /*计算树的深度:运用递归实现,定义空树的深度为-1,这样叶子节点的深度为0*/ template <typename T> int BinaryTree<T>::depth(TNode<T>* root) { int leftdepth;//左子树深度 int rightdepth;//右子树深度 int currentdepth;//当前节点深度 if (root == NULL) { currentdepth = -1; } else { leftdepth = depth(root->left); rightdepth = depth(root->right); currentdepth = max(leftdepth, rightdepth) + 1; } return currentdepth; } int _tmain(int argc, _TCHAR* argv[]) { int arr[10]; int size = sizeof(arr) / sizeof(int); cout<<"原数组为:"; for (int i=0; i<size; i++) { arr[i] = rand()/100 + 10; cout<<arr[i]<<" "; } cout<<endl; BinaryTree<int> bintree; TNode<int> *root; root = bintree.createTree(arr, 0, size); cout<<"中序为 :"; bintree.inorderOutput(root); cout<<endl; cout<<"后序为 :"; bintree.postorderOutput(root); cout<<endl; cout<<"前序为 :"; bintree.beforeorderOutput(root); cout<<endl; cout<<"层次为 :"; bintree.levelorderOutput(root); cout<<endl; int count = 0; bintree.countLeaf(root, count); cout<<"叶子节点的数量:"<<count<<endl; cout<<"当前树的深度为:"<<bintree.depth(root)<<endl; return 0; }