求C语言版数据结构二叉树的先序遍历递归算法,不要伪码,要求能实现能运行的。谢过各位大佬了!

作者&投稿:抄沿 (若有异议请与网页底部的电邮联系)
求用C语言实现二叉树层次遍历的递归算法,谢谢!!!~

#include
"stdio.h"
#include
"stdlib.h"
#include
"string.h"
#define
null
0
struct
node
{
char
data;
struct
node
*lchild;
struct
node
*rchild;
};
//先序,中序
建树
struct
node
*create(char
*pre,char
*ord,int
n)
{
struct
node
*
head;
int
ordsit;
head=null;
if(n<=0)
{
return
null;
}
else
{
head=(struct
node
*)malloc(sizeof(struct
node));
head->data=*pre;
head->lchild=head->rchild=null;
ordsit=0;
while(ord[ordsit]!=*pre)
{
ordsit++;
}
head->lchild=create(pre+1,ord,ordsit);
head->rchild=create
(pre+ordsit+1,ord+ordsit+1,n-ordsit-1);
return
head;
}
}
//中序递归遍历
void
inorder(struct
node
*head)
{
if(!head)
return;
else
{
inorder(head->lchild
);
printf("%c",head->data
);
inorder(head->rchild
);
}
}
//中序非递归遍历
void
inorder1(struct
node
*head)
{
struct
node
*p;
struct
node
*stack[20];
int
top=0;
p=head;
while(p||top!=0)
{
while
(p)
{
stack[top++]=p;
p=p->lchild
;
}
p=stack[--top];
printf("%c
",p->data
);
p=p->rchild
;
}
}
//主函数
int
main()
{
struct
node
*
head;
char
pre[30],ord[30];
int
n;
gets(pre);
gets(ord);
n=strlen(pre);
head=create(pre,ord,n);
inorder(head);
printf("
");
inorder1(head);
printf("
");
}
//测试事例;
/*
-+a*b%cd/ef
a+b*c%d-e/f
*/
几个月前自己编写,原版
vc++
6.0实验通过
怎么样,老板,第一次上百度知道,好激动
给点面子
给分!给分啊

#define LEN sizeof(struct tree)
#define NULL 0
#include
#include
struct tree
{
char data;
struct tree *lchild,*rchild;
};
//创建二叉树
struct tree *creat()
{
char c;
struct tree *t;
c=getchar();
if(c==' ')
t=NULL;
else
{
t=(struct tree*)malloc(LEN);
t->data=c;
t->lchild=creat();
t->rchild=creat();
}
return t;
}
//前序遍历
void Preprint(struct tree*t)
{
if(t!=NULL)
{
printf("%c->",t->data);
Preprint(t->lchild);
Preprint(t->rchild);
}
}
//中序遍历
void Inprint(struct tree*t)
{
if(t!=NULL)
{
Inprint(t->lchild);
printf("%c->",t->data);
Inprint(t->rchild);
}
}
//后序遍历
void Postprint(struct tree*t)
{
if(t!=NULL)
{
Postprint(t->lchild);
Postprint(t->rchild);
printf("%c->",t->data);
}
}
main()
{
struct tree *t;
printf("Please input tree in order:
");
t=creat();
printf("The result of Preorder traversal is
");
Preprint(t);
printf("^
The result of Inorder traversal is
");
Inprint(t);
printf("^
The result of Postorder traversal is
");
Postprint(t);
printf("^
");
getch();
}

K&R中的一个实现,可以读取数字,插入二叉树,并且统计出现次数。最后输出,这里假设只读取正数,自己可以改getword函数

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
 
#define MAXLINE 100
 
struct num {
    int number;
    int count;
    struct num *left;
    struct num *right;
} ;
 
struct num *addtree(struct num *, char w[]);
void treeprint(struct num *);
int getword(char w[], int lim);
 
int main(void)
{
    struct num *root;
    char word[MAXLINE];
 
    root = NULL;
    while (getword(word, MAXLINE) != EOF)
        if (isdigit(word[0]))
            root = addtree(root, word);
    treeprint(root);
 
    return 0;
}
 
int getword(char *word, int lim)
{
    int c;
    int getch();
    void ungetch();
    char *w = word;
 
    while (isspace(c = getch()))
        ;
    if (c != EOF)
        *w++ = c;
    if (!isdigit(c)) {
        *w = '\0';
        return c;
    }
    for (; --lim > 0; w++) {
        if (!isdigit(c = *w = getch())) {
            ungetch(c);
            break;
        }
    }
    *w = '\0';
    return word[0];
}
 
struct num *talloc(void);
struct num *addtree(struct num *p, char *w)
{
    int cond;
 
    cond = atoi(w);
   // printf("---%d---
", cond);
    if (p == NULL) {
        p = talloc();
        p->number = cond;
        p->count = 1;
        p->left = p->right = NULL;
    } else if (cond == p->number) 
        p->count++;
    else if (cond < p->number)
        p->left = addtree(p->left, w);
    else
        p->right = addtree(p->right, w);
    return p;
}
 
void treeprint(struct num *p)
{
    if (p != NULL) {
        treeprint(p->left);
        printf("%dhas:%d
", p->number, p->count);
        treeprint(p->right);
    }
}
 
struct num *talloc(void)
{
    return (struct num *) malloc(sizeof(struct num));
}
 
#define BUFSIZE 100
int bufp = 0;
char bufline[BUFSIZE];
int getch(void)
{
    return (bufp > 0) ? bufline[--bufp] : getchar();
}
void ungetch(int c)
{
    if (bufp < BUFSIZE)
        bufline[bufp++] = c;
    else
        printf("error : full
");
}





数据结构(C语言版)---二叉树
数据结构:二叉树详解二叉树是一种特殊的树形数据结构,每个节点最多有两个子节点,且子节点的位置固定。它分为几种类型:一般二叉树:常规二叉树结构,没有特定的满度要求。满二叉树:在不增层数前提下,无法添加更多节点的特殊二叉树。完全二叉树:删除了满二叉树底层右端节点后的结构,底层剩余节点...

哪位大佬有 数据结构(C语言版),有人帮我找找这资源嘛?谢谢啦
https:\/\/pan.baidu.com\/s\/1NgzMZD3ZBEDJDmj2g7x9cw 提取密码:1234 《数据结构(C语言版)\/清华大学计算机系列教材》是为“数据结构”课程编写的教材,也可作为学习数据结构及其算法的c程序设计的参考教材。《数据结构(C语言版)\/清华大学计算机系列教材》的前半部分从抽象数据类型的角度讨论各种基本...

数据结构要点精析:C语言版图书信息
以下是对《数据结构要点精析:C语言版》这本书的详细描述:该书由北京航空航天大学出版社出版,特别版是在2009年3月1日发行的。作为第二版,它为读者提供了全面的内容。全书共361页,适合深度学习和理解数据结构的理论与实践。语言版本采用的是简体中文,便于中国读者理解和掌握,符合国内学术交流的需要。

数据结构(c语言版)
测试数据1:创建二叉树,输入先序扩展序列:ABD##E##C#F##先序遍历输出节点:A B D E C F中序遍历输出节点:D B E A C F后序遍历输出节点:D E B F C A二叉树示意图: A \/ \\ B C \/ \\ \/ \\ D E # F \/ \\ \/ \\ \/ \\ # # # ...

数据结构C语言版2.12,词典次序的问题。
void compare_dic(char a[],char b[]) \/\/ error:expected ';', ',' or ')' before '&' token| { int n;printf("%c %c",a[0],b[0]);if(a[0]==b[0]){ \/\/先把0考虑下 for(n=1;a[n]==b[n]&&(a[n-1]==b[n-1]);n++); \/\/找出共同前缀 if(a[n]...

严蔚敏《数据结构》(C语言版|第二版)中,关于栈中的一个定义SElemType的...
selemtype肯定指的是栈元素类型,也就是说这只是一个类型说明符,指代的是可以是任意类型

数据结构(C语言版)图书目录
以下是改写后的文章内容,以HTML格式呈现:数据结构(C语言版)图书目录 第1章 数据结构概述 1.1 数据结构研究的问题1.1.1 计算机解决实际问题的一般步骤1.1.2 数据结构学科概念及其研究内容1.1.3 数据结构建模举例 1.2 有关概念1.2.1 数据相关概念1.2.2 数据结构术语1.2.3 数据类型概...

数据结构1000个问题与解答-C语言版目录
数据结构1000个问题与解答-C语言版目录,涵盖了丰富的C语言数据结构内容,从基础的数组操作到高级的矩阵运算,带你逐步掌握。第1章:数组 1.1 初始化数组:可通过声明时指定、循环填充、使用其他数组值或特殊值的方式实现。1.2 遍历数组:一维数组可通过下标或指针,二维数组则需要更细致的处理,指针...

怎么做?数据结构的(C语言版)
{ int d;struct Data *next;};\/\/创建链表 struct Data* create(){ int i,n;struct Data *h,*p,*q;printf("请输入链表节点的数量:");scanf("%d",&n);if(n>0){ h=q=(struct Data*)malloc(sizeof(struct Data));printf("请输入第1个链表节点的数据(整数):");scanf("%d",&(...

清华大学计算机系列教材•数据结构(C语言版)图书信息
清华大学出版社于2009年3月1日推出了《清华大学计算机系列教材•数据结构(C语言版)》一书,由吴伟民和严蔚敏两位作者共同编撰。这本书以其详实的内容和专业性,为读者提供了深入理解数据结构和C语言编程的宝贵资源。该书共334页,采用16开本设计,简洁易读。全书使用简体中文,便于广大中国读者理解和...

湄潭县18691658739: 二叉树的先序遍历算法 - -----将其用c语言编写程序 -
凭闹怡方: void preorder(BiTree T) { if(p!=NULL){printf("%c",T->data);preorder(T->lchild);preorder(T->rchild);} }

湄潭县18691658739: 求c语言数据结构二叉树的建树,前序遍历,输出树的代码,能用采纳. -
凭闹怡方: #include#include#define MAXSIZE 100 //二叉树中最多的结点数 typedef char TElemType; typedef struct BiTNode { TElemType data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree;//定义函数指针 typedef void(* Visit)(BiTree);//二叉树的初始化 ...

湄潭县18691658739: 用c语言编程实现二叉树的建立和遍历二叉树? -
凭闹怡方: //这是我上数据结构写的 建议理解为主#include#include#define ERROR 0#define OK 1#define OVERFLOW -2#define FLASE 0#define TURE 1 typedef int Status; typedef char TElemType; typedef struct BiTNode{ TElemType data; struct BiTNode ...

湄潭县18691658739: 数据结构,C语言,如何利用递归先序遍历二叉树 ,算法已经给出,不知道原理 -
凭闹怡方: 从整棵树的根节点出发,执行函数,访问根节点的值,然后递归调用,访问左子树,执行函数,访问左子树根节点的值,再递归调用,一直到叶子节点,不满足root!=NULL的条件,然后一层一层地返回,递归执行右子树遍历,就得到了先序遍历

湄潭县18691658739: 求二叉树遍历算法C语言实现的?
凭闹怡方: 下面是c语言的前序遍历二叉树的算法,在这里假设的节点元素值假设的为字符型, 说明:算法中用到了结构体,也用到了递归的方法,你看看怎么样,祝你好运! #include"stdio.h" typedef char ElemType; typedef struct node //定义链表结构 ...

湄潭县18691658739: 用C实现输入二叉树的前序和中序遍历用C实现:输入二叉树的前序和中
凭闹怡方: /* ds7_1.c: 二叉树的创建、显示、查找、遍历、插入 */ #include "stdio.h" #include "string. h" #define MAXSIZE 50 typedef struct node { char data; struct node *...

湄潭县18691658739: c语言 先序建立二叉树 先序遍历二叉树 -
凭闹怡方: preOrder(BinTree T);函数里面 把if(T) ; 这个分号去了;我靠 ,看了半天;

湄潭县18691658739: 急求C语言写二叉树的遍历 -
凭闹怡方: 下面是一个用递归方法编的二叉树遍历程序,供lz参考. #include <stdio.h>//头文件#include <stdlib.h>#include <malloc.h> typedef struct bitnode { char data; struct bitnode *lchild,*rchild; } bitnode,*bitree;//定义结点类型 bitree createbitree()//创...

湄潭县18691658739: 求数据结构(C语言版)建立二叉树的代码~~急~~谢谢了 -
凭闹怡方: BT.H文件 #include <stdio.h> #include <malloc.h> #include <conio.h> #define TRUE 1 #define FALSE 0 #define ERROR 0 #define OK 1 #define Stack_Size 50 #define NUM 50 #define MAXSIZE 50 //队列的最大长度 //定义二叉树 typedef char ...

湄潭县18691658739: 求一个用C语言写的建立二叉树.并且先序中序后序遍历这个二叉树 -
凭闹怡方: 其实这个程序很简单的. 代码如下:#include<stdio.h> #include<malloc.h> #define MAX_TREE_SIZE 100 typedef struct {int i; }TElemType; typedef struct BiTNode{char data;struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; int CreateBiTree(...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网