c++递归创建二叉树

建树的两种递归版本:

1.无返回值版(较易理解)

#include 
#include 
using namespace std;
class BiTreeNode {
public:
    BiTreeNode *LeftNode;
    BiTreeNode *RightNode;
    char data;
    BiTreeNode() :LeftNode(NULL), RightNode(NULL) {}
};
class BiTree {
    BiTreeNode *Root;
    string strTree;
    int pos;
    void CreateBiTree(BiTreeNode *p);
    void PreOrder(BiTreeNode *p);
public:
    BiTree();
    ~BiTree();
    void CreateBiTree();
    void PreOrder();
};
BiTree::BiTree() {
    strTree = "";
    pos = 0;
    Root = new BiTreeNode();
}
BiTree::~BiTree() {
    BiTreeNode *p = Root;
    if (p == NULL)
        return;
    queue line;
    line.push(p);
    do {
        p = line.front();
        line.pop();
        if (p->LeftChild != NULL)
            line.push(p->LeftChild);
        if (p->RightChild != NULL)
            line.push(p->RightChild);
    } while (!line.empty());
    cout << "Destructed Tree" << endl;
}
void BiTree::CreateBiTree() {
    string str;
    cin >> str;
    strTree.assign(str);
    CreateBiTree(Root);
}
void BiTree::CreateBiTree(BiTreeNode *p) {
    char ch = strTree[pos++];
    if (ch != '0') {
        p->data = ch;
        if (strTree[pos] != '0')
            p->LeftNode = new BiTreeNode();
        CreateBiTree(p->LeftNode);
        if (strTree[pos] != '0')
            p->RightNode = new BiTreeNode();
        CreateBiTree(p->RightNode);
    }
    else 
        p = NULL;
}
void BiTree::PreOrder() {
    PreOrder(Root);
}
void BiTree::PreOrder(BiTreeNode *p) {  //先序遍历
    if (p != NULL) {
        cout << p->data;
        PreOrder(p->LeftNode);
        PreOrder(p->RightNode);
    }
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        BiTree T;
        T.CreateBiTree();
        T.PreOrder();
        cout << endl;
    }
    system("pause");
    return 0;
}

空结点用字符‘0’表示;

2.返回值为结点指针版

#include 
#include 
using namespace std;
class BiTreeNode {
public:
    BiTreeNode *LeftNode;
    BiTreeNode *RightNode;
    char data;
    BiTreeNode() :LeftNode(NULL), RightNode(NULL) {}
};
class BiTree {
    BiTreeNode *Root;
    string strTree;
    int pos;
    void PreOrder(BiTreeNode *p);
    BiTreeNode *CreateTree();
public:
    BiTree();
    ~BiTree();
    void CreateBiTree();
    void PreOrder();
};
BiTree::BiTree() {
    strTree = "";
    pos = 0;
    Root = new BiTreeNode();
}
BiTree::~BiTree() {

}
void BiTree::CreateBiTree() {
    string str;
    cin >> str;
    strTree.assign(str);
    Root = CreateTree();
}
BiTreeNode* BiTree::CreateTree() {
    char ch = strTree[pos++];
    BiTreeNode *T;
    if (ch == '0')
        return NULL;
    else {
        T  = new BiTreeNode();
        T->data = ch;
        T->LeftNode = CreateTree();
        T->RightNode = CreateTree();
    }
    return T;
}

void BiTree::PreOrder() {
    PreOrder(Root);
}
void BiTree::PreOrder(BiTreeNode *p) {  //先序遍历
    if (p != NULL) {
        cout << p->data;
        PreOrder(p->LeftNode);
        PreOrder(p->RightNode);
    }
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        BiTree T;
        T.CreateBiTree();
        T.PreOrder();
        cout << endl;
    }
    system("pause");
    return 0;
}


/********input******
2
AB0C00D00
AB00C00

/********output******
ABCD
BCAD
CBDA
ABC
BAC
BCA
********************/

你可能感兴趣的:(数据结构)