代码:(有待改进)
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int pow(int x,int y)
{
static int res = 1;
if(y == 0)
return res;
else
{
res*=x;
pow(x,y-1);
}
}
class binaryTreeNode
{
public:
int data;
binaryTreeNode *leftChild,*rightChild;
};
class binaryTree : public binaryTreeNode
{
private:
binaryTreeNode *root;
public:
binaryTree()
{
root = new binaryTreeNode;
root->leftChild = NULL;
root->rightChild = NULL;
}
~binaryTree()
{
destory(root);
}
void destory(binaryTreeNode *&subtree)
{
if(subtree == NULL)
return ;
destory(subtree->leftChild);
destory(subtree->rightChild);
delete subtree;
}
void inorderCreatTree()
{
inorderCreatTree(root);
}
void inorderCreatTree(binaryTreeNode *&subtree)
{
int dx;
cin>>dx;
if(dx == 0)
{
subtree = NULL;
return;
}
subtree = new binaryTreeNode;
inorderCreatTree(subtree->leftChild);
subtree->data = dx;
inorderCreatTree(subtree->rightChild);
}
void preorderCreatTree()
{
preorderCreatTree(root);
}
void preorderCreatTree(binaryTreeNode *&subtree)
{
int dx;
cin>>dx;
if(dx == -1)
{
subtree = NULL;
return;
}
subtree = new binaryTreeNode;
subtree->data = dx;
preorderCreatTree(subtree->leftChild);
preorderCreatTree(subtree->rightChild);
}
void postorderCreatTree()
{
postorderCreatTree(root);
}
void postorderCreatTree(binaryTreeNode *&subtree)
{
int dx;
cin>>dx;
if(dx == -1)
{
subtree = NULL;
return;
}
subtree = new binaryTreeNode;
postorderCreatTree(subtree->leftChild);
postorderCreatTree(subtree->rightChild);
subtree->data = dx;
}
void inoder()
{
inorder(root);
}
const void inorder(binaryTreeNode *subtree)
{
if(subtree == NULL)
return ;
inorder(subtree->leftChild);
cout<<subtree->data<<endl;
inorder(subtree->rightChild);
}
void preorder()
{
preorder(root);
}
const void preorder(binaryTreeNode *subtree)
{
if(subtree == NULL)
return ;
cout<<subtree->data<<' ';
preorder(subtree->leftChild);
preorder(subtree->rightChild);
}
void postorder()
{
postorder(root);
}
const void postorder(binaryTreeNode *subtree)
{
if(subtree == NULL)
return ;
postorder(subtree->leftChild);
postorder(subtree->rightChild);
cout<<subtree->data<<' ';
}
};
int main()
{
binaryTree Test1;
Test1.inorderCreatTree();
Test1.inoder();
system("pause");
return 0;
}