跪求《二叉树的建立的与遍历》的运行程序
发布网友
发布时间:2022-05-12 05:24
我来回答
共3个回答
热心网友
时间:2023-11-20 09:32
#include "stdio.h"
#include "stdlib.h"
#define maxisize 100
typedef int datatype;
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
}bitree;
bitree *q[maxisize];
int i=0;
bitree *creatree()
{
char ch;
int front,rear;
bitree *root,*s;
root=NULL;
front=1;
rear=0;
printf("please enter the tree ,and the end of sign is #:\n");
ch=getchar();
while(ch!='#')
{
s=NULL;
if(ch!='@')
{
s=(bitree *)malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
q[rear]=s;
if(rear==1)
root=s;
else
{
if(s&&q[front])
if(rear%2==0)
q[front]->lchild=s;
else
q[front]->rchild=s;
if(rear%2==1)
front++;
}
i++;
ch=getchar();
}
return root;
}void INORDER(bitree *t)
{
if(t)
{
INORDER(t->lchild);
printf("%c ",t->data);
INORDER(t->rchild);
}
}
void PREORDER(bitree *t)
{
if(t)
{
printf("%c ",t->data);
PREORDER(t->lchild);
PREORDER(t->rchild);
}
}
void POSTORDER(bitree *t)
{
if(t)
{
POSTORDER(t->lchild);
POSTORDER(t->rchild);
printf("%c ",t->data);
}}
void main()
{
bitree *root;
root=creatree();
printf("前序遍历二叉树:");
PREORDER(root);
printf("\n");
printf("中序遍历二叉树:");
INORDER(root);
printf("\n");
printf("后序遍历二叉树:");
POSTORDER(root);
printf("\n");
}程序的运行结果如下:
热心网友
时间:2023-11-20 09:33
这是我自己写的
#include <stdio.h>
#include <stdlib.h>
typedef struct _btree {
int v;
struct _btree* l;
struct _btree* r;
}**btree, *node;
node Insert(btree r, int v)
{
node t, p, n;
t = (node)malloc(sizeof(struct _btree)) ;
t->v = v;
t->l = t->r = NULL;
p = NULL, n = *r;
while(n) {
p = n;
n = v < n->v ? n->l : n->r;
}
if (p) {
if (v < p->v) {
p->l = t;
} else {
p->r = t;
}
} else {
*r = t;
}
return t;
}
node Create(int* beg, int* end)
{
node root;
root = NULL;
while(beg != end)
Insert(&root, *beg++);
return root;
}
void Prevorder(node root)
{
if(root) {
printf("%d ", root->v);
Prevorder(root->l);
Prevorder(root->r);
}
}
void Inorder(node root)
{
if(root) {
Inorder(root->l);
printf("%d ", root->v);
Inorder(root->r);
}
}
void Postorder(node root)
{
if(root) {
printf("%d ", root->v);
Postorder(root->r);
Postorder(root->l);
}
}
void Destruct(node root)
{
if(root) {
Destruct(root->l);
Destruct(root->r);
free(root);
}
}
int main()
{
int a[] = { 6,1,3,5,4,2 };
node root;
root = Create(a, a + 6);
Prevorder(root); /* 前序 */
putchar('\n');
Inorder(root); /* 中序 */
putchar('\n');
Postorder(root); /* 后序 */
putchar('\n');
Destruct(root);
getchar();
return 0;
}
热心网友
时间:2023-11-20 09:33
# include "stdio.h"
# include "stdlib.h"
# define NULL 0
typedef struct BiNode
{int data;struct BiNode *l_child,*r_child;}BiTree;
int k;BiTree *create(BiTree *pt,int k)
{int ch;BiTree *p1,*p2;
scanf("%d",&ch);
if(ch!=0)
{p2=(BiTree *)malloc(sizeof(BiTree));
p2->l_child=p2->r_child=NULL;
p2->data=ch;
if(k==0) p1=p2;
if(k==1) pt->l_child=p2;
if(k==2) pt->r_child=p2;
printf("%d的左子树为:",ch);
create(p2,1);
printf("%d的右子树为:",ch);
create(p2,2);
}
return(p1);
}void zhongxu (BiTree *H)
{if(H!=NULL)
{zhongxu(H->l_child);
printf("%d",H->data);
zhongxu(H->r_child);
}
}
void main()
{BiTree *root,*p;
printf("输入根结点");
p=create(root,0);
zhongxu(p);
}