西工大数据结构理论第五部分,二叉树的应用

#include <iostream>
using namespace std;
typedef struct bintreenode{
    struct bintreenode *lchild;
    struct bintreenode *rchild;
    char value;
}node,*pnode;
pnode createnode(char &data)
{
    pnode p=(pnode)malloc(sizeof(bintreenode));
    if(!p)
    {
        cout<<"内存分配错误"<<endl;
        return NULL;
    }
    p->value=data;
    p->lchild=NULL;
    p->rchild=NULL;
    return p;
}
int i=0;
void create(pnode &root,string &str)
{
    root=createnode(str[i]);
    i++;
    if(str[i]=='(')
    {
        i++;
        create(root->lchild,str);
        create(root->rchild,str);
    }
    else if(str[i]==',')
    {
        i++;
        root->lchild=root->rchild=NULL;
    }
    else if(str[i]==')')
    {
        i=i+2;
        root->lchild=root->rchild=NULL;
    }
}
void inordertravse(pnode root)
{
    if(root==NULL)
        return;
    cout<<root->value;
    inordertravse(root->lchild);
    inordertravse(root->rchild);
}
int main()
{
    string str;
    cin>>str;
    pnode root=NULL;
    create(root,str);
    inordertravse(root);
    return 0;
}

 

#include <iostream>
using namespace std;
typedef struct bintreenode {
    struct bintreenode* lchild;
    struct bintreenode* rchild;
    char value;
}node, * pnode;
pnode createnode(char& data)
{
    pnode p = (pnode)malloc(sizeof(bintreenode));
    if (!p)
    {
        return NULL;
    }
    p->value = data;
    p->lchild = NULL;
    p->rchild = NULL;
    return p;
}
int i = 0;
int num = 0;
void create(pnode root, string& str)
{
    if (str[i] == '#')
    {
        i++;
        root = NULL;
        return;
    }
    root = createnode(str[i]);
    i++;
    if (str[i] != '#')
    {
        create(root->lchild, str);
        create(root->rchild, str);
    }
    else if (str[i + 1] == '#')//str[i]=='#
    {
        root->lchild = root->rchild = NULL;
        i = i + 2;
        num++;
    }
    else if (str[i + 1] != '#')//str[i]=='#
    {
        root->lchild = NULL;
        i++;
        create(root->rchild, str);
    }
}
int main()
{
    pnode root = NULL;
    string str;
    cin >> str;
    create(root, str);
    cout << num << endl;;
    return 0;
}

 

#include<iostream>
#include<vector>
using namespace std;
typedef struct treenode
{
    struct treenode* lchild;
    struct treenode* rchild;
    char value;
}node, * pnode;
pnode createnode(char data)
{
    pnode p = (pnode)malloc(sizeof(node));
    if (!p)
    {
        cout << "创建失败" << endl;
        return NULL;
    }
    p->value = data;
    p->lchild = NULL;
    p->rchild = NULL;
    return p;
}
pnode create(string preorder, string inorder, int preleft, int preright, int inleft, int inright)
{
    if (preleft > preright || inleft > inright)
        return NULL;
    char rootvalue = preorder[preleft];//先序遍历的根节点
    node* root = createnode(rootvalue);//创建根节点
    int index = 0; //记录中序遍历中根节点的位置
    for (int i = inleft; i <= inright; i++) {
        if (inorder[i] == rootvalue) {
            index = i;
            break;
        }
    }
    int leftsubtreesize = index - inleft;
    //递归构造左右子树
    root->lchild = create(preorder, inorder, preleft + 1, preleft+ leftsubtreesize, inleft, index - 1);
    root->rchild = create(preorder, inorder, preleft + leftsubtreesize + 1, preright, index + 1, inright);
    return root;
}
pnode createtree(string preorder, string inorder)
{
    int len = preorder.size();
    if (len == 0)
        return NULL;
    return create(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
}
void print(pnode root)
{
    if (root)
    {
        print(root->lchild);
        print(root->rchild);
        cout << root->value;
    }
}
int main()
{
    string preorder,inorder;
    cin>>preorder;
    cin>>inorder;
    pnode root = createtree(preorder, inorder);
    print(root);
    return 0;
}

 

#include<iostream>
#include<string>
using namespace std;
typedef struct treenode {
    struct treenode* lchild;
    struct treenode* rchild;
    char value;
}node, * pnode;
int i = 0;
pnode treeroot = NULL;
pnode createnode(char e)
{
    node* p = (pnode)malloc(sizeof(node));
    if (!p)
        return NULL;
    p->value = e;
    p->lchild = NULL;
    p->rchild = NULL;
    return p;
}
void create(pnode& root, string& str)
{

    if (str[i] == '#')
    {
        root = NULL;
        i++;
        return;
    }
    root = createnode(str[i]);
    if (i == 0)
    {
        treeroot = root;
    }
    i++;
    if (str[i] != '#')
    {
        create(root->lchild, str);
        create(root->rchild, str);
    }
    else if (str[i + 1] == '#')
    {
        root->lchild = root->rchild = NULL;
        i = i + 2;
    }
    else if (str[i + 1] != '#')
    {
        root->lchild = NULL;
        i++;
        create(root->rchild, str);
    }
}
void print(pnode root)
{
    if (!root)
    {
        return;
    }
    else
    {
        print(root->lchild);
        cout << root->value;
        print(root->rchild);
    }
}
int main()
{
    string str;
    cin >> str;
    node* root = NULL;
    create(root, str);
    print(root);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值