当前位置:首页 » 编程语言 » 如何用c语言录入二叉树
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

如何用c语言录入二叉树

发布时间: 2022-08-31 16:46:39

A. 如何用c语言建立二叉树

代码不全,怎么帮你调试呢? 请提供完整代码

B. 数据结构(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);
}}

C. C语言 二叉树的建立

从键盘输入字符,然后回车,字符会停留在缓冲区内,之后你每次scanf("%c", &ch)就会从缓冲区取出一个来

D. 请问如何用c语言实现二叉树的按层录入

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 1024

typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}BTnode;

static char a[1024];

void create(BTnode *&b,int m,int i)
{
if(i<m&&a[i]!=' ')
{
b=(BTnode*)malloc(sizeof(BTnode));
b->data=a[i];
b->lchild=b->rchild=NULL;
create(b->lchild,m,2*i);
create(b->rchild,m,2*i+1);
}
}

void print(BTnode *&b)
{
BTnode *p=b;
if(p!=NULL)
{
printf("%c",p->data);
if(p->lchild!=NULL||p->rchild!=NULL)
{
printf("(");
print(b->lchild);
if(p->rchild!=NULL)
{
printf(",");
print(b->rchild);
}
printf(")");
}
}
}

int main()
{
BTnode *b;
int j,m=1;
char s[1024],*p;
printf("以字符串形式输入第一层节点(根节点):\n");
gets(s);
while(strlen(s)!=0)
{
j=m;
p=s;
while(m<2*j)
{
a[m]=*p;
p++;
m++;
}
printf("以字符串形式输入下一层节点(空节点以空格表示):\n");
gets(s);
}
create(b,m,1);
printf("以二叉链表表示形式输出二叉树:\n");
print(b);
printf("\n\n");
system("pause");
}

E. 二叉树c语言实现

#include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char data;
struct node *lchild,*rchild;//
}BiTNode,*BiTree;
void CreatBiTree(BiTree &T)
{
char ch;
ch=getchar();
if (ch == ' ')
T = 0;
else {
T=(BiTNode*)malloc(sizeof(BiTNode));
T->data=ch;//生成根节点
CreatBiTree(T->lchild);//构造左子树
CreatBiTree(T->rchild);//构造右子树
}
}
void preorder(BiTree T)//前序遍历
{
if (T!=NULL){
printf ("%c",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
void inorder(BiTree T)//中序遍历
{
if (T!=NULL){
inorder(T->lchild);
printf ("%c",T->data);
inorder(T->rchild);
}
}
void postorder(BiTree T)//后序遍历
{
if (T!=NULL){
postorder(T->lchild);
postorder(T->rchild);
printf ("%c",T->data);
}
}
void main ()
{
cout<<"请输入要创建的二叉树包括空格:"<<endl ;
BiTree T;
CreatBiTree(T);//创建二叉树
cout<<"前序遍历的结果为:"<<endl;
preorder(T);
cout<<endl;
cout<<"中序遍历的结果为:"<<endl;
inorder(T);
cout<<endl;
cout<<"后序遍历的结果为:"<<endl;
postorder(T);
}

F. 二叉树怎样用C语言实现

以下代码可实现二叉树的递归创建与中序遍历,但在输入时需在输入完有效数据后再连续输入多个负数才可以。
#include <stdio.h>
#include <stdlib.h>
typedef struct BitNode
{
int data;
struct BitNode *lchile,*rchild;
}*BiTree;
BiTree CreateBiTree(BiTree &T)
{
int d;
scanf("%d",&d);
if (d<0)
return NULL;
else
{
if (!(T=(BiTree)malloc(sizeof(BiTree))))
{
exit(1);
}
T->data=d;//先根序创建二叉树
printf("%d ",T->data);
T->lchile=CreateBiTree(T->lchile);//创建左子树
T->rchild=CreateBiTree(T->rchild);//创建右子树
return T;
}
}
void InOrderView(BiTree &T)//中序遍历二叉树
{
if(T)
{
InOrderView(T->lchile);
printf("%d ",T->data);
InOrderView(T->rchild);
}
}
void main()
{
BiTree T;
T=CreateBiTree(T);//递归创建二叉树
InOrderView(T);
}

G. C语言先序建立二叉树(如何结束输入)

As you stated, the only way is to enter SPACE for each leaf node.
Otherwise you have to modify the code

H. 如何用C语言创建二叉树

#include<stdio.h>
typedef char TElemType;
typedef struct BiTNode /*结点定义*/
{
TElemType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;

BiTNode *CreateBiTree()
{
char c;
BiTNode *Root;
scanf("%c",&c);
if(c=='@') Root=NULL;
else{
Root=(BiTNode *)malloc(sizeof(BiTNode));
Root->data=c;
Root->lchild=CreateBiTree();
Root->rchild=CreateBiTree();
}
return(Root);
}

void PrintTree(BiTree T,int h)
{
int i;
if (T){
PrintTree(T->rchild,h+1);
for(i=0;i<h;i++) printf(" ");
printf("%c\n",T->data);
PrintTree(T->lchild,h+1);
}
}

main( )
{
BiTNode* Root;
Root=CreateBiTree();
PrintTree(Root,0);
return 0;
}