#include <iostream>
#include <string>
using namespace std;
class tree
{
public:
tree()
{
//root = create();
root = NULL;
}
/***输入扩展层次遍历序列,#表示该节点为空***/
tree(char *s)
{
root = NULL;
if (s == NULL)
{
return;
}
int size = strlen(s);
create(&root,s,0,size);
}
~tree()
{
/***清空二叉树***/
}
/***二叉排序树:插入。***/
void insert(char *s)
{
if (s == NULL)
{
return;
}
int size = strlen(s);
for (int i = 0; i<size; i++)
{
insert(&root, s[i]);
}
}
void preOrder(){ pOrder(root);}
void inOreder(){ zOrder(root);}
void postOreder(){ hOrder(root);}
private:
struct treenode
{
char data;
treenode * left;
treenode * right;
};
treenode *root;
void insert(treenode **p, char s)
{
if (((*p) == NULL) && s != '\0')
{
*p = new treenode;
(*p)->data = s;
(*p)->left = NULL;
(*p)->right = NULL;
}
else
{
if ((*p)->data > s)
{
insert(&((*p)->left) , s);
}
else
{
insert(&((*p)->right) , s);
}
}
}
treenode* create()
{
treenode *p;
char t;
cout<<"请输入:"<<endl;
t = getchar();
if (t=='#')
{
p = NULL;
}
else
{
p = new treenode;
p->data = t;
cout<<"create tree node: "<<t<<endl;
p->left = create();
p->right = create();
}
return p;
}
void create(treenode **p, char *str, int i, int size)
{
if (i>size-1 || str[i] == '\0')
{
*p = NULL;
return;
}
if (str[i] == '#')
{
*p=NULL;
}
else
{
*p = new treenode;
(*p)->data = str[i];
create(&((*p)->left),str,2*i+1,size);
create(&((*p)->right),str,2*i+2,size);
}
}
void pOrder(treenode *p)
{
if (p==NULL)
{
return;
}
cout<<p->data<<" "<<endl;
pOrder(p->left);
pOrder(p->right);
}
void zOrder(treenode *p)
{
if (p==NULL)
{
return;
}
zOrder(p->left);
cout<<p->data<<" "<<endl;
zOrder(p->right);
}
void hOrder(treenode *p)
{
if (p==NULL)
{
return;
}
hOrder(p->left);
cout<<p->data<<" "<<endl;
hOrder(p->right);
}
};
int main()
{
/***扩展层次序列简立树***/
//char t[9] = "ABCDE#FG";
//tree s(t);
//s.preOrder();
/***建立二叉排序树***/
tree s;
char t[9] = "57821639";
s.insert(t);
s.postOreder();
cout<<"Over"<<endl;
return 0;
}