#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;
}
运行结果: #表示,留着是为了输出好看点
对比:
转载请注明原文地址: https://mac.8miu.com/read-508298.html