C++根据前序和中序构造二叉树

#include <iostream>
#include <string.h>
using namespace std;

template<typename Type>
struct Node
{
    Type data;
    Node<Type> *left;
    Node<Type> *right;
    Node(Type d = Type()):data(d),left(NULL),right(NULL){}  
};

template<typename Type>
class MTree
{
    public:
    MTree()
    {
        root = NULL;
    }
    void Init(const char *s1,const char *s2)
    {
        int n = strlen(s1);
        Init(root,s1,s2,n);     
    }
    void Printf()
    {
        _Printf(root);
    }
    private:
    void _Printf(Node<Type> *t)
    {   
        if(t!=NULL)
        {
            cout<<t->data<<"\t";
            _Printf(t->left);
            _Printf(t->right);
        }
    }
    void Init(Node<Type> *&t,const char *&s1,const char *s2,int n)
    {
    //根据二叉树的前序跟后续构造二叉树.
      int i = 0;
        if(*s1=='\0')return;
        for(;i<=n;i++)
        {
            if(*s1==s2[i])
                {
                    break;
                }
        }
        if(i>n)return;
        t = new Node<Type>(*s1);
        Init(t->left,++s1,s2,i-1);
        Init(t->right,s1,s2+i+1,n-i);       
    }
    Node<Type> *root;
};
int main()
{
    MTree<char> mt;
    char *s1 = new char[7];
    char *s2 = new char[7];
    strcpy(s1,"ABCDEFG");
    strcpy(s2,"CBDAFEG");
    mt.Init(s1,s2);
    mt.Printf();
    return 0;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以的,以下是实现二叉树的基本操作的 C 代码: #include <stdio.h> #include <stdlib.h> // 二叉树的定义 typedef struct BinaryTreeNode { int data; struct BinaryTreeNode *left; struct BinaryTreeNode *right; } BinaryTreeNode; // 根据扩展二叉树前序遍历序列建立二叉树 BinaryTreeNode* createBinaryTree1() { BinaryTreeNode *p; int ch; scanf("%c", &ch); if (ch == ' ') { p = NULL; } else { p = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode)); p->data = ch; p->left = createBinaryTree1(); p->right = createBinaryTree1(); } return p; } // 根据二叉树前序中序遍历序列构造二叉树 BinaryTreeNode* createBinaryTree2(int *preorder, int *inorder, int len) { if (len == 0) { return NULL; } BinaryTreeNode *p = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode)); p->data = preorder[0]; int i; for (i = 0; i < len; i++) { if (inorder[i] == preorder[0]) { break; } } int leftlen = i; int rightlen = len - i - 1; p->left = createBinaryTree2(preorder + 1, inorder, leftlen); p->right = createBinaryTree2(preorder + 1 + leftlen, inorder + leftlen + 1, rightlen); return p; } // 二叉树前序遍历 void preOrder(BinaryTreeNode *p) { if (p != NULL) { printf("%d ", p->data); preOrder(p->left); preOrder(p->right); } } // 二叉树中序遍历 void inOrder(BinaryTreeNode *p) { if (p != NULL) { inOrder(p->left); printf("%d ", p->data); inOrder(p->right); } } // 二叉树的后序遍历 void postOrder(BinaryTreeNode *p) { if (p != NULL) { postOrder(p->left); postOrder(p->right); printf("%d ", p->data); } } int main() { // 手动输入前序遍历序列建立二叉树 BinaryTreeNode *root1 = createBinaryTree1(); // 根据前序中序遍历序列构造二叉树 int preorder[] = {1, 2, 4, 5, 3, 6, 7}; int inorder[] = {4, 2, 5, 1, 6, 3, 7}; BinaryTreeNode *root2 = createBinaryTree2(preorder, inorder, sizeof(preorder) / sizeof(int)); // 二叉树的遍历 printf("二叉树1的前序遍历:"); preOrder(root1); printf("\n"); printf("二叉树1的中序遍历:"); inOrder(root1); printf("\n"); printf("二叉树1的后序遍历:"); postOrder(root1); printf("\n"); printf("二叉树2的前序遍历:"); preOrder(root2); printf("\n"); printf("二叉树2的中序遍历:"); inOrder(root2); printf("\n"); printf("二叉树2的后序遍历:"); postOrder(root2); printf("\n"); return 0; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值