二叉树的基本实现和遍历——递归

来源:互联网 发布:程序员改bug 编辑:程序博客网 时间:2024/05/29 02:17

                                       二叉树

//创建结构体
template<class T>struct BinaryTreeNode{T _data;BinaryTreeNode<T>* _left;BinaryTreeNode<T>* _right;BinaryTreeNode(const T& x):_data(x), _left(NULL), _right(NULL){}};
//构造函数,拷贝构造函数template <class T>class BinaryTree{typedef BinaryTreeNode<T> Node;public:BinaryTree():_root(NULL){}BinaryTree(T* a, size_t n, const T& invalid)//构造函数{size_t index = 0;_root = _CreateTree(a, n, invalid, index);}Node* _CreateTree(T* a, size_t n, const T& invalid, size_t& index){Node* root = NULL;if (index < n && a[index] != invalid){root = new Node(a[index]);root->_left = _CreateTree(a, n, invalid, ++index);root->_right = _CreateTree(a, n, invalid, ++index);}return root;}BinaryTree(const BinaryTree& t)//拷贝函数{_root = _BinaryTree(t._root);}Node* _BinaryTree(Node* root){Node* newroot = NULL;if (root){//拷贝根节点newroot = new Node(root->_data);//拷贝左子树newroot->_left = _BinaryTree(root->_left);//拷贝右子树newroot->_right = _BinaryTree(root->_right);}return newroot;}~BinaryTree()//析构函数{_DestroyBinaryTree(_root);}void _DestroyBinaryTree(Node* &root){if (root){_DestroyBinaryTree(root->_left);_DestroyBinaryTree(root->_right);delete root;root = NULL;}}//递归实现四种遍历void PrevOrder()//前序遍历{cout << "PrevOrder" << endl;_PrevOrder(_root);cout << endl;}void _PrevOrder(Node* root){if (root == NULL){return;}cout << root->_data << " ";_PrevOrder(root->_left);_PrevOrder(root->_right);}void InOrder()//中序遍历{cout << "InOrder" << endl;_InOrder(_root);cout << endl;}void _InOrder(Node* root){if (root == NULL){return;}_InOrder(root->_left);cout << root->_data << " ";_InOrder(root->_right);}void PostOrder()//后序遍历{cout << "PostOrder" << endl;_PostOrder(_root);cout << endl;}void _PostOrder(Node* root){if (root == NULL){return;}_PostOrder(root->_left);_PostOrder(root->_right);cout << root->_data << " ";}void LevelOrder()//层序遍历{if (_root == NULL){return;}queue<Node*> q;q.push(_root);cout << "LevelOrder" << endl;while (!q.empty())//如果队列不为空{Node* cur = q.front();//取队头元素//取左、右子树if (cur->_left)q.push(cur->_left);if (cur->_right)q.push(cur->_right);//访问队头cout << cur->_data << " ";q.pop();}cout << endl;}
//查询结点个数和深度size_t Size()//结点个数{ return _Size(_root);}size_t _Size(Node* root){if (root == NULL){return 0;}return _Size(root->_left) + _Size(root->_right) + 1;}size_t LeafSize()//叶子结点个数{return _LeafSize(_root);}size_t _LeafSize(Node* root){if (root == NULL){return 0;}//二叉树不为空,左子树右子树为空else if (root->_left == NULL && root->_right == NULL){return 1;}//二叉树不为空,且左右子树不同时为空,返回左子树中叶子节点个数加上右子树中叶子节点个数return _LeafSize(root->_left) + _LeafSize(root->_right);}size_t GetKLevel(size_t k)//第k层的结点数{return _GetKLevel(_root, k);}size_t _GetKLevel(Node* root, size_t k){if (root == NULL || k < 1)return 0;if (k == 1)return 1;return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);}size_t Depth(){return _Depth(_root);}size_t _Depth(Node* root)//求二叉树深度{int hl = 0;int hr = 0;int max = 0;if (root == NULL){return 0;}else{hl = _Depth(root->_left);hr = _Depth(root->_right);max = hl > hr ? hl : hr;return max+1;}}
//查找、赋值Node* Find(const T& x)//查找二叉树结点{Node* cur = NULL;cur = _Find(_root, x);cout << _root->_data << " ";return cur;}Node* _Find(Node* root, const T& x){if (root == NULL)return NULL;if (root->_data == x)return root;Node* left = _Find(root->_left, x);if (left)return left;Node* right = _Find(root->_right, x);if (right)return right;return NULL;}
BinaryTree& operator=(const BinaryTree& t){        if (*this != &t){_DestroyBinaryTree(_root);_root = _BinaryTree(t._root);}return *this;}
protected:Node* _root;};

//测试代码void TestBinaryTree(){        int array[] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6, '#', '#' };BinaryTree<int> t(array, sizeof(array) / sizeof(array[0]), '#');t.PrevOrder();t.InOrder();        t.PostOrder();t.LevelOrder();//cout << "Find?" << t.Find(4) << endl;/*BinaryTree<int> t1(t);        t1.PrevOrder();        t1.PostOrder();        BinaryTree<int> t2 = t;t2.PrevOrder();*///t.LeafSize();//cout << "Size?" << t.Size() << endl;//cout << "LeafSize?" << t.LeafSize() << endl;//cout << "GetKLevel?" << t.GetKLevel(3) << endl;        //cout << "Depth?" << t.Depth() << endl;}


阅读全文
0 0