赞
踩
binaryTreeNode.h
#pragma once #include<iostream> template<class T> struct binaryTreeNode { T element; binaryTreeNode<T>* leftChild, * rightChild; binaryTreeNode() {} binaryTreeNode(const T& element, binaryTreeNode<T>* leftChild = NULL, binaryTreeNode<T>* rightChild = NULL) { this->element = element; this->leftChild = leftChild; this->rightChild = rightChild; } };
linkedBinaryTree.h
#ifndef linkedBinaryTree_ #define linkedBinaryTree_ #include"binaryTreeNode.h" #include<queue> #include<iostream> using namespace std; template<class T> class linkedBinaryTree { public: linkedBinaryTree(); ~linkedBinaryTree(); bool empty()const; int size()const; binaryTreeNode<T>* createBinaryTree(binaryTreeNode<T>* t); void createBinaryTree(); int height_recursion(binaryTreeNode<T>* t);//递归求高度 int height_level();//层次遍历求高度 int height(); void preOrder(void (*theVisit)(binaryTreeNode<T>*)); void inOrder(void (*theVisit)(binaryTreeNode<T>*)); void postOrder(void (*theVisit)(binaryTreeNode<T>*)); void levelOrder(void(*theVisit)(binaryTreeNode<T>*)); void preOrderOutput(); void inOrderOutput(); void postOrderOutput(); void erase(); private: binaryTreeNode<T>* root; int treesize; static void (*visit)(binaryTreeNode<T>*); static void preOrder(binaryTreeNode<T>*); static void inOrder(binaryTreeNode<T>*); static void postOrder(binaryTreeNode<T>*); static void dispose(binaryTreeNode<T>*); static void output(binaryTreeNode<T>*); }; template<class T> void(*linkedBinaryTree<T>::visit)(binaryTreeNode<T>*) = NULL; #endif
linkedBinaryTree.cpp
#include "linkedBinaryTree.h" #include<queue> template<class T> linkedBinaryTree<T>::linkedBinaryTree() { root = NULL; treesize = 0; } template<class T> linkedBinaryTree<T>::~linkedBinaryTree() { erase(); } template<class T> bool linkedBinaryTree<T>::empty() const { return treesize == 0; } template<class T> int linkedBinaryTree<T>::size() const { return treesize; } template<class T> binaryTreeNode<T>* linkedBinaryTree<T>::createBinaryTree(binaryTreeNode<T>* t) { T element; cin >> element; if (element == '#') { return NULL; } else { treesize++; t = new binaryTreeNode<T>(element); t->leftChild = createBinaryTree(t->leftChild); t->rightChild = createBinaryTree(t->rightChild); } return t; } template<class T> void linkedBinaryTree<T>::createBinaryTree() { root = createBinaryTree(root); } template<class T> int linkedBinaryTree<T>::height_recursion(binaryTreeNode<T>* t) { if (t == NULL) { return 0; } int h1 = height_recursion(t->leftChild); int h2 = height_recursion(t->rightChild); if (h1 > h2) { return h1 + 1; } else { return h2 + 1; } } template<class T> int linkedBinaryTree<T>::height_level() { queue<binaryTreeNode<T>*> q; q.push(root); int n = 0,len; binaryTreeNode<T>* cur; while (!q.empty()) { n++; len = q.size(); for (int i = 0; i < len; i++) { cur = q.front(); q.pop(); if (cur->leftChild != NULL) { q.push(cur->leftChild); } if (cur->rightChild != NULL) { q.push(cur->rightChild); } } } return n; } template<class T> int linkedBinaryTree<T>::height() { return height_recursion(root); } template<class T> void linkedBinaryTree<T>::preOrder(void(*theVisit)(binaryTreeNode<T>*)) { visit = theVisit; preOrder(root); } template<class T> void linkedBinaryTree<T>::inOrder(void(*theVisit)(binaryTreeNode<T>*)) { visit = theVisit; inOrder(root); } template<class T> void linkedBinaryTree<T>::postOrder(void(*theVisit)(binaryTreeNode<T>*)) { visit = theVisit; postOrder(root); } template<class T> void linkedBinaryTree<T>::levelOrder(void(*theVisit)(binaryTreeNode<T>*)) { queue<binaryTreeNode<T>*>q; q.push(root); while (q != NULL) { binaryTreeNode<T>* cur = q.front(); q.pop(); theVisit(cur); if (cur->leftChild != NULL) { q.push(cur->leftChild); } if (cur->rightChild != NULL) { q.push(cur->rightChild); } } } template<class T> void linkedBinaryTree<T>::preOrderOutput() { preOrder(output); cout << "\n"; } template<class T> void linkedBinaryTree<T>::inOrderOutput() { inOrder(output); cout << "\n"; } template<class T> void linkedBinaryTree<T>::postOrderOutput() { postOrder(output); cout << "\n"; } template<class T> void linkedBinaryTree<T>::erase() { postOrder(dispose); root = NULL; treesize = 0; } template<class T> void linkedBinaryTree<T>::preOrder(binaryTreeNode<T>* t) { if (t != NULL) { linkedBinaryTree<T>::visit(t); preOrder(t->leftChild); preOrder(t->rightChild); } } template<class T> void linkedBinaryTree<T>::inOrder(binaryTreeNode<T>* t) { if (t != NULL) { inOrder(t->leftChild); linkedBinaryTree<T>::visit(t); inOrder(t->rightChild); } } template<class T> void linkedBinaryTree<T>::postOrder(binaryTreeNode<T>* t) { if (t != NULL) { postOrder(t->leftChild); postOrder(t->rightChild); linkedBinaryTree<T>::visit(t); } } template<class T> void linkedBinaryTree<T>::dispose(binaryTreeNode<T>* t) { delete t; } template<class T> void linkedBinaryTree<T>::output(binaryTreeNode<T>* t) { cout << t->element << " "; }
test.cpp
#include<iostream> using namespace std; #include"linkedBinaryTree.h" #include"linkedBinaryTree.cpp" int main() { linkedBinaryTree<char> binaryTree; binaryTree.createBinaryTree(); cout << "前序遍历:"; binaryTree.preOrderOutput(); cout << "中序遍历:"; binaryTree.inOrderOutput(); cout << "后序遍历:"; binaryTree.postOrderOutput(); cout << "该二叉树的高度为:" << binaryTree.height()<<"\n"; cout << "该二叉树的高度为:" << binaryTree.height_level() << "\n"; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。