数据结构-树的基本操作

//ABC##DE#G##F###
//       A
//    B     #
//  C    D
// # # E   F
//    # G # #
//     # #
//先序:ABCDEFG
//中序:CBEGDFA
//后序:CGEFDBA
#include<stdio.h>
#include<stdlib.h>

#define MAX_TREE_SIZE 100
#define OK 1
#define OVERFLOW 1
typedef struct BiTNode {
    char data;
    struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;

//按先序次序输入扩展的正则二叉树中结点的值(一个字符)
//构造二叉链表表示的二叉树BT
int CreateBiTree(BiTree &BT) {
    char ch;
    scanf("%c", &ch);
    if (ch == '#') BT = NULL;
    else {
        if (!(BT = (BiTree) malloc(sizeof(BiTNode)))) {
            exit(OVERFLOW);
        }
        BT->data = ch;
        CreateBiTree(BT->lchild);
        CreateBiTree(BT->rchild);
    }
    return OK;
}

//先序遍历二叉树的递归算法
void PreOrder(BiTree BT) {

    if (BT != NULL) {
        printf("%c", BT->data);//访问跟结点
        PreOrder(BT->lchild);//先序递归遍历BT的左子树
        PreOrder(BT->rchild);//先序递归遍历BT的右子树
    }
}

//先序遍历二叉树的非递归算法
void NRPreOrder(BiTree BT) {
    BiTree stack[MAX_TREE_SIZE], p;
    int top;
    if (BT != NULL) {
        top = 1;
        stack[top] = BT;
        while (top > 0) {
            p = stack[top];
            top--;
            printf("%c", p->data);
            if (p->rchild != NULL) {
                top++;
                stack[top] = p->rchild;
            }
            if (p->lchild != NULL) {
                top++;
                stack[top] = p->lchild;
            }
        }
    }
}

//中序遍历二叉树的递归算法
void InOrder(BiTree BT) {
    if (BT != NULL) {
        InOrder(BT->lchild);//中序递归遍历BT的左子树
        printf("%c", BT->data);//访问跟结点
        InOrder(BT->rchild);//中序递归遍历BT的右子树
    }
}

//中序遍历二叉树的非递归算法
void NRInOrder(BiTree BT) {
    BiTree stack[MAX_TREE_SIZE], p;
    int top = 0;
    p = BT;
    do {
        while (p != NULL) {
            top++;
            stack[top] = p;
            p = p->lchild;
        }
        if (top > 0) {
            p = stack[top];
            top--;
            printf("%c", p->data);
            p = p->rchild;
        }
    } while (p != NULL || top > 0);
}

//后序遍历二叉树的递归算法
void PostOrder(BiTree BT) {
    if (BT != NULL) {
        PostOrder(BT->lchild);//后序递归遍历BT的左子树
        PostOrder(BT->rchild);//后序递归遍历BT的右子树
        printf("%c", BT->data);//访问跟结点

    }
}

//后序遍历二叉树的非递归算法
void NRPostOrder(BiTree BT) {
    BiTree stack[MAX_TREE_SIZE], p;
    int tag[MAX_TREE_SIZE];
    int top = 0;
    p = BT;
    do {
        while (p != NULL) {
            top++;
            stack[top] = p;
            p = p->lchild;
            tag[top] = 0;
        }
        if (top > 0) {
            if (tag[top] == 1) {
                printf("%c", stack[top]->data);
                top--;
            } else {
                p = stack[top];
                if (top > 0) {
                    p = p->rchild;
                    tag[top] = 1;
                }
            }
        }
    } while (p != NULL || top > 0);
}

//int main() {
//    BiTree Tree;
//    printf("先序遍历创建二叉树:");
//    CreateBiTree(Tree);
//    printf("先序遍历二叉树的递归算法:\n");
//    PreOrder(Tree);
//    printf("\n");
//    printf("中序遍历二叉树的递归算法:\n");
//    InOrder(Tree);
//    printf("\n");
//    printf("后序遍历二叉树的递归算法:\n");
//    PostOrder(Tree);
//    printf("\n");
//    printf("先序遍历二叉树的非递归算法:\n");
//    NRPreOrder(Tree);
//    printf("\n");
//    printf("中序遍历二叉树的非递归算法:\n");
//    NRInOrder(Tree);
//    printf("\n");
//    printf("后序遍历二叉树的递归算法:\n");
//    NRPostOrder(Tree);
//    printf("\n");
//    return 0;
//
//}
//统计度数为0的节点个数
void CountLeaves0(BiTree BT, int &count0) {
    if (BT) {
        if (!BT->lchild && !BT->rchild) count0++;//左右子树均为空,叶节点个数加1
        CountLeaves0(BT->lchild, count0);
        CountLeaves0(BT->rchild, count0);
    }
}

//统计度数为1的节点个数
void CountLeaves1(BiTree BT, int &count1) {
    if (BT) {
        if ((!BT->lchild && BT->rchild) || (BT->lchild && !BT->rchild)) count1++;//,右子树为空或左子树为空,叶节点个数加1
        CountLeaves1(BT->lchild, count1);
        CountLeaves1(BT->rchild, count1);
    }
}

//统计度数为2的节点个数
void CountLeaves2(BiTree BT, int &count2) {
    if (BT) {
        if (BT->lchild && BT->rchild) count2++;//左右子树均不为空,叶节点个数加1
        CountLeaves2(BT->lchild, count2);
        CountLeaves2(BT->rchild, count2);
    }
}

//统计所有节点个数
void CountLeaves(BiTree BT, int &count) {
    if (BT) {
        count++;//叶节点个数加1
        CountLeaves(BT->lchild, count);
        CountLeaves(BT->rchild, count);

    }
}

//统计二叉树高度
int BiTreeDepth(BiTree BT) {
    int lchilddep, rchilddep;
    if (!BT) return 0;
    else {
        lchilddep = BiTreeDepth(BT->lchild); //求左子树的高度为lchilddep
        rchilddep = BiTreeDepth(BT->rchild); //求右子树的高度为rchilddep
        return (lchilddep > rchilddep) ? (lchilddep + 1) : (rchilddep + 1);
    }
}

int main() {
    BiTree BT;
    int count = 0, count0 = 0, count1 = 0, count2 = 0;
    //ABC##DE#G##F###
    printf("请按照先序顺序输入二叉树(例如:ABC##DE#G##F###):");
    CreateBiTree(BT);
//    InOrder(BT);
//    NRPreOrder(BT);
//    NRInOrder(BT);
//    NRPostOrder(BT);
//    CountLeaves0(BT,count0);
//    CountLeaves1(BT,count1);
//    CountLeaves2(BT,count2);
//    CountLeaves(BT,count);
//    BiTreeDepth(BT);
    int n = 100;
    while (n != 0) {
        printf("选择操作:\n");
        printf(" 1,先序遍历实现\n");
        printf(" 2,中序遍历实现\n");
        printf(" 3,后序遍历实现\n");
        printf(" 4,统计度个数为0的节点个数\n");
        printf(" 5,统计度个数为1的节点个数\n");
        printf(" 6,统计度个数为2的节点个数\n");
        printf(" 7,统计节点个数\n");
        printf(" 8,求二叉树高度\n");
        printf("输入0,退出操作\n");
        printf("请输入数字1-8:");
        scanf("%d", &n);
        switch (n) {
            case 1:
                printf("先序遍历递归实现:");
                PreOrder(BT);
                printf("\n");
                printf("先序遍历非递归实现:");
                NRPreOrder(BT);
                printf("\n");
                break;
            case 2:
                printf("中序遍历递归实现:");
                InOrder(BT);
                printf("\n");
                printf("中序遍历非递归实现:");
                NRInOrder(BT);
                printf("\n");
                break;
            case 3:
                printf("后序遍历递归实现:");
                PostOrder(BT);
                printf("\n");
                printf("后序遍历非递归实现:");
                NRPostOrder(BT);
                printf("\n");
                break;
            case 4:
                CountLeaves0(BT, count0);
                printf("度个数为0的节点个数:%d\n", count0);
                break;
            case 5:
                CountLeaves1(BT, count1);
                printf("度个数为1的节点个数:%d\n", count1);
                break;
            case 6:
                CountLeaves2(BT, count2);
                printf("度个数为2的节点个数:%d\n", count2);
                break;
            case 7:
                CountLeaves(BT, count);
                printf("节点个数:%d\n", count);
                break;
            case 8:
                printf("二叉树高度:%d\n", BiTreeDepth(BT));
                break;
            case 0:
                printf("退出成功。\n");
                break;
            default:
                printf("error\n");
                break;
        }
    }
    return 0;
}




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值