数据结构--二叉树

来源:互联网 发布:阿里云百度云加速 编辑:程序博客网 时间:2024/05/04 11:28
#include <stdio.h>#include <stdlib.h>typedef struct tree{    char data;    struct tree * lc, * rc;}bit;void creat(bit * & );void inorder(bit * );//先序遍历int depth(bit * );int count(bit * );int main(void){    bit * root;    creat(root);    inorder(root);    printf("\n%d\n", depth(root));    printf("%d\n", count(root));    return 0;}int count(bit * node){    if (node == NULL)        return 0;    else        return count(node->lc) + count(node->rc) + 1;}int depth(bit * node){    int m, n;    if (node == NULL)        return 0;    else    {        m = depth(node->lc);        n = depth(node->rc);        if (m > n)            return m + 1;        else             return n + 1;    }}void inorder(bit * node){    if (node)    {        printf("%c ", node->data);        inorder(node->lc);        inorder(node->rc);    }}void creat(bit * &node){    char ch;    scanf("%c", &ch);    if (ch == '#')        node = NULL;    else    {        node = (bit * )malloc(sizeof(bit));        node->data = ch;        creat(node->lc);        creat(node->rc);    }}
0 0
原创粉丝点击