先序遍历和中序遍历创建二叉树

 #include <iostream>
 #include <cstdio>
 #include <cstring>
 #define N 50
 using namespace std;

 struct node_t {
    char data;
    struct node_t *lchild;
    struct node_t *rchild;
    node_t() {};
    node_t(char ch, node_t *l = NULL, node_t *r = NULL)
     : data(ch), lchild(l), rchild(r)
     {

     }
 };

 node_t* build_tree(int n, char *pre_str, char *in_str)
 {
    if(n <= 0)
        return NULL;

    int l_len = strchr(in_str, pre_str[0]) - in_str;
    int r_len = n - l_len - 1;

    node_t *root = new node_t(pre_str[0]);
    root->lchild = build_tree(l_len, pre_str + 1, in_str);
    root->rchild = build_tree(r_len, pre_str + l_len + 1, in_str + l_len + 1);

    return root;
 }

 void visit(node_t *p)
 {
     if(p != NULL)
        printf("%c", p->data);
 }

 void pre_traverse(node_t *T)
 {
     node_t *stack[N], *p = T;
     int top = -1;

     while(p != NULL || top != -1) {
        while(p != NULL) {
            visit(p);
            stack[++top] = p;
            p = p->lchild;
        }
        p = stack[top--];
        p = p->rchild;
     }
 }

 void lay_traverse(node_t *T)
 {
     node_t *queen[N], *p;
     int front, rear;

     if( T != NULL) {
        queen[0] = T;
        front = -1;
        rear = 0;

        while(front < rear) {
            p = queen[++front];
            visit(p);
            if(p->lchild != NULL)
                queen[++rear] = p->lchild;
            if(p->rchild != NULL)
                queen[++rear] = p->rchild;
        }
     }
 }

 void in_traverse(node_t *T)
 {
     node_t *stack[N], *p = T;
     int top = -1;

     while(p != NULL || top != -1) {
        while(p != NULL) {
            stack[++top] = p;
            p = p->lchild;
        }
        p = stack[top--];
        visit(p);
        p = p->rchild;
     }
 }

 void post_traverse(node_t *T)
 {
     node_t *stack[N], *p = T;
     int stack_flag[N];
     int flag, top = -1;

     while(p != NULL || top != -1) {
        while(p != NULL) {
            stack[++top] = p;
            stack_flag[top] = 0;
            p = p->lchild;
        }
        p = stack[top];
        flag = stack_flag[top--];
        if(flag == 0) {
            stack[++top] = p;
            stack_flag[top] = 1;
            p = p->rchild;
        } else {
            visit(p);
            p = NULL;
        }
     }
 }

 int tree_depth(node_t *T)
 {
     if(T == NULL)
        return 0;
     else {
        int ldepth = tree_depth(T->lchild);
        int rdepth = tree_depth(T->rchild);

        if(ldepth > rdepth)
            return ldepth + 1;
        else
            return rdepth + 1;
     }
 }

 int tree_depth2(node_t *T)
 {
     node_t *stack[N], *p = T;
     int stack_depth[N];
     int curdepth, maxdepth = 0, top = -1;

     if(p != NULL) {
        curdepth = 1;
        while(p != NULL || top != -1) {
            while(p != NULL) {
                stack[++top] = p;
                stack_depth[top] = curdepth;
                curdepth++;
                p = p->lchild;
            }
            p = stack[top];
            curdepth = stack_depth[top--];
            if(p->lchild == NULL && p->rchild == NULL) {
                if(curdepth > maxdepth)
                    maxdepth = curdepth;
            }
            curdepth++;
            p = p->rchild;
        }
     }

     return maxdepth;
 }

 int node_depth(node_t *T, char key)
 {
     node_t *stack[N], *p = T;
     int stack_flag[N];
     int flag, top = -1;

     while(p != NULL || top != -1) {
        while(p != NULL) {
            stack[++top] = p;
            stack_flag[top] = 0;
            p = p->lchild;
        }
        p = stack[top];
        flag = stack_flag[top--];
        if(flag == 0) {
            stack[++top] = p;
            stack_flag[top] = 1;
            p = p->rchild;
        } else {
            if(p->data == key)
                return top + 2;
            p = NULL;
        }
     }
 }

 int main()
 {
     char pre_str[N], in_str[N];
     int in_len;

     while(scanf("%s%s", pre_str, in_str) != EOF) {
        in_len = strlen(pre_str);
        node_t *root = build_tree(in_len, pre_str, in_str);

        printf("先序遍历:");
        pre_traverse(root);
        printf("\n");
        printf("中序遍历:");
        in_traverse(root);
        printf("\n");
        printf("后序遍历:");
        post_traverse(root);
        printf("\n");
        printf("层次遍历:");
        lay_traverse(root);
        printf("\n");
        printf("depth = %d\n", tree_depth(root));
        printf("depth2 = %d\n", tree_depth2(root));
        printf("node %c depth = %d\n", 'c', node_depth(root, 'c'));
     }
     return 0;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值