php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 508|回复: 0

c语言二叉树遍历代码

[复制链接]

2645

主题

2652

帖子

9404

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6637
贡献
0
注册时间
2021-4-14
最后登录
2024-5-3
在线时间
670 小时
QQ
发表于 2022-3-31 16:24:17 | 显示全部楼层 |阅读模式
[mw_shl_code=c,true]/* Binary tree traversal */
/* BT_TRAV.C */

# include<stdio.h>

struct NODE
{
        char Info;
        struct NODE *Left_Child;
        struct NODE *Right_Child;
};

struct NODE *Binary_Tree (char *, int, int);
void Output(struct NODE *, int );
void Pre_order(struct NODE *);
void In_order(struct NODE *);
void Post_order(struct NODE *);

/* Function to create an binary tree */

struct NODE *  Binary_Tree (char *List, int Lower, int Upper)
{
        struct NODE *Node;
        int Mid = (Lower + Upper)/2;
        Node = (struct NODE*) malloc(sizeof(struct NODE));

        Node->Info = List [Mid];
        if ( Lower>= Upper)
        {
                Node->Left_Child = NULL;
                Node->Right_Child = NULL;
                return (Node);
        }

        if (Lower <= Mid - 1)
                Node->Left_Child = Binary_Tree (List, Lower, Mid - 1);
        else
                Node->Left_Child = NULL;
        if (Mid + 1 <= Upper)
                Node->Right_Child = Binary_Tree (List, Mid + 1, Upper);
        else
                Node->Right_Child = NULL;
        return(Node);
}

/* Output function */

void Output(struct NODE *T, int Level)
{
        int i;
        if (T)
        {
                Output(T->Right_Child, Level+1);
                printf("\n");
                for (i = 0; i < Level; i++)
                        printf("   ");
                printf(" %c", T->Info);
                Output(T->Left_Child, Level+1);
        }
}

/* Pre-order traversal */

void Pre_order (struct NODE *Node)
{
        if (Node)
        {
                printf(" %c", Node->Info);
                Pre_order(Node->Left_Child);
                Pre_order(Node->Right_Child);
        }
}

/* In-order traversal */

void In_order (struct NODE *Node)
{
        if (Node)
        {
                In_order(Node->Left_Child);
                printf(" %c", Node->Info);
                In_order(Node->Right_Child);
        }
}

/* Post-order traversal */

void Post_order (struct NODE *Node)
{
        if (Node)
        {
                Post_order(Node->Left_Child);
                Post_order(Node->Right_Child);
                printf(" %c", Node->Info);
        }
}

/*  Function main */

void main()
{
        char List[100];
        int Number = 0;
        char Info;
        char choice;
        struct NODE *T = (struct NODE *) malloc(sizeof(struct NODE));
        T = NULL;
        printf("\n Input choice 'b' to break:");
        choice = getchar();
        fflush(stdin);
        while(choice != 'b')
        {
                printf("\n Input information of the node: ");
                scanf("%c", &Info);
                List[Number++] = Info;
                fflush(stdin);
                printf("\n Input choice 'b' to break:");
                choice = getchar();
                fflush(stdin);
        }
        Number --;
        printf("\n Number of elements in the list is %d", Number);
        T = Binary_Tree(List, 0, Number);
        Output(T,1);
        printf("\n Pre-order traversal\n");
        Pre_order (T);
        printf("\n In-order traversal\n");
        In_order (T);
        printf("\n Post-order traversal\n");
        Post_order (T);
}

.[/mw_shl_code]





上一篇:如何在 Laravel 中使用Update Query
下一篇:c++类确定windows版本
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-5-5 10:55 , Processed in 0.185478 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表