#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct Node
{
char data;
Node *lchild,*rchild;
Node(char _data)
{
data = _data;
}
};
void createTree(Node *&root,const int n,int i,const string &values)
{
if(i >= n )
{
root = NULL;
return ;
}
else
{
root = new Node(values[i]);
createTree(root->lchild, n, 2*i+1, values);
createTree(root->rchild, n, 2*i+2, values);
}
}
void printTree(Node *root,int k)
{
if(root != NULL)
{
printTree(root->rchild, k+5);
for(int i = 0; i < k; i++)
{
cout<<" ";
}
cout<<root->data<<endl;
printTree(root->lchild, k+5);
}
}
int main()
{
string values = "abic#jmde###k#l"; // #表示为空
Node * root = NULL;
createTree(root,values.length(),0,values);
printTree(root,0);
return 0;
}
运行结果:
#
表示,留着是为了输出好看点
对比: