64,691
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<malloc.h>
typedef struct Binnode //二叉树结点结构体
{
char data;
struct Binnode *lchild;
struct Binnode *rchild;
}Binnode;
typedef Binnode *Bintree;
/* 按照前序遍历建立二叉树 */
void Creat_Bintree(Bintree *root)
{
char ch;
while((ch = getchar())!= EOF)
if (ch==' ')
{
*root=NULL;
}
else
{
*root=(Binnode*)malloc(sizeof(Binnode));
(*root)->data=ch;
Creat_Bintree(&(*root)->lchild);
Creat_Bintree(&(*root)->rchild);
}
}
/* 按照前序递归遍历二叉树 */
void Preorder1(Bintree t)
{
if (t!=NULL)
{
printf("%c",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}
int mian(){
Bintree T;
creat(&T);
Preorder(T);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
char data;
struct Node *lchild;
struct Node *rchild;
}Binnode;
typedef Binnode *Treenode;
//按前序遍历创建二叉树
void creat(Treenode *root) {
char ch = getchar();
if (ch == '\n') {
*root = NULL;
}
else {
*root = (Binnode *)malloc(sizeof(Binnode));
(*root)->data = ch;
creat(&(*root)->lchild);
creat(&(*root)->rchild);
}
}
//前序遍历二叉树
void preorder(Treenode T) {
if(T) {
printf("%c\t", T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
//后序遍历二叉树
void posorder(Treenode T) {
if (T){
preorder(T->lchild);
preorder(T->rchild);
printf("%c\t", T->data);
}
}
//中序遍历二叉树
void inorder(Treenode T) {
if(T) {
preorder(T->lchild);
printf("%c\t", T->data);
preorder(T->rchild);
}
}
//交换左子树和右子树
void exchange(Treenode T) {
Treenode temp;
if(!T) {
return ;
}
exchange(T->lchild);
exchange(T->rchild);
temp = T->lchild;
T->lchild = T->rchild;
T->rchild = temp;
}
//叶子节点
int leaves(Treenode T) {
if(T) {
if(T->lchild == NULL && T->rchild == NULL) {
return 1;
}
return leaves(T->lchild) + leaves (T->rchild);
}
return 0;
}
//深度
int depth(Treenode T) {
int lh, rh;
if (!T) {
return 0;
}
else {
lh = depth(T->lchild);
rh = depth(T->rchild);
return (lh>rh?lh:rh) + 1;
}
}
//节点数
int num(Treenode T) {
if(!T) {
return 0;
}
return num(T->lchild) + num(T->rchild) + 1;
}
int main() {
Treenode T;
printf("preceeder create a tree******\n");
creat(&T);
printf("\n");
printf("preceeder search a tree******\n");
preorder(T);
printf("\n");
printf("middle search a tree*********\n");
inorder(T);
printf("\n");
printf("post search a tree***********\n");
posorder(T);
printf("the number of leave is %d\n", leaves(T));
printf("the number of node is %d\n", num(T));
return 0;
}
请问一下 我前序遍历生成数的顺序是12453,但我中序遍历和后序遍历得到的结果是24513和24531.我觉得中序遍历和后序遍历是42513和45231才对呀#include<stdio.h>
#include<malloc.h>
typedef struct Binnode //二叉树结点结构体
{
char data;
struct Binnode *lchild;
struct Binnode *rchild;
}Binnode;
typedef Binnode *Bintree;
/* 按照前序遍历建立二叉树 */
void Creat_Bintree(Bintree *root) {
char ch = getchar();//此次只需要读取一个字符,不能用循环。因为下面递归调用还会走这里
if (ch=='\n') {//我用\n作为节点为空的标志
*root=NULL;
}
else {
*root=(Binnode*)malloc(sizeof(Binnode));
(*root)->data=ch;
Creat_Bintree(&(*root)->lchild);
Creat_Bintree(&(*root)->rchild);
}
}
/* 按照前序递归遍历二叉树 */
void Preorder1(Bintree t) {
if (t!=NULL)
{
printf("%c",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}
int main(){
Bintree T;
Creat_Bintree(&T);
Preorder1(T);
return 0; }
构造一完全二叉树,其层次遍历是12345
124
5
3
12453Press any key to continue
#include<stdio.h>
#include<malloc.h>
typedef struct Binnode //二叉树结点结构体
{
char data;
struct Binnode *lchild;
struct Binnode *rchild;
}Binnode;
typedef Binnode *Bintree;//这里好像也有问题
/* 按照前序遍历建立二叉树 */
void Creat_Bintree(Bintree *root) { //这里应该是 **root
char ch;
while((ch = getchar())!= EOF)
if (ch==' ') {
*root=NULL;
}
else {
*root=(Binnode*)malloc(sizeof(Binnode));
(*root)->data=ch;
Creat_Bintree(&(*root)->lchild);
Creat_Bintree(&(*root)->rchild);
}
}
/* 按照前序递归遍历二叉树 */
void Preorder1(Bintree t) {
if (t!=NULL)
{
printf("%c",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}
int mian(){//这里应该是main
Bintree T;
creat_Bintree(&T);
Preorder1(T);
return 0; }
我也不怎么懂二叉树。你自己看吧#include<stdio.h>
#include<malloc.h>
typedef struct Binnode //二叉树结点结构体
{
char data;
struct Binnode *lchild;
struct Binnode *rchild;
}Binnode;
typedef Binnode *Bintree;
/* 按照前序遍历建立二叉树 */
void Creat_Bintree(Bintree *root) {
char ch;
while((ch = getchar())!= EOF)
if (ch==' ') {
*root=NULL;
}
else {
*root=(Binnode*)malloc(sizeof(Binnode));
(*root)->data=ch;
Creat_Bintree(&(*root)->lchild);
Creat_Bintree(&(*root)->rchild);
}
}
/* 按照前序递归遍历二叉树 */
void Preorder1(Bintree t) {
if (t!=NULL)
{
printf("%c",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}
int mian(){
Bintree T;
creat_Bintree(&T);
Preorder1(T);
return 0; }
这样还是有问题#include<stdio.h> #include<malloc.h> typedef struct Binnode //二叉树结点结构体 { char data; struct Binnode *lchild; struct Binnode *rchild; }Binnode; typedef Binnode *Bintree; /* 按照前序遍历建立二叉树 */void Creat_Bintree(Bintree *root) { char ch; while((ch = getchar())!= EOF) if (ch==' ') { *root=NULL; } else { *root=(Binnode*)malloc(sizeof(Binnode)); (*root)->data=ch; Creat_Bintree(&(*root)->lchild); Creat_Bintree(&(*root)->rchild); } } /* 按照前序递归遍历二叉树 */void Preorder1(Bintree t) { if (t!=NULL) { printf("%c",t->data); Preorder1(t->lchild); Preorder1(t->rchild); } } int mian(){ Bintree T; creat_Bintree(&T); Preorder1(T); return 0; }
这样还是有问题