.h文件
typedef unsigned char elemtype;
#include <stdio.h>
#include <iostream>
#include <tchar.h>
#include <stack>
using namespace std;
struct BinaryTreeNode
{
elemtype val; //元素
BinaryTreeNode *left_child, *right_child; //左右孩纸指针
BinaryTreeNode(const elemtype &_val) :val(_val), left_child(NULL), right_child(NULL){}
//构造函数
};
class BinaryTreeC //定义类,集成函数
{
public:
BinaryTreeC();
BinaryTreeC(const BinaryTreeC &rhs);
~BinaryTreeC();
struct BinaryTreeNode *create_tree(elemtype *pre,elemtype *mid , int n);
void preorder(const BinaryTreeNode *root) const;
void midorder(const BinaryTreeNode *root) const;
void postorder(const BinaryTreeNode *root) const;
struct BinaryTreeNode * exp_createtree(const string &str) const;
void Destroy_binarytree(BinaryTreeNode *root);
int Depth_binarytree(const BinaryTreeNode *root) const;
public:
BinaryTreeNode *Root;
}
cpp文件
BinaryTreeC::BinaryTreeC()
{
}
BinaryTreeC::BinaryTreeC(const BinaryTreeC &rhs)
{
this->Root = rhs.Root;
}
BinaryTreeC::~BinaryTreeC()
{
Destroy_binarytree(this->Root);
}
struct BinaryTreeNode* BinaryTreeC::create_tree(elemtype *pre, elemtype *mid, int n)
{
if (n==0) return NULL;
int i;
elemtype c = pre[0];
struct BinaryTreeNode *node = new BinaryTreeNode(c);
//This is the root node of this tree/sub tree.
for (i = 0; i<n && mid[i] != c; i++);
int lenL = i; // the node number of the left child tree.
int lenR = n - i - 1; // the node number of the rigth child tree.
if (lenL > 0) node->left_child = create_tree(&pre[1], &mid[0], lenL);
//build the left child tree.
if (lenR > 0) node->right_child = create_tree(&pre[lenL + 1], &mid[lenL + 1], lenR);
//build the right child tree.
return node;
}
void BinaryTreeC::preorder(const BinaryTreeNode *root) const
{
if (root!=NULL)
{
cout << root->val << endl;
preorder(root->left_child);
preorder(root->right_child);
}
}
void BinaryTreeC::midorder(const BinaryTreeNode *root) const
{
if (root != NULL)
{
preorder(root->left_child);
cout << root->val << endl;
preorder(root->right_child);
}
}
void BinaryTreeC::postorder(const BinaryTreeNode *root) const
{
if (root != NULL)
{
preorder(root->left_child);
preorder(root->right_child);
cout << root->val << endl;
}
}
struct BinaryTreeNode* BinaryTreeC::exp_createtree(const string &str) const
{
int i = 0;
BinaryTreeNode *current=NULL;
stack<BinaryTreeNode *> Stack;
while (str[i])
{
if (str[i] == ' ') //如果是空格,则继续读入
{
i++;
continue;
}
current = new BinaryTreeNode(str[i]);
if (str[i] >= 97 && str[i] <= 122) //字母入栈,小写的字母
Stack.push(current);
else{
current->right_child = Stack.top();
Stack.pop();
current->left_child = Stack.top();
Stack.pop();
Stack.push(current);
}
++i;
}
return current;
}
void BinaryTreeC::Destroy_binarytree(BinaryTreeNode *root)
{
if (root->left_child) Destroy_binarytree(root->left_child);
if (root->right_child) Destroy_binarytree(root->right_child);
free(root);
root = NULL;
}
int BinaryTreeC::Depth_binarytree(const BinaryTreeNode *root) const
{
if (!root) return 0;
return 1 + (Depth_binarytree(root->left_child) > Depth_binarytree(root->right_child))? Depth_binarytree(root->left_child) : Depth_binarytree(root->right_child);
}
main函数:
int _tmain(int argc, _TCHAR* argv[])
{
while (1)
{
BinaryTreeC *bt;
string s = "ab+cd+*";
bt = new BinaryTreeC();
bt->Root = bt->exp_createtree(s);
bt->preorder(bt->Root);
}
return 0;
}
运行结果:*+ab+cd
由于是自己写的,有很多不完善的地方,有什么要添加的函数,欢迎大家和我联系
转载于:https://my.oschina.net/u/2420022/blog/481362