#include "BinaryTree.h"
#include<iostream>
#include<string>
using namespace std;
Node * BinaryTree::Create(char * vlr, char * lvr, int n)
{
if (n == 0) return NULL;
int k = 0;
while (vlr[0] != lvr[k])k++;
Node *p = new Node(vlr[0]);
p->left = Create(vlr + 1, lvr, k);
p->right = Create(vlr + k + 1, lvr + k + 1, n - k - 1);
return p;
}
void BinaryTree::destory(Node * sub)
{
if (sub != NULL) {
destory(sub->left);
destory(sub->right);
delete sub;
}
}
void BinaryTree::Input(char *array)
{
char d;
int i = 0;
while (1) {
cin >> d;
if (d == '#')break;
array[i] = d;
i++;
}
N = i + 1;
}
void BinaryTree::Output(Node *tree, string s)
{
if (tree != NULL) {
s = s + "____";
Output(tree->left, s);
cout << s << tree->date << endl;
Output(tree->right, s);
}
}
void BinaryTree::preOrder(Node *sub)
{
if (sub != NULL) {
cout << sub->date;
preOrder(sub->left);
preOrder(sub->right);
}
}
void BinaryTree::inOrder(Node *sub)
{
if (sub != NULL) {
inOrder(sub->left);
cout << sub->date;
inOrder(sub->right);
}
}
void BinaryTree::postOrder(Node *sub)
{
if (sub != NULL) {
postOrder(sub->left);
postOrder(sub->right);
cout << sub->date;
}
}
void BinaryTree::Root(char *VLR, char *LVR)
{
string str = "";
root = Create(VLR, LVR, N);
cout << "后序遍历:";
postOrder(root);cout << endl;
cout << "凹入法输出:" << endl;Output(root, str);
cout << "前序遍历:";
preOrder(root); cout << endl;
cout << "中序遍历:";
inOrder(root); cout << endl;
}