二叉树的课堂代码
#include "iostream"
#include "assert.h"
using namespace std;
template <class T>
struct BinTreeNode {
T data;
BinTreeNode<T> *leftChild, *rightChild;
BinTreeNode()
{
leftChild = NULL; rightChild = NULL;
}
BinTreeNode(T x, BinTreeNode<T> *l = NULL, BinTreeNode<T> *r = NULL)
{
data = x; leftChild = l; rightChild = r;
}
};
template <class T>
class BinaryTree {
public:
BinaryTree() : root(NULL) { }
BinaryTree(T value) : RefValue(value), root(NULL) { }
BinaryTree(BinaryTree<T>& s)
{
if (this != &s)
this->root = CopyTree(s.root);
}
~BinaryTree() { destroy(root); }
bool IsEmpty() { return root == NULL; }
int Height() { return Height(root); }
int Size() { return Size(root); }
BinTreeNode<T> *Parent(BinTreeNode <T> *t)
{
return (root == NULL || root == t) ? NULL : Parent(root, t);
}
BinTreeNode<T> *LeftChild(BinTreeNode<T> *t)
{
return (t != NULL) ? t->leftChild : NULL;
}
BinTreeNode<T> *RightChild(BinTreeNode<T> *t)
{
return (t != NULL) ? t->rightChild : NULL;
}
BinTreeNode<T> *getRoot() const { return root; }
void levelOrder();
void PreOrder()
{
PreOrder(root);
}
void InOrder()
{
InOrder(root);
}
void PostOrder()
{
PostOrder(root);
}
void CreateBinTree() { CreateBinTree(root); };
void setRefValue(T x) { RefValue = x; }
BinTreeNode<T>* CopyTree(BinTreeNode<T>*s);
protected:
BinTreeNode<T> *root;
T RefValue;
void PreOrder(BinTreeNode<T> *subTree);
void InOrder(BinTreeNode<T> *subTree);
void PostOrder(BinTreeNode<T> *Tree);
void CreateBinTree(BinTreeNode<T> *&Tree);
void destroy(BinTreeNode<T> *& subTree);
int Height(BinTreeNode<T> *subTree)const;
int Size(BinTreeNode<T> *subTree)const;
};
template<class T>
void BinaryTree<T>::destroy(BinTreeNode<T> *& subTree)
{
if (subTree != NULL)
{
destroy(subTree->leftChild);
destroy(subTree->rightChild);
delete subTree;
}
}
template<class T>
BinTreeNode<T>* BinaryTree<T>::CopyTree(BinTreeNode<T>*s)
{
BinTreeNode<T>*temp = NULL;
if (s == NULL)
{
temp = NULL;
}
else
{
temp = new BinTreeNode<T>(s->data);
if (s->leftChild)temp->leftChild = CopyTree(s->leftChild);
if (s->rightChild)temp->rightChild = CopyTree(s->rightChild);
}
return temp;
}
template <class T>
void BinaryTree<T>::PreOrder(BinTreeNode<T> *subTree)
{
if (subTree != NULL) {
cout << subTree->data << " ";;
PreOrder(subTree->leftChild);
PreOrder(subTree->rightChild);
}
}
template <class T>
void BinaryTree<T>::InOrder(BinTreeNode<T> * subTree)
{
if (subTree != NULL) {
InOrder(subTree->leftChild);
cout << subTree->data << " ";
InOrder(subTree->rightChild);
}
}
template <class T>
void BinaryTree<T>::PostOrder(BinTreeNode<T> * subTree)
{
if (subTree != NULL) {
PostOrder(subTree->leftChild);
PostOrder(subTree->rightChild);
cout << subTree->data << " ";;
}
}
template<class T>
void BinaryTree<T>::CreateBinTree(BinTreeNode<T>*&subTree)
{
T item;
cin >> item;
if (item != RefValue)
{
subTree = new BinTreeNode<T>(item);
if (subTree == NULL)
{
cerr << "存储分配错!" << endl; exit(1);
}
CreateBinTree(subTree->leftChild);
CreateBinTree(subTree->rightChild);
}
else subTree = NULL;
}
template <class T>
int BinaryTree<T>::Size(BinTreeNode<T> * subTree) const {
if (subTree == NULL) return 0;
else return 1 + Size(subTree->leftChild) + Size(subTree->rightChild);
};
template <class T>
int BinaryTree<T>::Height(BinTreeNode<T> * subTree) const
{
if (subTree == NULL) return 0;
else {
int i = Height(subTree->leftChild);
int j = Height(subTree->rightChild);
return (i < j) ? j + 1 : i + 1;
}
}
int main()
{
BinaryTree<int> root, root1;
root.setRefValue(0);
cout << "二叉建立:" << endl;
root.CreateBinTree();
cout << "前序遍历输出二叉树:";
root.PreOrder();
cout << endl;
cout << "先序遍历输出二叉树(非递归调用):" << endl;
cout << "中序遍历输出二叉树:";
root.InOrder();
cout << endl;
cout << "后序遍历输出二叉树:";
root.PostOrder();
cout << endl;
cout << "输出二叉树高度:";
cout << root.Height() << endl;
cout << "输出二叉树结点数目:";
cout << root.Size() << endl;
return 0;
}