通过递归的方法构建二叉树.
一、二叉树结点定义以及一些基本函数定义
①二叉树结点:
#pragma once
#include<iostream>
using namespace std;
template <class T>
struct BinTNode
{
T data;
BinTNode<T>* lchild, * rchild;
BinTNode() :lchild(NULL), rchild(NULL) {
}
BinTNode(T x, BinTNode<T>* l = NULL, BinTNode<T>* r = NULL)
:data(x), lchild(l), rchild(r) {
}
};
②二叉树类:
template <class T>
class BinaryTree
{
protected:
BinTNode<T>* root;
T tag;
public:
BinaryTree() :root(NULL) {
}
BinaryTree(T x) :tag(x), root(NULL) {
}
~BinaryTree() {
DestroyTree(root); }
BinTNode<T>* GetRoot(BinTNode<T>* p) {
p = root; return p; }
void DestroyTree(BinTNode<T>* tree);
void CreateBinTree1(BinTNode<T>*& tree,char* pre);
void CreateBinTree2(BinTNode<T>*& tree,char* post);
void CreateBinTree3(BinTNode<T>*& tree,