c语言数据结构 递归创建二叉树的函数如何输入退出?这个函数一直让输入 无论输入什么 都无法结束输入

作者&投稿:仲泄 (若有异议请与网页底部的电邮联系)
数据结构(C语言版) 建立二叉树数据怎么输入?~

typedef struct BiTNode{............} BiTNode,*BiTree;BiTree CreateBiTree(){ BiTree T;char ch;scanf("%c",&ch);if(ch==' ')return (NULL); else{ if(!(T=( BiTNode*)malloc(sizeof(BiTNode))))return 0; T->data=ch; //生成根结点 T->lchild= CreateBiTree(); //构造左子树 T->rchild=CreateBiTree(); //构造右子树。因为前面定义具有BiTNode相同类型的二叉树,所以把递归的值付给右孩子 return (T); }}

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
typedef char TElemType;
typedef struct Btn{
TElemType data;
struct Btn *lchild , *rchild;
}BTN ,*BT;
//已经跟你说过了,不能返回局部指针!而且你的原程序中CreateBT函数的参数和调用时不一致!
void CreateBT(BT &T)//为了方便,这里我用了引用类型
{
char ch;
ch=getchar();
if(ch!='/'){ //当输入"/"则表示数据域为空不执行,否则执行
T=(BT)malloc(sizeof(BTN));//为结点分配空间
if(T){
T->data=ch;
CreateBT(T->lchild);
CreateBT(T->rchild);
}else{
printf("分配空间失败!");
exit(0);
}
}else{
T=NULL;//将T置为空
}

}
void PreOrderT(BT T )
{
if(T)
{
printf("%c",T->data);
PreOrderT(T->lchild);
PreOrderT(T->rchild) ;
}
}

void InOrderT(BT T)
{
if(T)
{
PreOrderT(T->lchild);
printf("%c",T->data);
PreOrderT(T->rchild) ;
}
}

void PostOrderT(BT T)
{
if(T)
{
PreOrderT(T->lchild);
PreOrderT(T->rchild) ;
printf("%c",T->data);
}
}

BT LocateElem(BT T , TElemType e)
{
BT p;
if(T==NULL)
return NULL;
else
if(T->data==e)return T;
else
{
p=LocateElem(T->lchild,e);
if(p) return p;
else return LocateElem(T->rchild,e);
}
}

int BTnum(BT T)
{
if(T==NULL)return 0;
else return(BTnum(T->lchild)+BTnum(T->rchild));
}

int BTdepth(BT T)
{
int h,lh,rh;
if(T==NULL)h=0;
else
{
lh=BTdepth(T->lchild);
rh=BTdepth(T->rchild);
if(lh>=rh)h=lh+1;
else h=rh+1;
}
return h;
}

void main()
{
BT T;
int i,flag=1;
char ch;
do{
printf("请输入你想要执行的操作:
");
printf("****** 1 按前序遍历创建一个二叉树
");
printf("****** 2 前序遍历二叉树
");
printf("****** 3 中序遍历二叉树
");
printf("****** 4 后续遍历二叉树
");
printf("****** 5 查找元e
");
printf("****** 6 统计结点
");
printf("****** 7 计算深度
");
printf("****** 8 退出程序
");
scanf("%d",&i);
getchar();
switch(i)
{
case 1: printf("请输入元素:");
CreateBT(T);
break;//case之后要用break跳出循环,有点粗心了!下面都是
case 2: PreOrderT(T);break;
case 3: InOrderT(T);break;
case 4: PostOrderT(T);break;
case 5: printf("请输入你要查找的元素:");
scanf("%c",&ch);
LocateElem(T,ch);
break;
case 6: BTnum(T);break;
case 7: BTdepth(T);break;
case 8: printf("程序退出:");
exit(0);
}
printf("是否继续操作?(y/n):");
scanf("%c",&ch);//这里的变量应该是字符类型,你的原程序中用的是整型!
getchar();
}while(ch!='y');
}
二叉树创建函数我已经验证了,没什么问题,其它的函数都没动。

递归创建二叉树的输入是有讲究的,可参考:网页链接中最后的输入示例:如果你用#作为结束,则对应输入:1 2 4 # 6 ###3 #5 #7 #8 ##

再给个递归创建二叉树的例子:

#include <stdio.h>
#include <stdlib.h>

typedef struct Tree {
    int Val;
    struct Tree* left;
    struct Tree* right;
}Tree;

Tree * CreateBiTree(void)
{
    Tree * T;
    int val;

    scanf("%d", &val);
    if(val == 0)
        T = NULL;
    else
    {
        T = (Tree *)malloc(sizeof(Tree));
        T -> Val = val;
        T -> left = CreateBiTree();
        T -> right = CreateBiTree();
    }
    return T;
}

void Print(Tree* root)
{
    if (root != NULL)
    {
        Print(root->left);
        printf("%d ", root->Val);
        Print(root->right);
    }
}

int main()
{
    Tree* root = CreateBiTree();

    Print(root);
    return 0;
}

以上面的输入例子1 2 4 # 6 ###3 #5 #7 #8 ##为例,对应的输入为:1 2 3 0 6 0 0 0 3 0 5 0 7 0 8 0 0

运行结果:

当然也可以这样:

#include <stdio.h>
#include <stdlib.h>

typedef struct Tree {
    int Val;
    struct Tree* left;
    struct Tree* right;
}Tree;

void CreateBiTree(Tree**T)
{
    int val;

    scanf("%d", &val);
    if(val == 0)
        *T = NULL;
    else
    {
        *T = (Tree *)malloc(sizeof(Tree));
        (*T)->Val = val;
        CreateBiTree(&(*T)->left);
        CreateBiTree(&(*T)->right);
    }
}

void Print(Tree* root)
{
    if (root != NULL)
    {
        Print(root->left);
        printf("%d ", root->Val);
        Print(root->right);
    }
}

int main()
{
    Tree* root;

    CreateBiTree(&root);
    Print(root);
    return 0;
}

这里的输入示例为:1 2 4 0 6 0 0 0 7 0 0 0

运行结果:

C++ 版:

#include <iostream>

using namespace std;

typedef struct Tree {
    int Val;
    struct Tree* left;
    struct Tree* right;
}Tree;

void CreateBiTree(Tree* & T)
{
    int val;

    cin >> val;
    if(val == 0)
        T = NULL;
    else
    {
        T = new Tree;
        T->Val = val;
        CreateBiTree(T->left);
        CreateBiTree(T->right);
    }
}

void Print(Tree*root)
{
    if (root != NULL)
    {
        Print(root->left);
        cout << root->Val << ' ';
        Print(root->right);
    }
}

int main()
{
    Tree* root;

    CreateBiTree(root);
    Print(root);
    return 0;
}

看了一下你的递归部分的代码,是正确的,没问题。



输入#号键结束


房县18579035853: 请问C语言如何创建二叉树???? -
乜贴八味: 创建二叉树的源程序如下: #include <cstdlib>#include <stdio.h>typedef struct node{ //树的结点int data;struct node* left;struct node* right;} Node;typedef struct{ //树根Node* root;} Tree;void insert(Tree* tree, int value)//创建树{Node* ...

房县18579035853: 求C语言题 二叉树递归算法的建立 -
乜贴八味: void CreateBinTree(BinTree *T) //按前序建立二叉树,数据按前序输入 { char ch; if((ch=getchar())==' ') *T=NULL; else{ *T=(BinTNode*)malloc(sizeof(BinTNode)); (*T)->data=ch; CreateBinTree(&(*T)->lchild); CreateBinTree(&(*T)->rchild); } } 你可以参考一下:http://wenwen.sogou.com/z/q787466401.htm

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

房县18579035853: 递归创建二叉树的程序是怎么执行的如下 -
乜贴八味: 递归只是一种形式,本质还是调用函数,所你你不要一直想着它是递归,就认为建立二叉树的函数是函数A,建立左子树是函数B,建立右子树是函数C,只不过B和C的内容和A一样罢了.那函数内的代码怎么执行?就是自上而下一步一步按顺序执行咯,执行完了建立左子树的函数就会执行建立右子树的函数了,只不过这些函数里面还要再调用函数.对于函数的调用,你就当是把函数里的代码全部嵌入到调用它的地方就好了,然后还是自上而下顺序执行.具体过程你看下面的图好了,由于内容比较多,我把if给省略了,就只写了执行到的部分.

房县18579035853: 求数据结构(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 ...

房县18579035853: c语言数据结构:怎么建立一个二叉树? -
乜贴八味: 只要将一个二叉树用“括号表示法”表示出来,然后,用链式存储结构将其各个结点存储就可以了,也就是输入一个二叉树.最后,用中序遍历输出! typedef struct node{ ElemType data;struct node *lchild,*rchild;} BTNode; //创建一个二叉树...

房县18579035853: 数据结构二叉树的建立 -
乜贴八味: /* 非递归法遍历二叉树C语言 */ #include "stdio.h" #include "stdlib.h"typedef char Datatype; typedef struct{int flag;PBinTree p; } typedef struct BinTNode{Datatype data;struct BinTNode *lchild, *rchild; }BinTree,*PBinTree;typedef struct ...

房县18579035853: 用c语言编,求一个二叉树用递归的方法来求前序遍历、中序遍历、后序遍历以及求它的叶子结点与树的深度的一个程序.万分感谢! -
乜贴八味: #include //建立二叉树 typedef struct bitnode{ char data; struct bitnode *lchild; struct bitnode *rchild; }bitnode,*bitree; //初始化二叉树 void createbitree(bitree &t){ char ch; scanf("%c",&ch); if(ch==' ') t=NULL; else{ t=(bitnode *)malloc(sizeof(bitnode...

房县18579035853: 数据结构递归构建二叉树C语言 -
乜贴八味: typedef struct BiTNode{ char data; struct BiTNode * lchild,* rchild; }BiTNode,* BiTree; int CreatBiTree(BiTree &T) { char ch; scanf("%c",ch); if(ch=='#')T=NULL; else { if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))exit(OVERFLOW); T->data =ch; ...

房县18579035853: 二叉树先序递归算法(C语言)
乜贴八味: 因为在定义树时,里面包含了树的指针,那么创建用递归的时候,传递的都是指针的地址,所以就要用指向指针的指针来引用这个地址.不然就会断开与上一层的联系.**t是定义指向指针的指针,用到*t就像你定义指针*t时,使用t一样.既然用了指针,那么在使用函数的时候当然就要用取址符号&取地址进行传递.算法思想...其实就是比较.分为4种情况,第一种是插入节点比根节点小,所以插入节点要成为根节点.第二种是插入节点要放在左节点,因为没有线索化,所以用到返回,返回到先前节点然后再插入、移动.第三种是插入节点要放在右节点,和左节点差不多,只是在插入的时候用到地址不一样,所以分开了.第四种就是插入的节点比原有的节点都大那么就要插入到最后一个节点.

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